- Home /
Question by
venomjadhav · Aug 04, 2020 at 06:03 AM ·
scripting beginnergameplay
My objects jumps even while mid-air.
I'm working on a small platformer just to get a hang with Unity. I coded a sphere with the following code: -
using UnityEngine;
using UnityEngine.SceneManagement;
public class movement : MonoBehaviour
{
Rigidbody rb;
public float speed = 5;
public float jumpSpeed = 2;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
if(Input.GetKey(KeyCode.A))
{
rb.AddForce(Vector3.left * speed);
}
if(Input.GetKey(KeyCode.D))
{
rb.AddForce(Vector3.right * speed);
}
if (Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(Vector3.up * jumpSpeed);
}
if(Input.GetKey(KeyCode.Escape))
{
SceneManager.LoadScene("pauseMenu");
Time.timeScale = 0f;
}
}
Problem is, my playerObject (the sphere) is able to jump mid-air lot by pressing Space repeatedly. And I want it to jump only if it touches the ground. Help will be appreciated.
Comment