- Home /
transform.Translate vs rigidbody.MovePosition?
Okay, here's a REAL noob question for you...
Which of the above should I be using for my race car? It's a VERY basic retro -styled game and I have it working perfectly with transform.Translate/rotate but the collision seems a little odd. I read somewhere that if you want the objects to collide, you should use rigidbody.MovePosition? Is this true? And if so... why is this? Also, I want my game to be able to run on low end hardware... which of the two methods produces the least CPU overheads?
Unfortunately, the Unity scripting reference just didn't answer my questions...
Cheers!
Answer by dannyskim · Feb 08, 2012 at 08:10 PM
The reason why it is recommended to use MovePosition is because when the rigid body is NOT kinematic, an extra step is taken into account when calculating how the rigid body reacts to physics. Since the transform has nothing to do with physics, it is just the coordinate information of the NON physics based components of the Game Object, this causes variation between the two bodies.
Now MovePosition takes this into effect and more properly handles your transform information along with your rigid body information.
When you think about it, when translating the transform and not the rigid body itself in the case of NON-kinematic bodies, the rigid body information is thus calculated at the end of the physics step frame, causing a slight difference in the two positions of each body. It's pretty much the same thing if you translate the position of the rigid body using
rigidbody.position
or anything of the like. MovePosition was made specifically to deal with this oscillation of the two bodies in conjunction with active physics. CPU overhead is negligible in this case, since MovePosition was made specifically for this purpose.
Ah, okay, thanks! So in my case, I can probably get away with transform.Translate, as I don't need physics, so much, just need to change speed when objects touch, as it is a 2.5D game, I am using planes with sprites mapped to them, but have cubes as the collision zones...
Am I right in that thinking?
Well the slight difference in calculation between the two may provide a chance for the Unity Engine to report inaccurate collisions when recalculating the rigid body's position and collider information. If accurate collisions is your concern, then I would suggest using $$anonymous$$ovePosition as recommended.
Yeah, you're right. I'm finding that with transform.Translate, I can make the trees adjust my car's speed when there is a crash, but then it keeps driving through them. I'm changing the control script now. Thanks for your help!
Answer by neuropanic · Feb 13, 2012 at 06:35 PM
Sorry guys but isn't rigidbody.MovePosition meant to be used on kinematic rigid bodies? If I well understood the PhysX philosophy, the only way you should move a rigid body is to apply a force.
I think you're right, but I've never used $$anonymous$$ovePosition before so there may very well be applications for it outside of kinematics, like if you want something to move but don't want it to be affected by momentum.
Interesting.. I will try to test the difference! If it's really the case will be really useful for me. Thanks :)
Should set force for RBs. And setting velocity directly can handle almost anything, such as momentum & faking friction.
$$anonymous$$ovePosition seems to be for special cases where you know where you want it be be. $$anonymous$$aybe following a motion curve (and blasting things out of the way.) You can set velocity and aim yourself along the tangent, but $$anonymous$$ovePosition is better.
Cool, I'll have a play. I'm still using transform.Translate at the mo, as I found a workaround, but I might try out addforce.
Actually, it looks like it should be affecting object along the way. Quote from the UnityDoc page
Objects lying along the path will be pushed aside if necessary. This often gives a more acceptable result than changing transform.position
Answer by neuropanic · Feb 13, 2012 at 11:00 PM
Hi Owen, so you already used MovePosition? No issues with complex hierarchy?
I just tested $$anonymous$$ovPos some and decided velocity
was better. $$anonymous$$ovPos is glitchy if you stay in contact. Tried these lines, with some child platforms and an unattached RB cube resting on one:
//rigidbody.$$anonymous$$ovePosition(SS+Vector3.up*Time.time);
rigidbody.velocity=Vector3.up;
Both move up at a speed of 1$$anonymous$$/S, but $$anonymous$$ovePos makes the cube on top bobble and eventually fall through. Even resting on the parent, I got some bobbling.
Say @OwenReynolds - if you see this ... now that two years have gone by. I have a trivial 2D scene, all objects are Rigidbody2D-$$anonymous$$inematic with CircleCollider2D. No gravity, forces or bouncing, are used at all; I just move the objects in straight lines. I wish to know only when there's a collision between two of 'em
I have tested just ordinary old "Translate" every Update, versus, using this newfangled $$anonymous$$ovePosition.
(Using .velocity is not ideal as the items sometimes don't move in straight lines, they flourish around you know.)
i really can't see any difference at all between Update-Translate and newfangled $$anonymous$$ovePosition .....although, I don't do many 2D projects.
Now that a couple yrs have passed do you a have a handle on this? Which is preferable ... is there kind of a standard approach, or?
Thanks if you know!!