J
J
justSMTH2021-10-25 16:04:21
Unity
justSMTH, 2021-10-25 16:04:21

How to remove wall jump?

I'm new to Unity and decided to start with a simple 2D platformer. After implementing the jump system, I ran into one problem, if the character crashes into the wall of the platform (its side part) and at the same time presses the jump button, then he will fly into space. Here is my code:

using UnityEngine;

public class Movment : MonoBehaviour {
    public float speed, JumpPower;
    public bool OnGround = true;

    void Update() {
        if (Input.GetKey(KeyCode.RightArrow))
            transform.Translate(new Vector2(speed * Time.deltaTime, 0));
        if (Input.GetKey(KeyCode.LeftArrow))
            transform.Translate(new Vector2(-speed * Time.deltaTime, 0));
        if (Input.GetKey(KeyCode.UpArrow) && OnGround) {
            OnGround = false;
            GetComponent<Rigidbody2D>().AddForce(Vector2.up * JumpPower);
        }

    }
    private void OnCollisionEnter2D(Collision2D collision) {
        if ((collision.gameObject.tag == "Platform")) {
            Debug.Log("WellDone");
            OnGround = true;
        }
    }
}

Initially, I tried to solve this problem by making a platform of 3 colliders. One main, the platform itself and 2 additional, side parts. The side colliders were supposed to enclose the main collider. But unfortunately it didn't help. Debugging showed that in any case, the player touches the main platform in addition to the side platform.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Crebor, 2021-11-01
@Crebor

if (Input.GetKey(KeyCode.UpArrow) (key down) try replacing it with
if (Input.GetKeyDown(KeyCode.UpArrow) (key down method)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question