I’ve finally gotten the jump properly, but now the moment the player touches the wall the player can shoot up like a cannonball and stops because of the roof, I have tried to search it online but no one seems to have this problem, can anyone help me?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class moving : MonoBehaviour
{
public float speed = 6f;
public float JumpHeight;
public bool InAir = false;
private Rigidbody2D rb2d;
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
}
private void OnCollisionEnter2D(Collision2D collision)
{
InAir = false;
Debug.Log("InAir false");
}
private void OnCollisionExit2D(Collision2D collision)
{
InAir = true;
Debug.Log("InAir True");
}
void FixedUpdate()
{
Vector2 NoMovement = new Vector2(0f, 0f);
float moveHorizontal = Input.GetAxis("Horizontal");
if (Input.GetKey(KeyCode.A) || (Input.GetKey(KeyCode.LeftArrow)))
{
rb2d.velocity = new Vector2(-speed, rb2d.velocity.y);
}
if (Input.GetKey(KeyCode.S) || (Input.GetKey(KeyCode.RightArrow)))
{
rb2d.velocity = new Vector2(speed, rb2d.velocity.y);
}
if (Input.GetKey(KeyCode.W) || (Input.GetKey(KeyCode.Space)))
{
if (InAir == false)
{
rb2d.AddForce(new Vector2(0, JumpHeight), ForceMode2D.Impulse);
}
}
}
}