I need to cast a raycast from the monsters to the player, but the monster is a DOTS and the player is a monobehaviour, meaning they are in different worlds. Does anyone know how I can do this?
I tried like this but without success
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Physics;
using Unity.Transforms;
public partial struct MonsterVisionSystem : ISystem
{
public void OnCreate( ref SystemState state )
{
// Garantir que o PhysicsWorld esteja disponível
state.RequireForUpdate<PhysicsWorldSingleton>( );
}
public void OnUpdate( ref SystemState state )
{
var physicsWorld = SystemAPI.GetSingleton<PhysicsWorldSingleton>( ).PhysicsWorld;
var ecb = new EntityCommandBuffer( Allocator.Temp );
float3 playerPosition = PlayerController.Instance.transform.position;
foreach ( var (monsterTransform, monsterEntity) in SystemAPI.Query<RefRO<LocalToWorld>>( ).WithEntityAccess( ) )
{
float3 monsterPosition = monsterTransform.ValueRO.Position;
float3 direction = math.normalize( playerPosition - monsterPosition );
float maxDistance = math.distance( playerPosition , monsterPosition );
RaycastInput raycastInput = new RaycastInput
{
Start = monsterPosition ,
End = monsterPosition + direction * maxDistance ,
Filter = new CollisionFilter( )
{
BelongsTo = ~0u ,
CollidesWith = ~0u ,
GroupIndex = 0
}
};
// Realizar o Raycast
if ( physicsWorld.CastRay( raycastInput , out RaycastHit hit ) )
{
UnityEngine.Debug.Log( "1" );
}
else
{
UnityEngine.Debug.Log( "2" );
}
ecb.Playback( state.EntityManager );
}
}
}