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
so AddForce/AddTorque is not meant to be used when wanting to position/rotate a rigidbody from point A to a fixed point B?
– C_1Indeed ! 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.
– Helliumthanks for the answer. much clearer now.
– C_1though i have to add that you can still use Transform.position/localPosition instead of Transform.Rotate any time having something like transform.position += new Vector3(speed * Time.deltaTime, 0.0f, 0.0f); same for the Transform.rotation/localRotation so, in this case also, it moves the object considering its previous position and you do not need to know its current position, nor its final position
– C_1I 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 ! ;)
– Hellium