I tried two options, one from the Laser Dot s 101 example, the other by simply going through all the suitable colliders, none registering an obvious collision. There is a client world on the stage, there is no server world (this is something like a lobby scene before connecting) Does anyone know how this can be achieved?
ClientDispBoxCollisionSystem.cs
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Physics;
using Unity.Physics.Systems;
using Unity.Rendering;
using Unity.Transforms;
using UnityEngine;
using Collider = Unity.Physics.Collider;
[BurstCompile]
[UpdateAfter(typeof(FixedStepSimulationSystemGroup))]
[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation)]
public partial struct ClientDispBoxCollisionSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<CamTran>();
state.RequireForUpdate<DispBox>();
state.RequireForUpdate<PhysicsWorldSingleton>();
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var ecb = new EntityCommandBuffer(Allocator.Temp);
// Получаем данные о трансформации камеры
var camTran = SystemAPI.GetSingleton<CamTran>();
// Параметры луча: начало — позиция камеры, конец — 10 единиц вперед
float rayLength = 5f;
float3 rayStart = camTran.tran.Position;
float3 rayEnd = camTran.tran.Position + camTran.tran.Forward() * rayLength;
Utils.DrawLine(ref ecb, rayStart, rayEnd, Color.green);
var raycastInput = new RaycastInput
{
// specify starting and end points of the raycast (which together imply direction)
Start = rayStart,
End = rayEnd,
// don't forget to set a filter or else you'll get no hits!
Filter = CollisionFilter.Default
};
// Перебираем все сущности с DispBox и PhysicsCollider
foreach (var (dispBox, collider, entity) in SystemAPI.Query<RefRO<DispBox>, RefRO<PhysicsCollider>>().WithEntityAccess())
{
unsafe
{
Collider* colliderPtr = collider.ValueRO.ColliderPtr;
colliderPtr->SetCollisionResponse(CollisionResponsePolicy.CollideRaiseCollisionEvents);
if (colliderPtr->CastRay(raycastInput, out var hit))
{
Debug.Log("Collide");
// Если луч попал в коллайдер, меняем цвет
SystemAPI.SetComponent(entity, new URPMaterialPropertyBaseColor
{
Value = new float4(1f, 0f, 0f, 1f) // Красный
});
}
else
{
Debug.Log("NoCollideUnsafe");
}
}
}
// to perform raycasts or other collision queries, we need the collision world
var physicsWorld = SystemAPI.GetSingleton<PhysicsWorldSingleton>();
if (physicsWorld.CollisionWorld.CastRay(raycastInput, out var closestHit))
{
//Debug.Log(closestHit.Entity.Index);
// Получаем текущий компонент DispBox
if (SystemAPI.HasComponent<DispBox>(closestHit.Entity))
{
// Устанавливаем красный цвет
SystemAPI.SetComponent(closestHit.Entity, new URPMaterialPropertyBaseColor
{
Value = new float4(1, 0, 0, 1),
});
}
}
else
{
Debug.Log("NoCollideWorld");
}
ecb.Playback(state.EntityManager);
ecb.Dispose();
}
}






