1
\$\begingroup\$

Here is my code. I have an empty Gameobject directly underneath the child with a small radius checking the ground every thing seems to be correctly covered, but for some reason the jumping mechanic is very finicky and broken feeling. Can you take a look a tell me what you see? Thank you for your time!

public class PlayerMovement : MonoBehaviour 
{

    public float speed;
    public float height;

    public Transform groundCheck;
    public float groundCheckRadius;
    public LayerMask whatIsGround;
    private bool grounded;

    private bool doubleJumped;

    // Update is called once per frame


    void FixedUpdate () 
    {
        if (grounded) 
        {
            doubleJumped = false;
        }

        if (Input.GetButton ("Horizontal")) 
        {
            transform.Translate (Vector3.right * speed * Time.deltaTime);
        }

        if( Input.GetButton("Left"))
        {
            transform.Translate (Vector3.left * speed * Time.deltaTime);
        }

        if (Input.GetButton ("Jump") && grounded) 
        {
            //transform.Translate (Vector3.up * height * Time.deltaTime);
            Jump();
        }

        if (Input.GetButtonDown ("Jump") && !doubleJumped && !grounded) 
        {
            Jump();
            doubleJumped = true;
        }

        grounded = Physics2D.OverlapCircle(groundCheck.position,groundCheckRadius,whatIsGround);

    }

    void Jump()
    {
        transform.Translate (Vector3.up * height * Time.deltaTime);
        return;
    }
}
\$\endgroup\$
1
\$\begingroup\$

finicky and broken feeling

This should probably just be the issue because you're using FixedUpdate() instead of Update(), thus the groundcheck / jumpcheck, doesn't happen every frame, but every physics update, try changing that and it should be fixed.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Careful though. Moving time-based gameplay into the non-fixed time step of Update() can cause your game to play differently depending on the framerate (as soon as you have any non-linear behaviours). That's why physics and movement are normally handled in FixedUpdate(). \$\endgroup\$ – DMGregory Jun 26 '15 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.