'I am trying to make a jump with this fps controller. Everything I've tried so far doesn't work. Can anyone help me? [closed]

public class movement : MonoBehaviour
{
    public CharacterController controller;

    public float speed = (int)12f;

    public float gravity = (int)-9.81;

    public Transform groundCheck;
    public float groundDistance = (int)0.4f;
    public LayerMask groundMask;
    Vector3 Velocity;
    bool isGrounded;
    public float jumpHieght = (int)3f;

    // Update is called once per frame
    void Update()
    {
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
        if (isGrounded && Velocity.y < 0) ;
        {
            Velocity.y = -4f;
        }
    
        
        float x = Input.GetAxis("Horizontal");
        
        float z = Input.GetAxis("Vertical");
    
        Vector3 move = transform.right * x + transform.forward * z;
    
        controller.Move(move * speed * Time.deltaTime);
    
        if (isGrounded == true)
        {
            if (Input.GetButtonDown("Jump")) ;
    
            Velocity.y = Mathf.Sqrt(jumpHieght * -2f * gravity);
        }
    
      
        Velocity.y += gravity * Time.deltaTime;
    
        controller.Move(Velocity * Time.deltaTime);
    
        
    }

}

You might notice this as Brackey's fps code with some tweaks (because it is) but his jump code didn't work. What happens is the capsule continues to rise.



Solution 1:[1]

    if (isGrounded && Velocity.y < 0) ;
    {
        Velocity.y = -4f;
    }

if does not require a semicolon. Do this instead ( for all of your ifs):

    if (isGrounded && Velocity.y < 0)
    {
        Velocity.y = -4f;
    }

The long version: if says: if some condition is true, execute the next statement or block. Since you're putting the ; right after the closing ) in the if, the "next statement" is empty, so your if doesn't do anything.

Also, things like this:

public float groundDistance = (int)0.4f;

This takes the floating point value 0.4, casts it to an int (which equals 0), and assigns that to groundDistance. So, your groundDistance value is always 0. The line should be:

public float groundDistance = 0.4f;

... and you've done that in several places as well.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1