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

How to detect if the player is grounded

Discussion in 'Physics for ECS' started by eneIr, Mar 18, 2020.

  1. eneIr

    eneIr

    Joined:
    Nov 28, 2018
    Posts:
    15
    This is my script, it's look not so bad but when I enter playmode and check the my player in the entity debugger, the grounded field in the PlayerMovementData component is not check. Can anyone tell me what's wrong with my script. Thanks in advance!
    PlayerMovementData:
    Code (CSharp):
    1. using Unity.Entities;
    2. using UnityEngine;
    3.  
    4. [GenerateAuthoringComponent]
    5. public struct PlayerMovementData : IComponentData
    6. {
    7.     [HideInInspector]
    8.     public bool grounded;
    9. }
    PlayerGroundedSystem:
    Code (CSharp):
    1. private struct PlayerGroundedJob : ICollisionEventsJob
    2.     {    
    3.         [ReadOnly] public ComponentDataFromEntity<GroundTag> ground;
    4.  
    5.         public ComponentDataFromEntity<PlayerMovementData> movementData;
    6.         public void Execute(CollisionEvent collisionEvent)
    7.         {
    8.             Entity entityA = collisionEvent.Entities.EntityA;
    9.             Entity entityB = collisionEvent.Entities.EntityB;
    10.  
    11.             if (ground.HasComponent(entityA) && movementData.HasComponent(entityB))
    12.             {
    13.                 PlayerMovementData move = movementData[entityB];
    14.                 move.grounded = true;
    15.                 movementData[entityB] = move;
    16.             }
    17.             else if (ground.HasComponent(entityB) && movementData.HasComponent(entityA))
    18.             {
    19.                 PlayerMovementData move = movementData[entityA];
    20.                 move.grounded = true;
    21.                 movementData[entityA] = move;
    22.             }
    23. }