-1
\$\begingroup\$

My issue is that the player sometimes (occasionally) doesn't jump up while moving. It sometimes works, sometimes don't. It's weird, and I am not sure whether it's the code problem, or the tile collider's problem, here's the code:

using UnityEngine;

public class PlayerController : MonoBehaviour
{

    [SerializeField] private LayerMask platformsLayerMask;

    public Rigidbody2D rb;

    public float MovementSpeed = 100f;

    public float JumpingHeight = 100f;

    public BoxCollider2D bc;

    public float fallMultiplier = 2.5f;
    public float lowJumpMultiplier = 2f;

    void Awake()
    {
        rb = transform.GetComponent<Rigidbody2D>();
        bc = transform.GetComponent<BoxCollider2D>();
    }

    void FixedUpdate()
    {
        if (IsGrounded() && Input.GetKeyDown(KeyCode.Space))
        {
            rb.velocity = new Vector2(rb.velocity.x, JumpingHeight * Time.deltaTime);
            Debug.Log("Jumping");
        }
        HandleMovement();

    }

    private bool IsGrounded()
    {
        RaycastHit2D raycastHit2d = Physics2D.BoxCast(bc.bounds.center, bc.bounds.size, 0f, Vector2.down, 1f, platformsLayerMask);
        Debug.Log(raycastHit2d.collider);
        return raycastHit2d.collider != null;
    }

    void HandleMovement()
    {
        if (Input.GetKey(KeyCode.A))
        {
            rb.velocity = new Vector2(-MovementSpeed * Time.deltaTime, rb.velocity.y);
            Debug.Log("Going Left");
        }
        else
        {
            if (Input.GetKey(KeyCode.D))
            {
                rb.velocity = new Vector2(+MovementSpeed * Time.deltaTime, rb.velocity.y);
                Debug.Log("Going Right");
            }
            else
            {
                //no keys pressed
                rb.velocity = new Vector2(0, rb.velocity.y);
                Debug.Log("No key pressed");
            }
        }
        if (rb.velocity.y < 0) //reponsive jumping and falling
        {
            rb.velocity += Vector2.up * Physics2D.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
        }
        else if (rb.velocity.y > 0 && Input.GetKeyDown(KeyCode.Space))
        {
            rb.velocity += Vector2.up * Physics2D.gravity.y * (lowJumpMultiplier - 1) * Time.deltaTime;
        }

    }

}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

This happens because FixedUpdate() is not meant for input handling, as it runs every a certain amount of time (specified in settings) and not in every frame so it's not guaranteed to run every frame. You should add it to the Update() method instead, which is guaranteed to run every frame.

\$\endgroup\$
6
  • \$\begingroup\$ Thanks, now it works everytime. The issue is everytime I hit play, and then if the first key I hit is a space, then it will bounce super high. But if I don't hit space the first time and instead press d or a to move left to right, the jump is normal. \$\endgroup\$
    – user136618
    Mar 10, 2020 at 9:15
  • 2
    \$\begingroup\$ @SkullCutter You should mark this question as answered, create a new question for your new issue, and post a link to that question in these comments \$\endgroup\$
    – Natalo77
    Mar 10, 2020 at 9:23
  • \$\begingroup\$ Why would they accept the answer if it creates a new bug? \$\endgroup\$
    – Vaillancourt
    Mar 10, 2020 at 13:24
  • \$\begingroup\$ @Vaillancourt Because the problem asked is solved. Other people in the future might have the same problem as the one that later occured here and they most probably won't expect to find a solution in the comments. Each question should solve only one specific problem. \$\endgroup\$ Mar 10, 2020 at 14:29
  • \$\begingroup\$ No, their problem is not solved, they can't jump the way they want and they have to post a near-duplicate question to get it fixed. \$\endgroup\$
    – Vaillancourt
    Mar 10, 2020 at 14:46

You must log in to answer this question.