Issues with Update and FixedUpdate with Rigidbody2D Velocity

This has been eating my brain for the past day or so, but I am not understanding how Unity works when regarding this issue. Basically, I learned that the input should always occur in update and physics movement/calculations should occur in fixedupdate. I didn’t ever separate them, I always put both in update or fixedupdate. Finally, I decided to do so, but now there are several issues

I have a script called charactercontroller as follows:

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

public class CharacterController : MonoBehaviour
{

    public Rigidbody2D rb;
    public float moveSpeed;
    public Vector2 inputVector;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        HorizontalInput();
    }

    private void FixedUpdate()
    {
        Movement();
    }

    public void HorizontalInput()
    {
        inputVector.x = Input.GetAxisRaw("Horizontal");
        inputVector.y = 0;
    }

    public void Movement()
    {
        Debug.Log(inputVector);
        if (inputVector.x > 0)
        {
            Debug.Log(inputVector);
            rb.velocity = new Vector2(moveSpeed, rb.velocity.y);
            Debug.Log("rb velocity" + rb.velocity);
        } else if(inputVector.x < 0)
        {
            Debug.Log(inputVector);
            rb.velocity = new Vector2(-moveSpeed, rb.velocity.y);
            Debug.Log("rb velocity" + rb.velocity);
        }
        //else
        //{
        //    Debug.Log(inputVector);
        //    rb.velocity = new Vector2(0, rb.velocity.y);
        //}
    }

My main issue is that when debug.log prints out the rb.velocity, the velocity is 0 and the movespeed the same number of times. The character does not move under any circumstance. I am left thoroughly confused as nothing works. I tried directly using the input vector * movespeed, even splitting it into components of inputvector.x and inputvector.y, but the character is not moving. I even tried passing in the inputvector as a parameter but that didn’t help either. But the issue doesn’t seem to be the inputvector as it always debugs the correct values. So why is the input vector always debugging the correct value, but the rb.velocity is not working at all even though it is running in fixedupdate using the logic of the inputvector?

First, make sure moveSpeed has a value in the editor other than zero, this is most likely the cause of the problem because n * 0 = 0.

Second, you don’t need all the fancy conditionals.

public void Movement()
{
    rb.velocity = new Vector2(inputVector.x * moveSpeed, rb.velocity.y);
}

That is all you need for it to work.

If it doesn’t work after doing that, I have no idea. Movement is usually pretty simple.

1 Like

Ya, I mentioned that I wrote the code exactly that way as well in my original post, back where I said “I tried directly using the input vector * movespeed, even splitting it into components of inputvector.x and inputvector.y, but the character is not moving.” Also movespeed is definitely not 0. I tried playing with many low and high values. It doesn’t move even a decimal fraction. So, that’s why I’m in such a tough position.

Can you show a screenshot of the characters inspector? Make sure rigidbody is not kinematic

1 Like

I happened to kind of find why it’s happening but no idea how to solve it. I’m using Unity 2019.2 for this project, and any public variable I edit in the inspector is not really being reflected when the code runs. I’m assuming the version changed something? But when I set the variables in the code it seems to work fine. That sucks, so I hope that if something changed through the update or something is wrong it can be reflected that way. As of now though, I may revert back to Unity 2017.

What version of Unity are you using? Edit nvm. Just read your post again. Try upgrading to 2019.4.

TBH suggesting that a Rigidbody2D wouldn’t respond or move when its velocity is set is a bug in Unity is kind of silly. This would mean nothing would work at all. Physics doesn’t care when you set things. By this I mean if you set velocity, it’ll be that and it doesn’t matter when you do it; it won’t ignore it.

Create a new project, add a Rigidbody2D and set its velocity and see it move. It’s more likely you’ve got a project with something happening you’re not aware of. I would suggest doing this test then start to reduce what you’re doing until you can identify what is causing it.

Hi, I have made tutorial video with player movement,
Maybe this will help you: