Search Unity

Question Why is this spherecast not working?

Discussion in 'Physics for ECS' started by AbrakaDabraSutra, Mar 12, 2023.

  1. AbrakaDabraSutra

    AbrakaDabraSutra

    Joined:
    Sep 3, 2021
    Posts:
    6
    Hi, this is probably a wrong way how to make a spherecast. But as I am a beginner to ECS I wonder why is it simply not working?

    I have a simple box in the subscene that I expect to be hit by the sphere cast (ObjectToBeHitBySphere.png)

    I have a simple Game Object in the subscene that from which I want to cast the sphere (CasterObject.png).
    It has an aspect with a following code.

    Code (CSharp):
    1. public readonly partial struct ColliderCasterAspect : IAspect
    2. {
    3.     private readonly Entity entity;
    4.     private readonly TransformAspect transformAspect;
    5.     private readonly ColliderAspect colliderAspect;
    6.     private readonly RefRO<CasterTagComponent> casterTagComponent;
    7.  
    8.    
    9.     public void DrawCapsuleRayCast()
    10.     {
    11.         float3 origin = transformAspect.WorldPosition;
    12.         float3 direction = transformAspect.Forward;
    13.         Debug.DrawLine(origin, origin + 100f * direction, Color.white);
    14.        
    15.      
    16.  
    17.         if (colliderAspect.SphereCast(origin, 1f, direction, 50f, out ColliderCastHit hit, colliderAspect.GetCollisionFilter()))
    18.         {
    19.             Debug.DrawLine(origin, hit.Position, Color.red);
    20.         }
    21.     }
    22. }

    The system looks like this:
    Code (CSharp):
    1. public void OnUpdate(ref SystemState state)
    2.     {
    3.         new RaycastJob().ScheduleParallel();
    4.     }
    5.  
    6.  
    7.    
    8.     public  partial struct RaycastJob : IJobEntity
    9.     {
    10.          
    11.         public void Execute(ColliderCasterAspect colliderCasterAspect)
    12.         {
    13.             colliderCasterAspect.DrawCapsuleRayCast();
    14.         }
    15.     }
    Only the white line is drawn. Why?

    Thank you for ur answer.
     

    Attached Files: