Search Unity

Question transform.Rotate not working as intended

Discussion in 'Scripting' started by bebros2, Dec 10, 2022.

  1. bebros2

    bebros2

    Joined:
    Aug 28, 2021
    Posts:
    36
    This is probably some simple error or maybe I'm not understanding Vectors correctly. Currently I'm trying to create pushable object that the player can walk up to then grab and rotate or push it. Rotating works fine until the cube is flipped on its side. Here is the code I'm using to rotate it. (This object has a rigidbody if that effects anything)

    Code (CSharp):
    1.             pushDirection = horizontalInput * rotationSpeed * Time.fixedDeltaTime;
    2.             transform.Rotate(Vector3.up, pushDirection);
    I believe the cube is trying to use its local up-vector. But shouldn't Vector3.up be using the world up-vector? I tried to switch Vector3.up with transform.up but it gives the same results. Anyone see what I'm doing wrong?
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
    If you look at the documentation, you can see Transform.Rotate has an overload which tells it to either use local or global space, and the default is local space.
     
    bebros2 likes this.