Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Question Player Rigidbody 2D has velocity but doesn't actually move

Discussion in 'Physics' started by amojulien, Dec 19, 2023.

  1. amojulien

    amojulien

    Joined:
    Jul 16, 2022
    Posts:
    1
    I've tried searching for an answer to this and come up with nothing. My player movement was working fine until I made a bunch of changes to my game and went back to check on things, and now my player will not move at all. Animations are working fine, and when I use debug.log it shows the rigidbody velocity I would expect, but the player is stuck in one place, and when I try to move it manually in the inspector while I'm in play mode, it doesn't move, either. My rigidbody is set as Dynamic.

    Here's the relevant code:
    Code (CSharp):
    1.     void Update()
    2.     {
    3.         playerMoving = false;
    4.  
    5.         if (moveDirection.x > 0.5f || moveDirection.x < -0.5f)
    6.         {
    7.             myRigidBody.velocity = new Vector2(moveDirection.x * moveSpeed, 0f);
    8.             playerMoving = true;
    9.             lastMove = new Vector2(moveDirection.x, 0f);
    10.             anim.SetFloat("MoveX", moveDirection.x);
    11.             anim.SetFloat("MoveY", 0f);
    12.         }
    13.  
    14.         if (moveDirection.y > 0.5f || moveDirection.y < -0.5f)
    15.         {
    16.             myRigidBody.velocity = new Vector2(0f, moveDirection.y * moveSpeed);
    17.             playerMoving = true;
    18.             lastMove = new Vector2(0f, moveDirection.y);
    19.             anim.SetFloat("MoveX", 0f);
    20.             anim.SetFloat("MoveY", moveDirection.y);
    21.         }
    22.  
    23.         if (moveDirection.x < 0.5f && moveDirection.x > -0.5f)
    24.         {
    25.             myRigidBody.velocity = new Vector2(0f, myRigidBody.velocity.y);
    26.         }
    27.  
    28.         if (moveDirection.y < 0.5f && moveDirection.y > -0.5f)
    29.         {
    30.             myRigidBody.velocity = new Vector2(myRigidBody.velocity.x, 0f);
    31.         }
    32.  
    33.         anim.SetBool("Moving", playerMoving);
    34.         anim.SetFloat("LastMoveX", lastMove.x);
    35.         anim.SetFloat("LastMoveY", lastMove.y);
    36.         lastMoveVec3 = new Vector3(lastMove.x, lastMove.y, 1f);
    37.     }
     
  2. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,913
    Wild guess: maybe the animator is overriding the player's transform position, so it ignores any changes made by the physics system (or any other system for that matter)?

    Check whether your animation clips set any values for the player's transform.

    This is one of the reasons why using a version control system (git, plastic, svn, etc) comes in handy: you could just go back to a previous version of your project and see what changes caused this issue, problem solved in just a few minutes! ;)
     
  3. Braza

    Braza

    Joined:
    Oct 11, 2013
    Posts:
    136
    Also time scale is worth checking. I think physics does not get simulated at 0.