public Entity GetCarAtScreenPosition(Vector3 screenPosition, float radius) { var result = Entity.Null; var em = World.Active?.EntityManager; if (em != null) { var query = em.CreateEntityQuery(typeof(CarState)); var entities = query.ToEntityArray(Allocator.TempJob); for (int i = 0; i < entities.Length; ++i) { var state = em.GetComponentData <CarState>(entities[i]); DotsHighway.GetWorldPosition( state.PositionOnTrack, state.Lane, out float3 pos, out quaternion rot); Vector3 carScreenPos = Camera.main.WorldToScreenPoint(pos); carScreenPos.z = 0; if (Vector3.Distance(screenPosition, carScreenPos) <= radius) { result = entities[i]; break; } } entities.Dispose(); } return(result); }
public void Execute( [ReadOnly] ref CarState state, [ReadOnly] ref CarSettings settings, ref Translation translation, ref Rotation rotation, ref ColorComponent color) { DotsHighway.GetWorldPosition( state.PositionOnTrack, state.Lane, out float3 pos, out quaternion q); translation.Value = pos; rotation.Value = q; bool isFaster = state.FwdSpeed > settings.DefaultSpeed; float maxSpeed = math.select( settings.DefaultSpeed, settings.DefaultSpeed * settings.OvertakePercent, isFaster); float minSpeed = math.select(0, settings.DefaultSpeed, isFaster); float4 minColor = math.select(minSpeedColor, defaultColor, isFaster); float4 maxColor = math.select(defaultColor, maxSpeedColor, isFaster); color.Value = math.lerp( minColor, maxColor, (state.FwdSpeed - minSpeed) / (maxSpeed - minSpeed)); }