Reputation: 19
I did a jump script. But when i press space, my character first jumping, and after flying forever. And i get this problem my almost all projects. How can i fix this?
My script:
Rigidbody2D rb;
public float speed = 2.5f;
public float jumpSpeed = 50f;
bool isJump = false;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update() {
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Vector3 force = new Vector3(horizontal,rb.velocity.y,vertical);
rb.AddForce(force*speed);
if(Input.GetKeyDown(KeyCode.Space)){
if(isJump == false)
{
rb.velocity = Vector2.up * jumpSpeed;
isJump = true;
}
}
}
private void OnCollisionEnter2D(Collision2D other) {
isJump = false;
}
Upvotes: 1
Views: 45