#using Collider->GetChild returns non-existent entity

1 messages · Page 1 of 1 (latest)

humble quarry
#

I'm attempting to find the wheel of a vehicle that's colliding with the ground, but using GetChild on the vehicle collider returns a non existent entity, any suggestions?

#
struct WheelCollisionJob : ICollisionEventsJob
{
    [ReadOnly] public ComponentLookup<VehicleComponent> vehicleComponentLookup;

    [ReadOnly] public ComponentLookup<PhysicsCollider> physicsColliderLookup;

    [ReadOnly] public ComponentLookup<WheelComponent> wheelComponentLookup;

    public void Execute(CollisionEvent collisionEvent)
    {
        Entity vehicleEntity = GetVehicleEntity(collisionEvent, out ColliderKey vehicleColliderKey);

        if (vehicleEntity == Entity.Null) return;

        Debug.Log("vehicle Entity");

        if (!physicsColliderLookup.TryGetComponent(vehicleEntity, out PhysicsCollider vehiclePhysicsCollider)) return;

        Debug.Log("vehicle physics collider");

        Entity wheelEntity = GetWheelChildCollidedEntity(vehiclePhysicsCollider, ref vehicleColliderKey);
//stops here
        if (wheelEntity == Entity.Null) return;

        Debug.Log("wheel collision");
    }

    Entity GetWheelChildCollidedEntity(PhysicsCollider vehiclePhysicsCollider, ref ColliderKey vehicleColliderKey)
    {
        unsafe
        {
            if (!vehiclePhysicsCollider.ColliderPtr->GetChild(ref vehicleColliderKey, out ChildCollider vehicleEntityChildCollided)) return Entity.Null;

            if (wheelComponentLookup.HasComponent(vehicleEntityChildCollided.Entity)) return vehicleEntityChildCollided.Entity;

            return Entity.Null;
        }
    }

    private Entity GetVehicleEntity(CollisionEvent collisionEvent, out ColliderKey vehicleColliderKey)
    {
        if (vehicleComponentLookup.HasComponent(collisionEvent.EntityA))
        {
            vehicleColliderKey = collisionEvent.ColliderKeyA;
            return collisionEvent.EntityA;
        }
        else if (vehicleComponentLookup.HasComponent(collisionEvent.EntityB))
        {
            vehicleColliderKey = collisionEvent.ColliderKeyB;
            return collisionEvent.EntityB;
        }

        vehicleColliderKey = ColliderKey.Empty;
        return Entity.Null;
    }
}```
dreamy canopy
#

potentially your wheel is just child entity of your collider entity

#

inspect subscene in edit mode

#

and see how your component-entity relationships are setup for your wheels

humble quarry
dreamy canopy
#

whatever is relevant here

#

you check collider entity - it has no component

#

are you sure it's supposed to?

humble quarry
#

just checked my vehicle entity during runtime, the vehicle has a physics collider and vehicle component, and the wheel has a wheel component, but no physics collider? (there are physics colliders in the prefab)

#

the wheel is 4 stages deep in the hierchy

dreamy canopy
#

physics colliders can only exist at root of physics body

#

so they probably get moved to root

#

while your wheel is somehwere as a child

humble quarry
#

so does this mean unity creates a fake entity child?

dreamy canopy
#

no

#

just inspect your wheel

#

and find you collider

humble quarry
#

I'm getting entities returned when I do GetChild that don't exist in the inspector though

#

it's got a collider

dreamy canopy
#

authoring does not match runtime 1 to 1

#

inspect entities

#

not game objects

humble quarry
#

wheels have got a wheel component

#

when I debug cs if (!vehiclePhysicsCollider.ColliderPtr->GetChild(ref vehicleColliderKey, out ChildCollider vehicleEntityChildCollided)) return Entity.Null;
vehicleEntityChildCollided, it prints out an entity that doesn't exist in the inspector, any reason for this?

#

I was thinking of making use of the keys in the Physics Collider Key Entity Pair buffer (on the root entity), might lead me somewhere

#

yep, looks like the collider keys in the root entity buffer match the ones in the collision event, will cache those and use them to find the correct entity