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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question Velocity number is not round when I put it in Fixedupdate.

Discussion in 'Scripting' started by Kokomeant, Dec 23, 2022.

  1. Kokomeant

    Kokomeant

    Joined:
    Dec 23, 2022
    Posts:
    13
    Hello, I'm unity beginner.
    I'm trying to create simple character movement with Rigitbody velocity, And now when i put the velocity code in the FixedUpdate the velocity number decreased to 2.292152 (my speed is: 3), But when i put the code in Update it works perfectly and stayed on 3 without any change, Now I'm asking it because i understood that a Rigitbody proprties should be in FixedUpdate and when you set the velocity and don't add velocity it shouldn't be multiplied with Time.DeltaTime.
    Here is my script:

    Code (CSharp):
    1. public class PlayerMovement : MonoBehaviour
    2. {
    3.     [SerializeField] float ForceSpeed;
    4.     [SerializeField] float jumpPower;
    5.  
    6.     Rigidbody2D Rb2D;
    7.  
    8.     private void Awake()
    9.     {
    10.         Rb2D = GetComponent<Rigidbody2D>();
    11.     }
    12.  
    13.     void Start()
    14.     {
    15.         ForceSpeed = 3f;
    16.         jumpPower = 6f;
    17.     }
    18.  
    19.     string keyDown;
    20.     void KeyIdentificator()
    21.     {
    22.      
    23.         if (Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.A))
    24.         {
    25.             keyDown = "D";
    26.             Debug.Log(keyDown);
    27.         }
    28.         else if (!Input.GetKey(KeyCode.D) && Input.GetKey(KeyCode.A))
    29.         {
    30.             keyDown = "A";
    31.             Debug.Log(keyDown);
    32.         }
    33.         else if (Input.GetKey(KeyCode.D) && Input.GetKey(KeyCode.A))
    34.         {
    35.             keyDown = "Both";
    36.             Debug.Log(keyDown);
    37.         }
    38.         else
    39.         {
    40.             keyDown = "";
    41.             Debug.Log(keyDown);
    42.         }
    43.     }
    44.  
    45.     void CharacterMovement()
    46.     {
    47.         if(keyDown == "D")
    48.         {
    49.             Rb2D.velocity = new Vector2(ForceSpeed, Rb2D.velocity.y);
    50.         }
    51.         else if(keyDown == "A")
    52.         {
    53.             Rb2D.velocity = new Vector2(-ForceSpeed, Rb2D.velocity.y);
    54.         }
    55.         else if(keyDown == "Both")
    56.         {
    57.             Rb2D.velocity = new Vector2(0f, Rb2D.velocity.y);
    58.         }
    59.         else
    60.         {
    61.             Rb2D.velocity = new Vector2(0f, Rb2D.velocity.y);
    62.         }
    63.     }
    64.  
    65.     public int fps = 9999;
    66.  
    67.     private void Update()
    68.     {
    69.         Application.targetFrameRate = fps;
    70.         KeyIdentificator();
    71.        // CharacterMovement();
    72.         Debug.Log(Rb2D.velocity.x);
    73.     }
    74.  
    75.     private void FixedUpdate()
    76.     {
    77.         CharacterMovement();
    78.     }
    79. }
    80.  
    Sorry for my english, And Thanks in advance :)
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    You can only read input per-frame. You cannot/should not do it during the FixedUpdate.

    That's not true or is a bad version of the truth. You can set velocity whenever you like. The simulation will run by default during the FixedUpdate (although you can change that).
     
    Kokomeant likes this.
  3. Kokomeant

    Kokomeant

    Joined:
    Dec 23, 2022
    Posts:
    13
    First of all thank you for the response.
    I think I'm a little lost, So I can set the velocity in Update because I'm setting the velocity right? But if I add velocity it should be in the FixedUpdate right?
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    Update vs FixedUpdate is nothing specifically to do with velocity or physics. Update runs per-frame which can/is variable. FixedUpdate, as its name implies, runs at a fixed interval.

    If you have any value and you add 1 to it during Update, it'll get 1 bigger every frame. If your frame-rate goes up, it'll go up faster and likewise slower if the frame-rate goes down. If you do that during the FixedUpdate then it's frame-rate independent and will always go up by the same rate which is 50Hz by default (can be changed).

    If you apply that to velocity then you'll get a differing change in velocity during update and a constant one if in FixedUpdate.

    As I say, it's nothing to do with physics and everything to do with timing.

    You can, of course, change velocity per-frame and scale that change by the elapsed time to make it change/sec.
     
    Kokomeant likes this.
  5. Kokomeant

    Kokomeant

    Joined:
    Dec 23, 2022
    Posts:
    13
    I got it, Thank you very much! :)
     
    MelvMay likes this.
  6. oscarAbraham

    oscarAbraham

    Joined:
    Jan 7, 2013
    Posts:
    431
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    You're best to quote the dev above, not me. I know how to use this stuff.

    Also, the dev is not using the Input System package and they are a beginner so that's not something they should jump into immediately.
     
    oscarAbraham likes this.
  8. oscarAbraham

    oscarAbraham

    Joined:
    Jan 7, 2013
    Posts:
    431
    OK. You're right, sorry. I'll tag them: @rafaelgolu2005. Also, it is my personal opinion that the input system is not too hard to get started into; it's not as easy as the old ways, but I think you seem knowledgeable enough to use it if you need it.