I’m working on a 2d platformer game for a school project. I know little about coding, here’s all I have so far.
[SerializeField] private float speed;
private Rigidbody2D body;
private Animator anim;
private bool grounded;
private void Awake()
{
body = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
private void Update()
{
float horizontalInput = Input.GetAxis("Horizontal");
body.velocity = new Vector2(horizontalInput * speed, body.velocity.y);
if (horizontalInput > 0.01f)
transform.localScale = Vector3.one;
else if (horizontalInput < -0.01f)
transform.localScale = new Vector3(-1, 1, 1);
if (Input.GetKey(KeyCode.Space) && grounded)
Jump();
anim.SetBool("Running", horizontalInput != 0);
anim.SetBool("grounded", grounded);
}
private void Jump()
{
body.velocity = new Vector2(body.velocity.x, speed + 5);
grounded = false;
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Ground")
grounded = true;
}
The player can stick to a wall while jumping if you move into it. I want to fix that so the player always falls down. Does anyone know how to do that?