Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Does anyone know what's wrong with this code?

Discussion in '2D' started by SoupAuthority, Mar 18, 2022.

  1. SoupAuthority

    SoupAuthority

    Joined:
    Mar 10, 2022
    Posts:
    24
    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?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RubyController : MonoBehaviour
    6. {
    7.     Rigidbody2D rigidbody2d;
    8.     float horizontal;
    9.     float vertical;
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.         rigidbody2d = GetComponent<Rigidbody2D>();
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         float horizontal = Input.GetAxis("Horizontal");
    20.         float vertical = Input.GetAxis("Vertical");
    21.     }
    22.     void FixedUpdate()
    23.     {
    24.  
    25.  
    26.         Vector2 position = rigidbody2d.position;
    27.         position.x = position.x + 3.0f * horizontal * Time.deltaTime;
    28.         position.y = position.y + 3.0f * vertical * Time.deltaTime;
    29.  
    30.         rigidbody2d.MovePosition(position);
    31.     }
    32. }
    33.  
     
  2. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,319
    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:
    Code (CSharp):
    1. void Update()
    2. {
    3.     this.horizontal = Input.GetAxis("Horizontal");
    4.     this.vertical = Input.GetAxis("Vertical");
    5. }
    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. https://www.geeksforgeeks.org/scope-of-variables-in-c-sharp/
     
    Kurt-Dekker likes this.
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,521
    Please note that you already asked this question here. Please don't post duplicates, thanks.
     
  4. SunnyValleyStudio

    SunnyValleyStudio

    Joined:
    Mar 24, 2017
    Posts:
    67
    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 :)
     
  5. SoupAuthority

    SoupAuthority

    Joined:
    Mar 10, 2022
    Posts:
    24
    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!
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,521
    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.
     
  7. SoupAuthority

    SoupAuthority

    Joined:
    Mar 10, 2022
    Posts:
    24
    That's good to know, thanks for the info