I hello i am very new to unity and this is my first platformer. I made my player jump and implemented my ground check with just a trigger enter 2d and it all workes fine but after some jumps my player just stops jumping here is my code: Code (CSharp): public class Player : MonoBehaviour { [SerializeField] private int Player_Move_Speed; [SerializeField] private int Jump_height; private bool CanJump = false; void Start() { } void Update() { Movement(); } private void Movement() { if (Input.GetKey(KeyCode.A)) { transform.Translate(Vector3.left * Player_Move_Speed * Time.deltaTime); } if (Input.GetKey(KeyCode.D)) { transform.Translate(Vector3.right * Player_Move_Speed * Time.deltaTime); } if (CanJump == true) { if (Input.GetKeyDown(KeyCode.W)) { GetComponent<Rigidbody2D>().AddForce(new Vector2(0, Jump_height), ForceMode2D.Impulse); CanJump = false; } } if (CanJump == false) { return; } } private void OnTriggerEnter2D(Collider2D other) { if(other.tag== "GROUND") { Debug.Log("landed"); CanJump = true; } } }
Put a few more debug.logs in, i assume you character either gets 'stuck' in the ground and can't be accelerated out of it, or he's levitating exactly at 0 or something like that.
There might be some instances where CanJump is false and you have already entered the ground collider so nothing is going to trigger to change CanJump back to true. Checking if you are on ground every frame in Update(), or every physics update in FixedUpdate() is one possible solution. You could try changing the method to OnTriggerStay2D. That might work for you. This is more costly cpu wise, but if your game is small then you shouldn't have to worry about it. Better to get your game working and fun, then worry about performance. also you can remove: Code (CSharp): if (CanJump == false) { return; } as it is the last thing in the method anyways, it is not preventing any other code from being run, so it does not do anything. Your code looks well structured and well thought out. Keep up the good work!