unity – Character bobs when moving

Whenever I try to move a character in unity, the character kind of, like, bobs around when walking. Is there any way to remedy this? I have tried the trick of putting the inputs into update and the outputs into fixedupdate, if that’s any consolation. Also please ignore the fact that I’ve used the arrow keys instead of getaxis.

using System.Collections.Generic;
using UnityEngine;

public class MovePlayer : MonoBehaviour
{

    public bool upArrow = true;
    public bool downArrow = true;
    public bool leftArrow = true;
    public bool rightArrow = true;
    public float speed;
    public bool test = false;

    void Update()
    {
        if (Input.GetKey(KeyCode.UpArrow))
        {
            upArrow = true;
        }
        else
        {
            upArrow = false;
        }

        if (Input.GetKey(KeyCode.DownArrow))
        {
            downArrow = true;
        }
        else
        {
            downArrow = false;
        }

        if (Input.GetKey(KeyCode.LeftArrow))
        {
            leftArrow = true;
        }
        else
        {
            leftArrow = false;
        }

        if (Input.GetKey(KeyCode.RightArrow))
        {
            rightArrow = true;
        }
        else
        {
            rightArrow = false;
        }

    }

    void FixedUpdate()
    {

        if (upArrow == true)
        {
            transform.position = new Vector3(transform.position.x, transform.position.y + speed, 0);
        }

        if (downArrow == true)
        {
            transform.position = new Vector3(transform.position.x, transform.position.y - speed, 0);
        }

        if (leftArrow == true)
        {
            transform.position = new Vector3(transform.position.x - speed, transform.position.y, 0);
        }

        if (rightArrow == true)
        {
            transform.position = new Vector3(transform.position.x + speed, transform.position.y, 0);
        }

    }

    void OnTriggerEnter2D(Collider2D bump)
    {

        if (bump.gameObject.tag == "Wall")
        {

            test = true;

        }
        else
        {

            test = false;

        }

    }


}
```

7646

BlackHatKings: Proxy Lists
Posted by: Afterbarbag
Publication time: June 11, 2019 at 06:50 PM.