Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Conversion to Translate

Discussion in 'Physics for ECS' started by shotoutgames, Jul 5, 2020.

  1. shotoutgames

    shotoutgames

    Joined:
    Dec 29, 2013
    Posts:
    290
    I was under the impression that the following code that applies an impulse would change the translation component of the entity.
    My scenario has a game object player character that converts to entity but keeps game object.
    After the force is applied the velocity changes in the entity debugger but the translation (or rotation) entity component doesn't change. When working I would then have the game object follow the entity as the translate changes.
    Thanks!

    Code (CSharp):
    1. public class ApplyImpulseSystem : SystemBase
    2. {
    3.  
    4.     protected override void OnUpdate()
    5.     {
    6.         /// Get the physics world.
    7.  
    8.         Entities.WithoutBurst().ForEach(
    9.             (
    10.  
    11.                 Entity _entity,
    12.                 ref PhysicsVelocity _physicsVelocity,
    13.                 in PhysicsMass _physicsMass,
    14.                 in ApplyImpulseComponent _applyImpulseData
    15.                 ) =>
    16.             {
    17.                 // Apply a linear impulse to the entity.
    18.                 ComponentExtensions.ApplyLinearImpulse(ref _physicsVelocity, _physicsMass,
    19.                  _applyImpulseData.Direction * _applyImpulseData.Force);
    20.  
    21.                    
    22.  
    23.                 //ComponentExtensions.ApplyImpulse(ref _physicsVelocity, _physicsMass, translation, rotation, _applyImpulseData.Force * _applyImpulseData.Direction, translation.Value);
    24.  
    25.             }).Run();
    26.     }
    27. }
    28.  
     
  2. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    461
    You need to schedule your system at the appropriate time, otherwise another system could overwrite the results.

    Try adding UpdateAfter(typeof(ExportPhysicsSystem)) and UpdateBefore(typeof(EndFramePhysicsSystem)).
     
  3. Sima_Havok

    Sima_Havok

    Joined:
    Dec 16, 2019
    Posts:
    52
    Also after proper scheduling you will need to copy entity data back to game object