1

I have been coding a game for about an hour now and I have run into an error with my script that I can't seem to fix. What I mainly seem to be focusing on is the isGrounded update part of my script:

 private void OnCollisionExit2D(Collision2D collision) { isGrounded = false; } private void OnCollisionEnter2D(Collision2D collision2) { isGrounded = true; } 

But I am concerned it might be a different part of my script because I don't think anything is wrong with this part. Here is the full script:

using UnityEngine; using System.Collections; public class move : MonoBehaviour { public float speed; float moveVelocity; public float jumpPower = 300.0f; private bool isGrounded = false; private bool facingEast = false; void Update() { moveVelocity = 0; //Left Right Movement if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A)) { moveVelocity = -speed; facingEast = false; } if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D)) { moveVelocity = speed; facingEast = true; } GetComponent<Rigidbody2D>().velocity = new Vector2(moveVelocity, GetComponent<Rigidbody2D>().velocity.y); Rigidbody2D rb = GetComponent<Rigidbody2D>(); if (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Space)) { if (isGrounded == true) { rb.AddForce(Vector2.up * jumpPower); } } } private void OnCollisionExit2D(Collision2D collision) { isGrounded = false; } private void OnCollisionEnter2D(Collision2D collision2) { isGrounded = true; } } 

Sorry if this is a bad question or if there is an obvious answer and I am just being stupid. I am still fairly new to C#

8
  • 1
    Try to set a breakpoint where isGrounded is getting set, or print the contents of the variable at the start of every Update. That should get you started on debugging this :-) Commented Mar 16, 2020 at 23:17
  • An easier solution to figure out what the variable is set to constantly is to just make the bool public and then view it through my objects behaviors, so I'll just try that instead Commented Mar 16, 2020 at 23:23
  • Ok so when I move away from the wall it says that boolegan IsGrounded sets to false, the weird thing is that it doesn't immediately set to false when I am no longer touching the wall, it waits for about 400 milliseconds than sets itself to false. Commented Mar 16, 2020 at 23:26
  • The Editor UI does not always update immediately (IIRC), so you may be better off logging the value out. Commented Mar 16, 2020 at 23:30
  • 1
    Debug.Log(isGrounded) Commented Mar 17, 2020 at 0:03

1 Answer 1

1

In general in OnCollisionEnter2D and OnCollisionExit2D you should check what you are colliding with!

e.g. using Tags and CompareTag like

private void OnCollisionExit2D(Collision2D collision) { if(!collision.transform.CompareTag("Ground")) return; // only do this if the thing you don't collide // anymore is actually the ground isGrounded = false; } private void OnCollisionEnter2D(Collision2D collision2) { if(!collision.transform.CompareTag("Ground")) return; // only do this if the thing you collided with is actually the ground isGrounded = true; } 

otherwise what happens when you collide with a wall is a OnCollisionEnter2D followed by a OnCollisionExit2D => isGrounded = false;


Also in general: Don't use GetComponent in Update and especially not for getting the same reference in 3 different places!

Rather get it once in Awake or already assign it via the Inspector

// already assign via the Inspector by drag&drop [SerializeField] private Rigidbody2D _rigidbody; // as fallback assign ONCE in the beginning private void Awake() { if(!_rigidbody) _rigidbody = GetComponent<Rigidbody2D>(); } 

and later reuse it

void Update() { moveVelocity = 0; //Left Right Movement if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A)) { // I would use -= and += here so when pressing both buttons // you simply stay at 0. But that's only my opinion of course // Alternatively I would use 'else if' in order to make the blocks exclusive moveVelocity -= speed; facingEast = false; } if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D)) { moveVelocity += speed; facingEast = true; } _rigidbody.velocity = new Vector2(moveVelocity, _rigidbody.velocity.y); // here it is cheeper to first check the isGrounded flag // and only if it is true get the input if (isGrounded && (Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Space))) { _rigidbody.AddForce(Vector2.up * jumpPower); } } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thankyou this helped so much, was totally stumped till I saw your answer! (:

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.