Howto: Kinematic ECS Entity having same pos and rot of a GameObject and perform physics correctly

Hello !!

I have both, a plain old GameObject (a VR hand) and an instantiated Entity on the scene (a baseball bat entity with PhysicsBody to “Kinematic” and a PhysicsShape).

I have the Entity to always match the position and rotation of the GameObject inside an Update method. However, when swinging the VR hand too fast and hitting a ball (another Entity), physics work a bit weird as the ball doesn’t go flying towards my swing direction but does some erratic movements (as if the collision detection was discrete). If I swing the bat slowly however, the physics seem to work just fine.

I’m doing the position update as follows:

private void Update() 
{
        // Go: GameObject
        _entityManager.SetComponentData(entity, new Translation { Value = Go.position });
        _entityManager.SetComponentData(entity, new Rotation { Value = Go.rotation });

}

I know that, for GameObjects using a RigidBody, I shouldn’t change the transform.position values directly as the physics won’t work as expected. I’m guessing this is the same for Entities (discrete vs continuous collision detection). However, do you know of a way for an Entity to match a GameObject position and rotation every frame and have physics work as expected ?

Thanks a lot for your help beforehand !

Hi,
you can check the documentation here: Enum BodyMotionType | Unity Physics | 0.6.0-preview.3. I think even though you set your entity for kinematic motion, you still directly modifying the translation and rotation values. I guess it behaves as if it were teleporting. Try to figure out a code that is modifying the PhysicsVelocity component of the entity based upon the difference in position and rotation. That might produce better results.

Thanks a lot “DevViktoria” for your answer.

I’ll try what you are suggesting for sure. I kind of have an idea of how to do it but will need to get the deltas for those values (position and rotation)

1 Like