Answered Can someone help me?

DoomPriestK

New Coder
Nov 30, 2019
12
5
3
Hi guys, new user here!

Getting a response from the Unity community can sometimes be a bit slow so I decided to ask in other forums if someone can help

I'm new to unity and have been writing a 2D platformer

My code is using GetAxisRaw to determine if the player is crouching then uses a box collider script to move the characters collider box around, then toggles the public bool crouching (which I believe triggers the appropriate animation in the animator)

I need to apply the same thing but using a GetKey so I can assign a different duck button for each player, i've tried several ways to make it work but im just a novice and could use some help:

My Player Controller Script:
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{

    public float moveSpeed;
    public float jumpForce;

    public KeyCode left;
    public KeyCode right;
    public KeyCode jump;
    public KeyCode throwNut;

    private Rigidbody2D theRB;

    public Transform groundCheckPoint;
    public float groundCheckRadius;
    public LayerMask whatIsGround;

    public Transform CeilingCheck;
   
    public bool isGrounded;
    private bool isCrouched;
    private float crouch;
    public bool crouching;
   
    private Animator anim;

    public GameObject throwingNut;
    public Transform ThrowPoint;
       
    // Start is called before the first frame update
    void Start()
    {
        theRB = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
       
    }

    // Update is called once per frame
    void Update()
    {

        isGrounded = Physics2D.OverlapCircle(groundCheckPoint.position, groundCheckRadius, whatIsGround);
        isCrouched = Physics2D.OverlapCircle(CeilingCheck.position, groundCheckRadius, whatIsGround);

        crouch = Input.GetAxisRaw("CrouchInput");
        CrouchFunction();
        GetComponent<Animator>().SetBool("Crouch", crouching);

        if(Input.GetKey(throwNut))
        {
            GameObject nutClone = (GameObject)Instantiate(throwingNut, ThrowPoint.position, ThrowPoint.rotation);
            nutClone.transform.localScale = transform.localScale;
            anim.SetTrigger("Throw");
        }
        if (Input.GetKey(left))
        {
            theRB.velocity = new Vector2(-moveSpeed, theRB.velocity.y);
        }
        else if (Input.GetKey(right))
        {
            theRB.velocity = new Vector2(moveSpeed, theRB.velocity.y);
        }
        else
        {
            theRB.velocity = new Vector2(0, theRB.velocity.y);
        }
        if (Input.GetKeyDown(jump) && isGrounded)
        {
            theRB.velocity = new Vector2(theRB.velocity.x, jumpForce);
        }
        if (theRB.velocity.x < 0)
        {
            transform.localScale = new Vector3(-1, 1, 1);
        }
        else if(theRB.velocity.x > 0)
        {
            transform.localScale = new Vector3(1, 1, 1);
        }
        anim.SetFloat("Speed", Mathf.Abs(theRB.velocity.x));
        anim.SetBool("Grounded", isGrounded);
       
    }
    void CrouchFunction()
    {
        if((crouch!=0 || isCrouched==true) && isGrounded == true)
        {
            crouching = true;
            moveSpeed = 1.5f;
        }
        else
        {
            crouching = false;
            moveSpeed = 6f;
        }
    
    }
}
My Box Collider Script:

Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ColliderControl : MonoBehaviour
{
    public BoxCollider2D stand;
    public BoxCollider2D crouch;
    public CircleCollider2D circle;

    PlayerController playerC;

    // Start is called before the first frame update
    void Start()
    {
        playerC = GetComponent<PlayerController>();
        stand.enabled = true;
        crouch.enabled = false;
        circle.enabled = true;
    }

    // Update is called once per frame
    void Update()
    {
        if(playerC.isGrounded == false)
        {
            stand.enabled = true;
            crouch.enabled = false;
            circle.enabled = true;
        }
        else
        {
            if(playerC.crouching == true)
            {
                stand.enabled = false;
                crouch.enabled = true;
                circle.enabled = true;
            }
            else
            {
                stand.enabled = true;
                crouch.enabled = false;
                circle.enabled = true;
            }
        }
    }
}
Any help would be appreciated, thanks!
 

DoomPriestK

New Coder
Nov 30, 2019
12
5
3
I NAILED IT!

This is wierd, twice now I bash my head against a problem for days, then the moment I get frustrated enough to ask on forums I solve it LOL!

For those interested:

Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    // THIS CODE IS BUILT ON THE PREFAB ROBOT PROVIDED IN THE UNITY
    // SAMPLE ASSETS PACK

    public float moveSpeed;
    public float jumpForce;

    public KeyCode left;
    public KeyCode right;
    public KeyCode jump;
    public KeyCode duck;
    public KeyCode throwNut;

    private Rigidbody2D theRB;

    public Transform groundCheckPoint;
    public float groundCheckRadius;
    public LayerMask whatIsGround;

    public Transform CeilingCheck;
    
    public bool isGrounded;
    private bool isCrouched;
    private float crouch;
    public bool crouching;
    
    private Animator anim;

    public GameObject throwingNut;
    public Transform ThrowPoint;
        
    // Start is called before the first frame update
    void Start()
    {
        theRB = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        // USES THE GroundCheck CHILD ITEM OF THIS PLAYER TO DETERMINE IF PLAYER IS GROUNDED
        isGrounded = Physics2D.OverlapCircle(groundCheckPoint.position, groundCheckRadius, whatIsGround);

        // USES THE CeilingCheck CHILD OBJECT OF THIS PLAYER TO DETERMINE IF PLAYER
        // IS TOUCHING A CIELING
        isCrouched = Physics2D.OverlapCircle(CeilingCheck.position, groundCheckRadius, whatIsGround);

        // TO DUCK AND CHANGE THE PLAYERS SPEED APPROPRIATELY IF DUCK IS HELD DOWN
        if (Input.GetKey(duck))
        {
            if (isGrounded == true)
            {
                anim.SetBool("Crouch", true);
                crouching = true;
                moveSpeed = 1.5f;
            }
            else if (isCrouched == true && isGrounded == true)
            {
                anim.SetBool("Crouch", true);
                crouching = true;
                moveSpeed = 1.5f;
            }
            else if (isCrouched == true && isGrounded == false)
            {
                crouching = false;
                anim.SetBool("Crouch", false);
            }
        }
        
        // ELSE PLAYER IS STANDING!
        else
        {
            crouching = false;
            anim.SetBool("Crouch", false);
            moveSpeed = 6f;
        }

        // THROWS WHATEVER PREFAB IS ASSIGNED TO THE Throwing Nut SECTION 
        // OF THE INSPECTOR
        if (Input.GetKey(throwNut))
        {
            GameObject nutClone = (GameObject)Instantiate(throwingNut, ThrowPoint.position, ThrowPoint.rotation);
            nutClone.transform.localScale = transform.localScale;
            anim.SetTrigger("Throw");
        }

        // MOVES LEFT WHEN PRESSING ASSIGNED KEY IN THE left BUTTON
        // FIELD IN THE INSPECTOR
        if (Input.GetKey(left))
        {
            theRB.velocity = new Vector2(-moveSpeed, theRB.velocity.y);
        }
        
        // MOVES RIGHT WHEN PRESSING ASSIGNED KEY IN THE right BUTTON
        // FIELD IN THE INSPECTOR
        else if (Input.GetKey(right))
        {
            theRB.velocity = new Vector2(moveSpeed, theRB.velocity.y);
        }
        
        // ELSE STAND STILL IF NEITHER RIGHT OR LEFT IS PRESSED
        else
        {
            theRB.velocity = new Vector2(0, theRB.velocity.y);
        }
        
        // TO JUMP AS LONG AS PLAYER isGrounded
        if (Input.GetKeyDown(jump) && isGrounded)
        {
            theRB.velocity = new Vector2(theRB.velocity.x, jumpForce);
        }
        
        // PLAYER DROPS TO THE GROUND?
        if (theRB.velocity.x < 0)
        {
            transform.localScale = new Vector3(-1, 1, 1);
        }
        
        // CHARACTER IS MOVING IN ANOTHER DIRECTION?
        else if(theRB.velocity.x > 0)
        {
            transform.localScale = new Vector3(1, 1, 1);
        }



        // SEND CURRENT PLAYER SPEED TO ANIMATOR SO IT CAN CHECK
        // THE PLAYER IS MOVING A CERTAIN SPEED
        anim.SetFloat("Speed", Mathf.Abs(theRB.velocity.x));
        
        // IF isGrounded CIRCLE IS TOUCHING GROUND, SET ANIMATOR TO
        // GROUNDED SO IT CAN CHECK PLAYER IS Grounded IN ANIMATOR CHECKS
        anim.SetBool("Grounded", isGrounded);
        
    }

}
Thanks for replying anyways, i'm sure I'll get stuck again soon lol!
 
Last edited:

Malcolm

Administrator
Administrator
Verified
Jan 2, 2018
1,103
303
95
Canada
codeforum.org
I'm no way an expert in this but I'll give my two cents; so what you're trying to do is create a multiplayer game on a computer where for example one player is using WASD and the other is using the arrows right?

@TableFlipGod Do you have experience with this?
 

DoomPriestK

New Coder
Nov 30, 2019
12
5
3
I'm no way an expert in this but I'll give my two cents; so what you're trying to do is create a multiplayer game on a computer where for example one player is using WASD and the other is using the arrows right?

@TableFlipGod Do you have experience with this?
EXACTLY!

the crouch code I added on top of the initial movement tutorial i watched so its basically 2 codes merged into one, I tried the below modification but it gives me compile errors now on line 55: if (Input.GetKey(duck)) isCrouched == true && isGrounded == true) (which I tried writing myself so obviously the syntax is probably all messed up)

The crouch button was permalinked to left ctrl so I couldn't assign it in the inspector per-character.

I tried fixing it up like this:

Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    // THIS CODE IS BUILT ON THE PREFAB ROBOT PROVIDED IN THE UNITY
    // SAMPLE ASSETS PACK

    public float moveSpeed;
    public float jumpForce;

    public KeyCode left;
    public KeyCode right;
    public KeyCode jump;
    public KeyCode duck;
    public KeyCode throwNut;

    private Rigidbody2D theRB;

    public Transform groundCheckPoint;
    public float groundCheckRadius;
    public LayerMask whatIsGround;

    public Transform CeilingCheck;
   
    public bool isGrounded;
    private bool isCrouched;
    private float crouch;
    public bool crouching;
   
    private Animator anim;

    public GameObject throwingNut;
    public Transform ThrowPoint;
       
    // Start is called before the first frame update
    void Start()
    {
        theRB = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        // USES THE GroundCheck CHILD ITEM OF THIS PLAYER TO DETERMINE IF PLAYER IS GROUNDED
        isGrounded = Physics2D.OverlapCircle(groundCheckPoint.position, groundCheckRadius, whatIsGround);

        // USES THE CeilingCheck CHILD OBJECT OF THIS PLAYER TO DETERMINE IF PLAYER
        // IS TOUCHING A CIELING
        isCrouched = Physics2D.OverlapCircle(CeilingCheck.position, groundCheckRadius, whatIsGround);

        // TO DUCK AND CHANGE THE PLAYERS SPEED APPROPRIATELY IF DUCK IS HELD DOWN
        if (Input.GetKey(duck)) isCrouched == true && isGrounded == true)
        {
            anim.SetBool("Crouch", true);
            crouching = true;
            moveSpeed = 1.5f;
        }
       
        // ELSE THE PLAYER IS NOT CROUCHING!
        else
        {
            crouching = false;
        }

        // THROWS WHATEVER PREFAB IS ASSIGNED TO THE Throwing Nut SECTION
        // OF THE INSPECTOR
        if (Input.GetKey(throwNut))
        {
            GameObject nutClone = (GameObject)Instantiate(throwingNut, ThrowPoint.position, ThrowPoint.rotation);
            nutClone.transform.localScale = transform.localScale;
            anim.SetTrigger("Throw");
        }

        // MOVES LEFT WHEN PRESSING ASSIGNED KEY IN THE left BUTTON
        // FIELD IN THE INSPECTOR
        if (Input.GetKey(left))
        {
            theRB.velocity = new Vector2(-moveSpeed, theRB.velocity.y);
        }
       
        // MOVES RIGHT WHEN PRESSING ASSIGNED KEY IN THE right BUTTON
        // FIELD IN THE INSPECTOR
        else if (Input.GetKey(right))
        {
            theRB.velocity = new Vector2(moveSpeed, theRB.velocity.y);
        }
       
        // ELSE STAND STILL IF NEITHER RIGHT OR LEFT IS PRESSED
        else
        {
            theRB.velocity = new Vector2(0, theRB.velocity.y);
        }
       
        // TO JUMP AS LONG AS PLAYER isGrounded
        if (Input.GetKeyDown(jump) && isGrounded)
        {
            theRB.velocity = new Vector2(theRB.velocity.x, jumpForce);
        }
       
        // PLAYER DROPS TO THE GROUND?
        if (theRB.velocity.x < 0)
        {
            transform.localScale = new Vector3(-1, 1, 1);
        }
       
        // CHARACTER IS MOVING IN ANOTHER DIRECTION?
        else if(theRB.velocity.x > 0)
        {
            transform.localScale = new Vector3(1, 1, 1);
        }



        // SEND CURRENT PLAYER SPEED TO ANIMATOR SO IT CAN CHECK
        // THE PLAYER IS MOVING A CERTAIN SPEED
        anim.SetFloat("Speed", Mathf.Abs(theRB.velocity.x));
       
        // IF isGrounded CIRCLE IS TOUCHING GROUND, SET ANIMATOR TO
        // GROUNDED SO IT CAN CHECK PLAYER IS Grounded IN ANIMATOR CHECKS
        anim.SetBool("Grounded", isGrounded);
       
    }

}
Like I said im fairly new so still struggling with Syntax :(
 

Malcolm

Administrator
Administrator
Verified
Jan 2, 2018
1,103
303
95
Canada
codeforum.org
That's awesome! Sometimes asking does actually solve your questions! I have marked your thread as a question thread type so please feel free to mark your best answer! :)

I find most calculations are gibberish but have been trying to understand them!