Good Place. Good Things. Good Time.

Unity – 2D Platformer Controller responsiveness issue

I’ve been designing some basic controls for a 2D platformer and managed to get the feeling that I’ve been looking for when moving and jumping, but something is not quite right, it’s not very responsive.

Most of the inputs are detected, but a lot of times, for example, the simple action of pressing some key to jump is completely ignored and the player ends up falling when the key was pressed at the right time. I don’t consider that I’m developing on a laptop with low specs at all if that matters.

Looking at some other questions it has always been said that input detection should be inside the Update function and that physics related stuff should be on FixedUpdate, however, it has not always been very clear what exactly should be on which function, as far as I understand it should go something like this…right?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour {
    public float speed;
    public float jumpForce;
    public float groundCheckRadius;

    public Transform groundCheck;
    public LayerMask groundMask;

    private float direction;

    private bool facingRight;
    private bool shouldJump;
    private bool isGrounded;
    private bool shouldFall;

    private SpriteRenderer renderer;
    private Rigidbody2D rigidbody;

    void Awake() {
        this.renderer = GetComponent();
        this.rigidbody = GetComponent();
    }


    void Start () {
        this.facingRight = true;
        this.shouldJump = false;
        this.isGrounded = false;
        this.shouldFall = false;
    }


    void Update () {
        this.direction = Input.GetAxisRaw("Horizontal");
        this.isGrounded = Physics2D.OverlapCircle(this.groundCheck.position, this.groundCheckRadius, this.groundMask);
        this.shouldJump = Input.GetKeyDown(KeyCode.S) && this.isGrounded;

        if (this.facingRight && this.direction < 0 || !this.facingRight && this.direction > 0)
            this.Flip();

        if (Input.GetKeyUp(KeyCode.S) && this.rigidbody.velocity.y > 0)
            this.shouldFall = true;
    }

    void FixedUpdate() {
        Vector2 velocity = this.rigidbody.velocity;

        if (this.shouldFall) {
            velocity.y = 0f;
            this.shouldFall = false;
        }

        if (this.shouldJump)
            velocity.y = this.jumpForce;

        velocity.x = this.speed * this.direction;

        this.rigidbody.velocity = velocity;
    }

    private void Flip() {
        this.renderer.flipX = this.facingRight;
        this.facingRight = !this.facingRight;
    }
}

I’m not sure if I got the idea of Update vs FixedUpdate correctly because of this lack of responsive jumps, and I’m guessing that if I add some more mechanics, later on, those will react the same or worse.

Something that I noted is that if I move everything to update it now feels right every time I press the jump button, but I’m still not sure if that should be the right approach when building tight controls.

And I still feel lost about what everyone means by “You should manage physics in FixedUpdate”, what do you mean by physics here?

Search more articles here: Unity – 2D Platformer Controller responsiveness issue

Read original article here: Unity – 2D Platformer Controller responsiveness issue



Disclaimers and Denial of responsibility..!

Denial of responsibility! TheTopMag.com is an automatic aggregator of all media around the world. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – jamesonline999@gmail.com . The content will be deleted within 72 hours.
You might also like
Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.