- Home /
how to CORRECTLY position and rotate a gameobject in unity
How to correctly position and rotate a gameobject taking into consideration the different methods available?
Can anyone diminish the confusion by giving an explanation of what each of the following functions does and in what context should it be used (best practices, etc.)?
- Transform.position/localPosition
"The position of the transform in world space./Position of the transform relative to the parent transform."
- Transform.rotation/localRotation
"The rotation of the transform in world space stored as a Quaternion./The rotation of the transform relative to the parent transform's rotation."
"Unity stores rotations as Quaternions internally. To rotate an object, use Transform.Rotate. Use Transform.eulerAngles for setting the rotation as euler angles." -what does this mean? when do i use these 3 ways of rotating a gameobject?
- Transform.Rotate
"Applies a rotation of eulerAngles.z degrees around the z axis, eulerAngles.x degrees around the x axis, and eulerAngles.y degrees around the y axis (in that order)."-why would i use Transform.Rotate and not Transform.rotation/localRotation?
- Transform.Translate
"Moves the transform in the direction and distance of translation."-why would i use Transform.Translate and not Transform.position/localPosition?
- Transform.eulerAngles
"Only use this variable to read and set the angles to absolute values. Don't increment them, as it will fail when the angle exceeds 360 degrees."
- Rigidbody.position
"Get and set the position of a Rigidbody using the physics engine."
"Use this if you want to teleport a rigidbody from one position to another, with no intermediate positions being rendered."
"The transform will be updated after the next physics simulation step."
- Rigidbody.rotation
"Get and set the rotation of a Rigidbody using the physics engine."
"Use this if you want to teleport a rigidbody from one rotation to another, with no intermediate positions being rendered."
"The transform will be updated after the next physics simulation step."
- Rigidbody.MovePosition
"Use this if you want to continuously move a rigidbody in each FixedUpdate, which takes interpolation into account."
- Rigidbody.MoveRotation
"Use this if you want to continuously rotate a rigidbody in each FixedUpdate, which takes interpolation into account."
- Rigidbody.AddForce
"Adds a force to the rigidbody."-used when?
- Rigidbody.AddTorque
"Adds a torque to the rigidbody."-used when?
The only big difference i know between these is that you want to use Rigidbody functions for physics game objects, that are not kinematic. other than that, they seem to achieve the same result.
Other comments about this subject are appreciated.
Links
http://docs.unity3d.com/ScriptReference/Transform.html
http://docs.unity3d.com/ScriptReference/Rigidbody.html
Answer by Hellium · Jun 22, 2015 at 11:27 AM
I think that the documentation is quite clear about this :
Transform.position :
Places your object in a specific point in the world space (regardless of the position of its parent). Places the object instantly, regardless to its previous position => Absolute position in world space.
Transform.localPosition :
Places your object in a specific point considering the position of its parent (the position of the parent is the origin of the "sub-space" of your object. Places the object instantly, regardless to its previous position => Absolute position in parent space.
Transform.rotation:
Set the rotation of your object in a specific way (set by Quaternion) in the world space (regardless of the rotation of its parent). Set the rotation of the object instantly, regardless to its previous rotation => Absolute rotation in world space.
Transform.localRotation :
Set the rotation of your object in a specific way (set by Quaternion) considering the rotation of its parent (the rotation of the parent is (0, 0, 0, 0) for you object) => Absolute rotation in parent space. Set the rotation of the object instantly, regardless to its previous rotation.
Transform.eulerAngles
Same as Transform.rotation but with euler angles (pitch, yaw, roll) to be easier to manipulate for humans.
Transform.Translate
Makes your object move considering its previous position (relative) and in its own space (unless you specify the
relativeTo
argument to Space.World). Use it if you do not know the position of if the final position doesn't matter => Relative position in self / world space.Transform.Rotate
Makes your object turn considering its previous rotation (relative) and in its own space (unless you specify the
relativeTo
argument to Space.World). Use it if you do not know the rotation of your object or if the final rotation doesn't matter (you just want you object to turn) => Relative position in self / world space.
Rigidbody functions are based on physics, adding force to an object will "propel" your object in a direction for instance. As if you shoot in a soccer ball with your foot. You don't have to calculate the position of your ball each FixedUpdate, the physics engine will do it for you.
so AddForce/AddTorque is not meant to be used when wanting to position/rotate a rigidbody from point A to a fixed point B?
Indeed !
I have never used the AddTorque function though.
I think that the other functions of the Rigidbody class are used if you have custom physics laws and you want to calculate all by yourslef.
You must also know that moving a transform will also move its rigidbody.
I see what you mean, but unfortunately, the answer of tis question is out of my knowledge. I think you should not bother yourself with this question and use the way your prefere ! ;)
I have realised something @C___ about using Translate ins$$anonymous$$d of the position/localPosition. I have made a mistake in my answer, I will correct it now :
When you use Translate ins$$anonymous$$d of position/local position, you are manipulating the object in its OWN space (unless you specify the relativeTo
argument to Space.World)