Does anyone know what's wrong with this code?

I followed a tutorials instructions for programming movement but when I changed the movement from every update to Fixed Update, I lost the ability to move here’s the code any help?

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

public class RubyController : MonoBehaviour
{
    Rigidbody2D rigidbody2d;
    float horizontal;
    float vertical;
    // Start is called before the first frame update
    void Start()
    {
        rigidbody2d = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");
    }
    void FixedUpdate()
    {


        Vector2 position = rigidbody2d.position;
        position.x = position.x + 3.0f * horizontal * Time.deltaTime;
        position.y = position.y + 3.0f * vertical * Time.deltaTime;

        rigidbody2d.MovePosition(position);
    }
}

You see how on line #8 and #9 you have fields for horizontal and vertical?

But then in Update (line #19 and line #20) you declare new floats? The floats declared in lines #19 & #20 fall out of scope. That means they’re not recorded anywhere so you can’t make use of those values in FixedUpdate.

Instead, try:

void Update()
{
    this.horizontal = Input.GetAxis("Horizontal");
    this.vertical = Input.GetAxis("Vertical");
}

By using the C# keyword this, you assign the float to the fields of the instance of this class (line #8 and #9). Technically, you don’t have to use “this.someVariable” and can just say “someVariable = 12f” and the compiler will figure out that you mean to use this someVariable on the class instance.

Here’s an article on the concept of scope. Scope of Variables in C# - GeeksforGeeks

1 Like

Please note that you already asked this question here . Please don’t post duplicates, thanks.

The problem is the variable scope as Lo-renzo answer explains.

In Unity we want to get the input inside the Update but since you use RigidBody2D (Physics2D engine) to perform the movement we want it to happen inside FixedUpdate.

If you try to get the input inside the FixedUpdate it will not feel correct - there will be a delay between player pushing the keys and the player movement happening. That is why you need to save the values received inside the Update in a global variable - 2 floats or in a Vector2 and next use the in the FixedUpdate.

I hope it gives you some understanding of why it was done that way :slight_smile:

It wasn’t my intention to create a duplicate, in that post I asked how I would check what’s wrong for future reference while in this one I asked how to fix the problem, thanks for the notification though!

Yeah, you cannot post two of the exact same code to both ask what’s wrong then another asking how to fix it. The two are the same thing so please keep it in a single thread.

Anyway, glad you’ve got some answers.

That’s good to know, thanks for the info