Search Unity

Feedback RaycastHit: Could we get hit entity position, please?

Discussion in 'Physics for ECS' started by Slarti-42, Feb 1, 2021.

  1. Slarti-42

    Slarti-42

    Joined:
    Jul 1, 2019
    Posts:
    49
    Hi folks,

    as the result is delivering the entityId, is it possible to implement as well the position of that hit entity, please?

    Similiar as:

    Vector3 hitedObjectPosition1 = hit.transform.position;


    Thanks for considering it.
    Slarti
     
  2. Kmsxkuse

    Kmsxkuse

    Joined:
    Feb 15, 2019
    Posts:
    306
    If it returns an entity, you can access the position by using:

    float3 hitObjectPosition1 = ComponentDataFromEntity<Translation>(true)[hit.entity].Value;


    Of course, there is probably better ways to do this but it is one way to get position from entity.

    I also wrote that from memory so the exact method to get the ComponentDataFromEntity might not be the one I remember. Might need a Get- prefix and will only work inside a SystemBase. If you need it in mono, use EntityManager.GetComponentData<>(Entity).
     
    Slarti-42 likes this.
  3. Slarti-42

    Slarti-42

    Joined:
    Jul 1, 2019
    Posts:
    49
    Thanks @Kmsxkuse, this would be working if the hit entity is in the query inside the lambda, but I am querying static objects from a subscene. ;)
     
  4. Slarti-42

    Slarti-42

    Joined:
    Jul 1, 2019
    Posts:
    49
    then ... I just learnt:
    GetComponentDataFromEntity.
    float4 hitObjectPositiontmp = GetComponentDataFromEntity<LocalToWorld>(true)[raycastHit.Entity].Value.c3;

    (I needed the LocalToWorld, as it was not used in my ref query. Translation was used(compiler didn't like that.)
    Cheers to that.
    Thanks again.
    Slarti
     
    milos85miki likes this.