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 Issues with Update and FixedUpdate with Rigidbody2D Velocity

Discussion in '2D' started by nandanv123, Aug 6, 2020.

  1. nandanv123

    nandanv123

    Joined:
    Mar 5, 2017
    Posts:
    4
    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:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CharacterController : MonoBehaviour
    6. {
    7.  
    8.     public Rigidbody2D rb;
    9.     public float moveSpeed;
    10.     public Vector2 inputVector;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         rb = GetComponent<Rigidbody2D>();
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         HorizontalInput();
    22.     }
    23.  
    24.     private void FixedUpdate()
    25.     {
    26.         Movement();
    27.     }
    28.  
    29.     public void HorizontalInput()
    30.     {
    31.         inputVector.x = Input.GetAxisRaw("Horizontal");
    32.         inputVector.y = 0;
    33.     }
    34.  
    35.     public void Movement()
    36.     {
    37.         Debug.Log(inputVector);
    38.         if (inputVector.x > 0)
    39.         {
    40.             Debug.Log(inputVector);
    41.             rb.velocity = new Vector2(moveSpeed, rb.velocity.y);
    42.             Debug.Log("rb velocity" + rb.velocity);
    43.         } else if(inputVector.x < 0)
    44.         {
    45.             Debug.Log(inputVector);
    46.             rb.velocity = new Vector2(-moveSpeed, rb.velocity.y);
    47.             Debug.Log("rb velocity" + rb.velocity);
    48.         }
    49.         //else
    50.         //{
    51.         //    Debug.Log(inputVector);
    52.         //    rb.velocity = new Vector2(0, rb.velocity.y);
    53.         //}
    54.     }
    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?
     
  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    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.
    Code (CSharp):
    1. public void Movement()
    2. {
    3.     rb.velocity = new Vector2(inputVector.x * moveSpeed, rb.velocity.y);
    4. }
    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.
     
    nandanv123 likes this.
  3. nandanv123

    nandanv123

    Joined:
    Mar 5, 2017
    Posts:
    4
    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.
     
  4. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,282
    Can you show a screenshot of the characters inspector? Make sure rigidbody is not kinematic
     
    nandanv123 likes this.
  5. nandanv123

    nandanv123

    Joined:
    Mar 5, 2017
    Posts:
    4
    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.
     
  6. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    What version of Unity are you using? Edit nvm. Just read your post again. Try upgrading to 2019.4.
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,529
    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.
     
  8. GrizzlyPunchGames

    GrizzlyPunchGames

    Joined:
    May 21, 2020
    Posts:
    11
    Hi, I have made tutorial video with player movement,
    Maybe this will help you: