2D Endless game - Best method for moving objects?
I am developing 2D endless game, and I would like to know which method is the best for moving objects? Is it rigidbody2d.velocity or transform.position? I know that there is written that we should use rigidbody on all physics objects, yea, I know that, but I have rigidbody only on my main character, cuz only on this one I use gravity. Other enemies have colliders and I can move them by transform.position. I have tested both methods and think that my method for moving objects by transform.position is more smooth. So which one is better? My method or should I attach also rigidbody2d to other moving objects and use velocity for movement?
Answer by MelvMay · Mar 25, 2017 at 11:32 AM
O$$anonymous$$G, you think that I haven't done any research? Please read my question again. Thanks.
Well your question doesn't make sense to me.
Not adding a Rigidbody(2D) just means the collider gets implicitly and automatically added to a STATIC rigidbody behind the scenes called the ground-body. Static means you don't intend to move it so yes, if it's moving, add a Rigidbody. Rigidbody2D allows you to explicitly state the body type as well so if you're moving it then don't select a Static body type. Note that moving something under velocity means it moves through space. Setting the Transform position/rotation just means it instantly gets repositioned to the new pose without moving through the interviening space. The difference being that you don't get to use Rigidbody interpolation and that you're constantly forcing overlaps as you moving overlpped into other colliders. Using the velocity of a Rigidbody means you can use stuff like CCD which stops you tunelling through other colliders. You use the $$anonymous$$inematic body type if you want to expliclty control the movement of the body and you do not want it having any collision response. It also isn't affected by gravity but neither is a Dynamic body with its gravity scale set to zero. You use a Dynamic body if you want it to respond to collision forces. You talk about smooth and say moving via Transform.position is "smoother" but that must be subjective because it is in no way smoother than a body moving under its own velocity that uses linear interpolation per-frame. In the end, the choices of static, kinematic or dynamic are yours but the highlights are: static = you don't ever move it kinematic = you move it but try to move it by specifying body.position/body.rotation and not set the transform directly. The body colliders will not have any collision response. dynamic = you do not ever move it directly via transform or body.position/body.rotation but let it move via its own velocity. You can change the velocity directly or apply forces which do that indirectly. Gravity is also available here, scaled using the Rigidbody2D.gravityScale.I know that there is written that we should use rigidbody on all physics objects
Thank you for your answer. I didn't know that if I create game object with collider and without rigidbody, that it automatically adds static rigidbody behind the scenes. So according to your explanation, if I have kinematic ridigbody, I should use rigidbody.$$anonymous$$ovePosition and if I have dynamic rigidbody, I should use rigidbody.velocity or rigidbody.AddForce, right? Also all these functions ($$anonymous$$ovePosition, velocity, AddForce) should be used in FixedUpdate and not in Update, yes? Could you also tell me when do you recommend to use Interpolation? I have found in doc, that it should be only used on my main character and not used on anything else, so should I stick to this rule?