a question : do i have to add rigidbody2D to an object that i'm moving through code ?

hi there
i have a little question that i’m hoping to find and answer here , i read somewhere that i have to attach RB2D to any moving object , not doing this will impact performance of my app, in my case i have some sprites that i’m moving using leantween , it works perfectly without RB2D ,
so what do you guys think about this ?
thank you

anyone please ?

Rigidbodies will tend to look better, because they interpolate/extrapolate motion. Moving a sprite every update will give it a more “juddery” look. I haven’t used leantween, but for anything that moves in the gameworld I use rigidbodies, unless it’s for UI.
Try using them, and if they don’t look as good then just do it your way.

okey , thanx a lot Scabbage for sharing your thoughts

If you don’t need force interaction, then you won’t need a rigidbody. Collision detection for 2D can easily be solved with raycasts, unless you need it to be pixel perfect in which case the amount of raycasts needed would produce a significant overhead. Simple AABB tests works too, although, you will get inaccurate results if you rotate the object.

If you want to use the OnCollision/Trigger2DEnter/stay/exit callbacks, then atleast one non-kinematic rigidbody has to be present in the event of intersection between collider geometries.

If you just want to move an object, doing so without a rigidbody is perfectly fine and shouldn’t at all produce a ‘juddery’ look unless maybe, you move it in FixedUpdate, which is to be used for physics simulations and possibly to sync camera movement with whatever physics object its following. Raycasts should also be done in FixedUpdate.

@EnokV thanx a lot for the explanation ,i’m following the the right track based on what you said above , what made me consider using rigid body 2D for moving abjects that doesn’t need physics forces is an article that i have read, more specifically this section of the article

-Objects with a collider but no RigidBody are considered static.
Moving these is expensive, so if you’re creating them with code, add the collider and physics material (to the collider) after positioning.

You can select if an object should be static or not in the inspector. Also, that site has one or more malicious ads.

okey man , simple as it is , that’s what i thought too

This is true, if an object has a collider but no attached RigidBody it will be consider as static and if you do move it, it will have to rebake a collision mesh for every frame that it is moving. You can see this expensive call pop up in the Profiler.
Simply add a Kinematic Rigidbody to the somewhere on the object, ideally the root (ABOVE the colliders in the hierarchy).

Yes, my bad - I looked it up now. I got confused with static game objects.

However, I performed some tests just now using @Iron-Warrior 's SCC and a collider attached. Slapping a kinematic rigidbody to it does absolutely nothing in terms of performance what I can see, no matter how I move the character - be it transform.translate/position or rigidbody.position.

I’m not quite sure what to look for either, to be fair.

Either way, considering PxGeometryQuery::computePenetration / computeMeshPenetration is to be exposed, it would make sense to be able to mark colliders as dynamic in the inspector, without the need of a rigidbody at all.

I’m gonna bring that up in the other thread We need a way to detect and resolve collision hits manually - Unity Engine - Unity Discussions

It depends how complex your Colliders are. If there’s just a handful that are being moved, you likely won’t notice it.
For example, in a previous game, buttons were Collider-based and were moved around frequently via Animation. I only noticed the performance hit when we started to get busy scenes with lots of buttons moving at once, but it all adds up.

FYI, I believe it shows up as Collider.Move (Expensive Delayed Cost) in the profiler.

Hm, I see. Well I made a post about it in the thread I linked, it would be incredibly helpful if you could just treat colliders as geometry to check against other geometry using the suggested method(s) without having to slap on a kinematic rigidbody for the sake of optimization :S.

I agree there could probably just be a check box on the Collider, but you’re not adding a RigidBody to optimise, you’re adding a RigidBody to tell the Physics engine that this object will move at some point, therefore don’t try and optimise it into a static structure.

See the latest post by HiddenMonk in the thread I linked above. “In Unity 5, we’ll use the same data structure to handle the movement of both dynamic and static colliders.”, more in the link he provided.

so the guy working in unity just replied on the thread you linked above @EnokV , seams like attaching a kinematic RB to a moving gameobject with collision is advisable after all
thanx @Gizmoi for sharing your thoughts regarding this