TransWikia.com

i am making a ball rolling game and i added jumping but for some reason i can jump infinite times

Stack Overflow Asked by saif7899 on January 5, 2021

i am making a ball rolling game and i added jumping the the game but for some reason i can jump infinite times here is my code (i put the jump script with the movement script)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ballcontroll : MonoBehaviour
{

    public float speed;

    private Rigidbody rb;

    

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    private void FixedUpdate()
    {
        float moveHorizontal = Input.GetAxis("Vertical");
        float moveVertical = Input.GetAxis("Horizontal");
        float jump;
        if (Input.GetKeyDown(KeyCode.Space))
            jump = 14.0f;
        else
            jump = 0;
        Vector3 movement = new Vector3 (moveVertical, jump , moveHorizontal);

        rb.AddForce(movement * speed);
    
    }

}

One Answer

You can jump infinitely because you are not checking if the ball is on the ground. There are several ways to solve this problem. I would recommend you to watch this video.

You could also watch this if you want to know about alternative ways of checking for ground.

Correct answer by SushiWaUmai on January 5, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP