Search Unity

Question Why i cant send a ray like this ?

Discussion in 'Physics for ECS' started by dofuk06, Sep 18, 2022.

  1. dofuk06

    dofuk06

    Joined:
    Jul 25, 2018
    Posts:
    2
    Code (CSharp):
    1.  private BuildPhysicsWorld buildPhysicsWorld;
    2.     private CollisionWorld collisionWorld;
    3.    
    4.     protected override void OnStartRunning()
    5.     {
    6.         buildPhysicsWorld = World.DefaultGameObjectInjectionWorld.GetExistingSystem<BuildPhysicsWorld>();
    7.         collisionWorld = buildPhysicsWorld.PhysicsWorld.CollisionWorld;
    8.     }
    9.    
    10.     protected override void OnUpdate()
    11.     {
    12.         float xValue = Input.GetAxis("Horizontal");
    13.         float zValue = Input.GetAxis("Vertical");
    14.  
    15.         Vector3 direction = new Vector3(xValue, 0, zValue);
    16.         bool isInputAvaliable = direction.magnitude > 0 ? true : false;
    17.        
    18.         Entities.ForEach(( ref Translation translation ,ref MovementComponent movementComponent, ref PhysicsVelocity physicsVelocity, in TimeComponent timeComponent) =>
    19.         {
    20.             RaycastInput raycastInput = new RaycastInput
    21.             {
    22.                 Start = translation.Value,
    23.                 End = translation.Value - new float3(0, -1, 0)
    24.             };
    25.        
    26.             bool isGrounded = IsGrounded(raycastInput);
    27.            
    28.             if (isInputAvaliable && isGrounded)
    29.             {
    30.                 movementComponent.lastDirection = direction;
    31.                 physicsVelocity.Linear = new float3(xValue, 0, zValue) * timeComponent.localTimeScale * movementComponent.groundSpeed;
    32.             }
    33.         }).Run();
    34.     }
    35.  
    36.     public bool IsGrounded(RaycastInput raycastInput)
    37.     {
    38.         Unity.Physics.RaycastHit raycastHit = new Unity.Physics.RaycastHit();
    39.  
    40.         if (collisionWorld.CastRay(raycastInput, out raycastHit))
    41.         {
    42.             return true;
    43.         }
    44.         return false;
    45.     }
    this code isn't working because of Isgrounded function. How should I do that ?
     
  2. TRS6123

    TRS6123

    Joined:
    May 16, 2015
    Posts:
    246
    I'm pretty sure you have to fetch the collision world every update. See if moving
    collisionWorld = buildPhysicsWorld.PhysicsWorld.CollisionWorld;
    to OnUpdate fixes the problem.
     
  3. dofuk06

    dofuk06

    Joined:
    Jul 25, 2018
    Posts:
    2
    It's say Assets\_Scripts\System\MovementSystem.cs(27,9): error DC0004: Entities.ForEach Lambda expression captures a non-value type 'this'. This is only allowed with .WithoutBurst() and .Run()
     
  4. vectorized-runner

    vectorized-runner

    Joined:
    Jan 22, 2018
    Posts:
    398
    Make your IsGrounded method static