Hi all,
Im trying new ways to move objects around, I mean using physics forces and all that is cool in many ways but sometimes I just want to use the xyz I want the object to be posiitoned, so Im trying this like
transform.position = new Vector3(x,y,z);
And works fine, however I notice that this same method works with the rigidbody’s transfor, like:
rb.transform.position = new Vector3(x,y,z);
(rb is the rigidbody component I obtained previously)
Is there any advantage or disadvantage in using either of this? both seem to react to physics and move in the right way so I wonder if its “wrong” to do it in either way or maybe it will trigger errors in future who knows. Thanks a lot beforehand for the help =)
PS Forgot to mention that most of the times I work with an empty gameobject and later add the mesh and rigidbody
They are identical. You are manipulating the same Transform in both cases, just accessing it differently.
1 Like
A bit more detail in case it’s useful:
-
accessing rigidbody.position isn’t the same as transform.position - you may want rb.position because it only changes after FixedUpdate is called, while transform.position can often contain the interpolated result. Same for rotation.
-
a gameobject may only ever have one transform, so it becomes a matter of what is convenient to you to access that transform.
3 Likes
Thanks a lot skalev for clarifying its just different ways to access the same Transform, I will use what seems cleaner in the code then.
Thanks a lot hippocoder for this advice, so as I have tried in my little learning project, using rb.position works better than transform.position as its more accurate if used within the FixedUpdate, so I will stick to the rb.position / rb.rotation methods instead.
Note though, moving a rigidbody by setting its position is essentially just teleporting it, it will not be constrained by collisions, and will not cause forces to act on other bodies directly. What forces or collisions you may perceive is the forces physx applies to objects in order to solve issues where two bodies are penetrating each other.
1 Like
rb.MovePosition and rb.MoveRotation
1 Like
Those are slightly more involved as I understand it, causing direct forces to be applied to bodies in the path of the moving object. They should also provide proper interpolation to the difference between the fixed vs rendered frame time steps. They will not be constrained by collisions though.
1 Like
They’re higher performance than transform too.
1 Like
Thanks for pointing that out ThermalFusion, you are right, in my experiments I notice that these objects moved through teleporting the rigidbody are not affected by forces, again thanks for the help hippocoder you are right about these MovePosition and MoveRotation, it seems these are better method to move like I want, I will try using these but alway being cautious on what I need on each moment =).