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#
isGroundedis getting set, or print the contents of the variable at the start of everyUpdate. That should get you started on debugging this :-)Debug.Log(isGrounded)