Does my script is correct?

I have structed up my script for giving the jump ability to the object but I’m not sure if the object is really jumping (although I applied Debug.Log() which it changes whenever the condition is met):

{
public bool isGrounded;

private float jumpSpeed = 8f;

private float ySpeed;
 

{   
       ySpeed += Physics.gravity.y * Time.deltaTime;

        if (distanceToBall < 5 && Raycasting.ballHeight >= 2 && isGrounded == true)
        {
            isGrounded = false;

            animator.SetBool("Jump", true);

            ySpeed = jumpSpeed + 3;
           
            Debug.Log("Jump" + ySpeed);
        }
        else
        {
            isGrounded = true;
            
            animator.SetBool("Jump", false);
        }
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Ground")
        {
            isGrounded = true;
        }
    }
}

Thank you.

you are missing the method’s head in line 8. probably void Update().

also, I don’t understand what distanceToBall < 5 && Raycasting.ballHeight >= 2 is supposed to do in your logic, so I cannot say if that makes sense.
I could imagine that animator.SetBool("Jump", false); would be better inside the OnTriggerEnter() method, but maybe it is better here because of the check I didn’t understand (but it will be called almost every frame, so it doesn’t seem to be optimal).

All in all, that could work, but the way it is programmed has some room for improvement.

1 Like

I made a raycasting that measures how high the ball is off the ground. If the ball is higher and equal to 2 the object suppose to jump. I might be forgotten the method’s head when I did copy-paste🙂

Thank you so much! :blush:

Recently I realized that my 3d enemy jump script doesn’t work😐 I also tried rb.AddForce(0, 20, 0) to make the object jump but that doesn’t work either. I can make the player jump however to make the enemy jump I have no idea.