Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

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

Discussion in 'Physics for ECS' started by jazzbach, Feb 8, 2022.

  1. jazzbach

    jazzbach

    Joined:
    May 11, 2016
    Posts:
    30
    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:

    Code (CSharp):
    1. private void Update()
    2. {
    3.         // Go: GameObject
    4.         _entityManager.SetComponentData(entity, new Translation { Value = Go.position });
    5.         _entityManager.SetComponentData(entity, new Rotation { Value = Go.rotation });
    6.  
    7. }

    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 !
     
  2. DevViktoria

    DevViktoria

    Joined:
    Apr 6, 2021
    Posts:
    94
    Hi,
    you can check the documentation here: https://docs.unity3d.com/Packages/c...i/Unity.Physics.Authoring.BodyMotionType.html. 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.
     
  3. jazzbach

    jazzbach

    Joined:
    May 11, 2016
    Posts:
    30
    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)
     
    DevViktoria likes this.