my ball sometimes jumps higher or sometimes not

player = ball

my ball sometimes jumps higher or sometimes not i set the jump height to 12 but it jumps sometimes much much higher than that or sometimes not here are the scripts i am used

using System;

using UnityEngine;

namespace UnityStandardAssets.Vehicles.Ball
{

public class Ball : MonoBehaviour
{

    [SerializeField] private float m_MovePower = 5; // The force added to the ball to move it.
    [SerializeField] private bool m_UseTorque = true; // Whether or not to use torque to move the ball.
    [SerializeField] private float m_MaxAngularVelocity = 25; // The maximum velocity the ball can rotate at.
    [SerializeField] private float m_JumpPower = 12; // The force added to the ball when it jumps.

    private const float k_GroundRayLength = 1f; // The length of the ray to check if the ball is grounded.
    private Rigidbody m_Rigidbody;

    private void Start()
    {
        m_Rigidbody = GetComponent<Rigidbody>();
        // Set the maximum angular velocity.
        GetComponent<Rigidbody>().maxAngularVelocity = m_MaxAngularVelocity;
    }

    public void Move(Vector3 moveDirection, bool jump)
    {
        // If using torque to rotate the ball...
        if (m_UseTorque)
        {
            // ... add torque around the axis defined by the move direction.
            m_Rigidbody.AddTorque(new Vector3(moveDirection.z, 0, -moveDirection.x)*m_MovePower);
        }
        else
        {
            // Otherwise add force in the move direction.
            m_Rigidbody.AddForce(moveDirection*m_MovePower);
        }

        // If on the ground and jump is pressed...
        if (Physics.Raycast(transform.position, -Vector3.up, k_GroundRayLength) && jump)
        {				               
			m_Rigidbody.AddForce(Vector3.up*m_JumpPower, ForceMode.Impulse);
        }
    }
}

}
and the second script

using System;

using UnityEngine;

namespace UnityStandardAssets.Vehicles.Ball

{

public class BallUserControl : MonoBehaviour
{
    private Ball ball; // Reference to the ball controller.

    private Vector3 move;
    // the world-relative desired move direction, calculated from the camForward and user input.

    private Transform cam; // A reference to the main camera in the scenes transform
    private Vector3 camForward; // The current forward direction of the camera
    private bool jump; // whether the jump button is currently pressed

    private void Awake()
    {
        // Set up the reference.
        ball = GetComponent<Ball>();

        // get the transform of the main camera
        if (Camera.main != null)
        {
            cam = Camera.main.transform;
        }
        else
        {
            Debug.LogWarning(
                "Warning: no main camera found. Ball needs a Camera tagged \"MainCamera\", for camera-relative controls.");
            // we use world-relative controls in this case, which may not be what the user wants, but hey, we warned them!
        }
    }

    private void Update()
    {
        // Get the axis and jump input.

        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        jump = Input.GetButton("Jump");

        // calculate move direction
        if (cam != null)
        {
            // calculate camera relative direction to move:
            camForward = Vector3.Scale(cam.forward, new Vector3(1, 0, 1)).normalized;
            move = (v*camForward + h*cam.right).normalized;
        }
        else
        {
            // we use world-relative directions in the case of no main camera
            move = (v*Vector3.forward + h*Vector3.right).normalized;
        }
    }

    private void FixedUpdate()
    {
        // Call the Move function of the ball controller
        ball.Move(move, jump);
        jump = false;
    }
}

}

THANKS FOR ANY HELP

Hi, Update() and FixedUpdate() are not called the same amount of time. What happens is that sometimes 2 Update() are called for only 1 FU (= on high framerates; the inverse can happen on low framerates). Imagine the first U the jump is pressed but the second one it is no more. No FU was called in between so your character won’t jump. It’s the same for the movement: the first frame the axis may report 1.0 but the second only 0.5 so the “move” vector will only be 0.5 when FU interpret it.

Now, how to fix it depends on what behavior you want: do you want to average the move vector in the case of 2 or more U for 1 FU ? If it’s = 0 : set the value according to the axis, if it’s not 0, average the value between the existing value and the new one. Set “move” to 0 after you “used” it in FU the same way you do for the “jump” value.
For the jump, it’s quite simple to have it not set to false in U if it’s already to true.

It’s the way to do it to have very precise controls = interpret inputs during Update and use those values in FixedUpdate. It’s a little complicated though and should also take in account deltaTime in Update to know for how long a button was pressed or an axis moved.

Put the part of the code that jumps the ball FixedUpdate(). I actually had exactly the same problem. With the ball jumping and everything. It will sometimes jump higher because of a higher framerate, because Update() is called each frame, whereas FixedUpdate() is called a set amount of times per second.