Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Object doesn't go in desired direction after being flipped over.

Discussion in 'Physics' started by bebros2, Dec 7, 2022.

  1. bebros2

    bebros2

    Joined:
    Aug 28, 2021
    Posts:
    36
    I have an object (cube) that the player can walk up to and push. The problem is that whenever the object gets flipped on its side or just flipped upside down the player can't push it anymore.

    I'm using this to move a cube.
    Code (CSharp):
    1.         moveDir = Player.transform.forward * verticalInput;
    2.  
    3.         rb.AddForce(moveDir.normalized * pushSpeed * 10, ForceMode.Force);
    Instead of AddForce I've also tried

    Code (CSharp):
    1.         rb.velocity = moveDir * pushSpeed * 10;
    Shouldn't this make it so that no matter how its flipped it should addforce in the direction that the player is looking? If not, then how do I make it move in the direction the player looks no matter what way the cube is flipped.
     
  2. Unknown_Programmers

    Unknown_Programmers

    Joined:
    May 30, 2020
    Posts:
    3
    the player.transform.forward points at the forward vector of the cube. Hence if the object gets flipped over ,the objects forward direction also gets flipped over.

    (You can check this by selecting the object in the editor [make sure you have the handle rotation set to local else the handle will not change depending on rotation ] and you can see the handle you use to move the object . The blue color arrow points at the forward direction )

    So instead of using the forward vector of the cube you can use the world space forward vector: Vector3.forward ,it's value is (0,0,1) always ,so it doesn't get changed by the rotation of any objects.
     
  3. bebros2

    bebros2

    Joined:
    Aug 28, 2021
    Posts:
    36
    Just to clarify I want the cube to move in the direction the player's y value is pointing towards. Not exactly where the camera is looking at. The player never rotates so its forward direction always points forwards and never changes with the cube's rotation.

    Also, where exactly should I plug in Vector3.forward? I swapped it with Player.transform.forward and like expected, it only goes in one direction. Not where the player is looking. Another thing I tried, just to see if it would work, was converting the Player.transform.position into world position but it gives the same results. Also, if the object is flipped over when trying either of these it still doesn't move. Any ideas on why this happens?