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

dealing with non-custom components

Discussion in 'Entity Component System' started by Garzec, Oct 19, 2019.

  1. Garzec

    Garzec

    Joined:
    Mar 3, 2016
    Posts:
    151
    Hi there,
    I would like to start with a simple 2D top down movement. I created two components for the input and the movementspeed

    Code (CSharp):
    1. public struct MovementInputComponent : IComponentData
    2. {
    3.     public float X;
    4.     public float Y;
    5. }
    6.  
    Code (CSharp):
    1. public struct MovementSpeedComponent : IComponentData
    2. {
    3.     public float Value;
    4. }
    5.  
    and when converting the player to an entity I want to add those components to him.

    Code (CSharp):
    1. public class MovementBehaviour : MonoBehaviour, IConvertGameObjectToEntity
    2. {
    3.     [SerializeField]
    4.     private float movementSpeed;
    5.  
    6.     public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    7.     {
    8.         dstManager.AddComponentData(entity, new MovementInputComponent());
    9.         dstManager.AddComponentData(entity, new MovementSpeedComponent { Value = movementSpeed });
    10.     }
    11. }
    Lastly I want to move entities having those components attached.

    Code (CSharp):
    1. public class MovementSystem : JobComponentSystem
    2. {
    3.     [BurstCompile]
    4.     struct MovementJob : IJobForEach<MovementInputComponent, MovementSpeedComponent>
    5.     {
    6.         public float fixedDeltaTime;
    7.  
    8.         public void Execute(ref MovementInputComponent movementInput, ref MovementSpeedComponent movementSpeed)
    9.         {
    10.             // rb.MovePosition(rb.position + movementInput + movementSpeed * fixedDeltaTime);
    11.         }
    12.     }
    13.  
    14.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    15.     {
    16.         MovementJob job = new MovementJob { fixedDeltaTime = Time.fixedDeltaTime };
    17.         return job.Schedule(this, inputDeps);
    18.     }
    19. }
    20.  
    As you can see the rigidbody component is missing yet.

    Should I attach it to my GameObject first?
    How do I convert it to a component in the MovementBehaviour script that I can access it later on in the MovementSystem script?

    Thanks in advance :)
     
  2. Garzec

    Garzec

    Joined:
    Mar 3, 2016
    Posts:
    151
    It seems dealing with physics components is not possible yet ...
     
  3. RecursiveEclipse

    RecursiveEclipse

    Joined:
    Sep 6, 2018
    Posts:
    298
    Why not? Dots/Havok Physics exists now as PhysicsBody/PhysicsShape monobehaviours. They will be converted automatically into ECS components like Transform/Renderer. It may not be clear what exactly you're looking/asking for.
     
  4. Garzec

    Garzec

    Joined:
    Mar 3, 2016
    Posts:
    151
    @RecursiveEclipse thanks, didn't know that. So the Rigidbody2D gets converted to PhysicsBody when having the Unity Physics package installed?
     
  5. paul-figiel

    paul-figiel

    Joined:
    Feb 9, 2017
    Posts:
    9
    There is no 2D physics in ECS for now, only 3D. But you can create your own conversion workflow that converts your 2D rigidbody to unity ecs physics in 3D.
     
  6. Garzec

    Garzec

    Joined:
    Mar 3, 2016
    Posts:
    151
    @paul-figiel so if I got you correctly everything for 3D is fine now? :) Except Sounds, Animations etc. for DOTS.
     
  7. Garzec

    Garzec

    Joined:
    Mar 3, 2016
    Posts:
    151
    Hey guys,
    I updated my movement job to

    Code (CSharp):
    1.     [BurstCompile]
    2.     struct MovementJob : IJobForEach<MovementInputComponent, MovementSpeedComponent, PhysicsBodyAuthoring>
    3.     {
    4.         public float fixedDeltaTime;
    5.  
    6.         public void Execute(ref MovementInputComponent movementInput, ref MovementSpeedComponent movementSpeed, ref PhysicsBodyAuthoring physicsBody)
    7.         {
    8.             // rb.velocity = movementInput * movementSpeed * fixedDeltaTime
    9.         }
    10.     }
    but get this error

    PhysicsBody is obsolete and was renamed. So it seems I didn't understand you correctly..
     
  8. Ziboo

    Ziboo

    Joined:
    Aug 30, 2011
    Posts:
    356
    You need to fetch the PhysicsVelocity Component.
    PhysicBody and PhysicShape get converted into PhysicsMass, PhysicsVelocity, PhysicsCollider, ...
     
  9. Garzec

    Garzec

    Joined:
    Mar 3, 2016
    Posts:
    151
    Thanks @Ziboo
    I think this looks better now :)

    Code (CSharp):
    1.     [BurstCompile]
    2.     struct MovementJob : IJobForEach<HorizontalMovementInputComponent, MovementSpeedComponent, PhysicsVelocity>
    3.     {
    4.         public float fixedDeltaTime;
    5.  
    6.         public void Execute(ref HorizontalMovementInputComponent horizontalMovementInput, ref MovementSpeedComponent movementSpeed, ref PhysicsVelocity velocity)
    7.         {
    8.             float3 movement = new float3 { x = horizontalMovementInput.X, y = 0, z = horizontalMovementInput.Y };
    9.             velocity.Linear = movement * movementSpeed.Value * fixedDeltaTime;
    10.         }
    11.     }
    When I pass in 0.5f for the input axis and 3 for movementspeed the player is still not moving. Is velocity the wrong approach now? Should I use the Translation component?
     
  10. thelebaron

    thelebaron

    Joined:
    Jun 2, 2013
    Posts:
    851
    What does your entity debugger show for the system? How are you setting up your presumable player?
     
  11. Garzec

    Garzec

    Joined:
    Mar 3, 2016
    Posts:
    151
    @thelebaron sorry for the late response...
    I attached a Rigidbody (not 2D) to a basic cube with a collider. I have these components

    Code (CSharp):
    1. public struct HorizontalMovementInputComponent : IComponentData
    2. {
    3.     public float X;
    4.     public float Y;
    5. }
    6.  
    7. public struct MovementSpeedComponent : IComponentData
    8. {
    9.     public float Value;
    10. }
    This conversion

    Code (CSharp):
    1. public class MovementBehaviour : MonoBehaviour, IConvertGameObjectToEntity
    2. {
    3.     [SerializeField]
    4.     private float movementSpeed;
    5.  
    6.     public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    7.     {
    8.         dstManager.AddComponentData(entity, new HorizontalMovementInputComponent());
    9.         dstManager.AddComponentData(entity, new MovementSpeedComponent { Value = movementSpeed });
    10.     }
    11. }
    And these systems

    Code (CSharp):
    1. public class MovementSystem : JobComponentSystem
    2. {
    3.     [BurstCompile]
    4.     struct MovementJob : IJobForEach<HorizontalMovementInputComponent, MovementSpeedComponent, PhysicsVelocity>
    5.     {
    6.         public float fixedDeltaTime;
    7.  
    8.         public void Execute(ref HorizontalMovementInputComponent horizontalMovementInput, ref MovementSpeedComponent movementSpeed, ref PhysicsVelocity velocity)
    9.         {
    10.             float3 movement = new float3 { x = horizontalMovementInput.X, y = 0, z = horizontalMovementInput.Y };
    11.             velocity.Linear = movement * movementSpeed.Value * fixedDeltaTime;
    12.         }
    13.     }
    14.  
    15.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    16.     {
    17.         MovementJob job = new MovementJob { fixedDeltaTime = Time.fixedDeltaTime };
    18.         return job.Schedule(this, inputDeps);
    19.     }
    20. }
    21.  
    22. public class PlayerHorizontalInputSystem : JobComponentSystem
    23. {
    24.     [BurstCompile]
    25.     struct PlayerHorizontalInputJob : IJobForEach<HorizontalMovementInputComponent>
    26.     {
    27.         public void Execute(ref HorizontalMovementInputComponent horizontalMovementInput)
    28.         {
    29.             // TEST
    30.             horizontalMovementInput.X = 1;
    31.             horizontalMovementInput.Y = 1;
    32.         }
    33.     }
    34.  
    35.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    36.     {
    37.         PlayerHorizontalInputJob job = new PlayerHorizontalInputJob();
    38.         return job.Schedule(this, inputDeps);
    39.     }
    40. }
    When running the game this is what the entity debugger shows this



    but I also get these runtime errors...

     
  12. astrogee

    astrogee

    Joined:
    Feb 4, 2018
    Posts:
    16
    @Garzec Since you are setting the velocity as opposed to the position, you should not use fixedDeltaTime at all.