So I started Unity yesterday and I decided to create a character that moves and jumps, walking and jumping its done, although when I press the space the key I can press it multiple times and the character just flies away. So basically I want to make the jump limited, I tried various methods watch various videos but I cant understand how I can solve it, so the forum seems like the best place to show my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class scr: MonoBehaviour
{
public float speed;
private Rigidbody rb;
public float jumpforce = 1f;
public bool isGrounded;
//Vai obter o rigid body do character
void Start()
{
rb = GetComponent<Rigidbody>();
}
//Movimentaçao do character
void Update()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
float moveUp = Input.GetAxis("Jump");
Vector3 movement = new Vector3(moveHorizontal, moveUp, moveVertical);
rb.AddForce(movement * speed);
if (isGrounded == true && Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(Vector3.up * jumpforce, ForceMode.Impulse);
}
if (isGrounded == false && Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(Vector3.down * jumpforce, ForceMode.Impulse);
}
}
}