- Home /
Kinematic rigidbody movement.
Hello everyone,
I have this scenario. I have a simple prefab which has the following components:
RigidBody2D - marked as kinematic.
CapsuleCollider2D.
PerceptionComponent - a custom component that uses Physics2D.CircleOverlapAll to detect other instances of the prefab in question.
MovementComponent - a custom component that contains the movement logic.
I read a lot about kinematic rigid bodies and I'm confused. My understanding is that every moving object in my game should either be a dynamic rigid body or kinematic rigid body. This way the objects are registered in the "Physics world" by the Physics engine. Static rigid bodies are optimized by the physics engine.
In my case, I don't want my prefab to collide with anything. I'm using its collider only in the perception component to do a circle sweep in a given radius.
What is the PROPER way to move the prefab (kinematic rigid body)?
Should I manipulate transform.position/rotation directly?
Should I use RigidBody2D.MovePosition/MoveRotation?
Should I manipulate the rigid body's velocity?
Should I use FixedUpdate or Update in my case?
The documentation was ambiguous (to me) when it came down to kinematic bodies. I'm looking for the best way to move a kinematic body. What are the differences between the different approaches in terms of the Physics Engine?
You can move it with the two ways, my personal preference for kinematic bodies (like space shooter games, for example) is moving it using the rigidbody itself, first getting the input value and changing the velocity using a new Vector, the movement looks more smooth to me, and of course, when dealing with Physics components, using FixedUpdate.
Hello, thanks for the reply. I got the best results when I used transform.position directly, but the question is different. I need to know the details behind the different approaches.
transform.position
rigidbody.position
rigidbody.$$anonymous$$ovePosition
It's important because they are all different to the Physics Engine and without knowing what's going on we might suffer from performance issues.
Answer by Reid_Taylor · Oct 28, 2020 at 06:25 PM
I believe you should manipulate transform.position/rotation. Because you shouldn't be able to use Rigidbody2D.MovePosition/Rotation when a rigid body is kinematic. Same thing with changing velocity. And When ever dealing with physics you should use fixed update although you could use update if you use transform.position/rotation. Hopefully this answers your question. Anyone feel free to correct me.
Hello, thanks for the reply. The documentation for RigidBody2D says that $$anonymous$$ovePosition is intended to be used with kinematic rigidbodies (check the note near the bottom):
https://docs.unity3d.com/ScriptReference/Rigidbody2D.$$anonymous$$ovePosition.html