Hi,
I’m having a wierd issue, I have a turret prefab which has a child “head”. My goal is to rotate the head (and only the head) towards a predetermined target. The problem is that if I go in the editor, I can see the head’s transform values changing and rotating towards the target, but I can’t actually see it in the scene. I know my code works, because the ray is always facing the target and if I turn the whole turret (instead of just the head), I can actually see it moving.
Here is my code :
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
[UpdateInGroup(typeof(CustomLateSimulationSystemGroup))]
public partial struct RotateTurretSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<EnemyComponent>();
using EntityQueryBuilder queryBuilder = new EntityQueryBuilder(Allocator.Temp)
.WithAll<TurretComponent, Solidified>();
state.RequireForUpdate(state.GetEntityQuery(queryBuilder));
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
foreach ((RefRO<TurretTarget> target, RefRW<LocalTransform> transform, RefRO<LocalToWorld> localToWorld, DynamicBuffer<Child> childrens)
in SystemAPI.Query<RefRO<TurretTarget>, RefRW<LocalTransform>, RefRO<LocalToWorld>, DynamicBuffer<Child>>().WithAll<Solidified>())
{
if (!SystemAPI.Exists(target.ValueRO.Entity))
{
continue;
}
Entity head = childrens[0].Value;
LocalToWorld targetTrans = SystemAPI.GetComponent<LocalToWorld>(target.ValueRO.Entity);
RefRW<LocalTransform> local = SystemAPI.GetComponentRW<LocalTransform>(head);
LocalToWorld trans = SystemAPI.GetComponent<LocalToWorld>(head);
float3 direction = targetTrans.Position - trans.Position;
direction.y = 0;
direction = math.normalize(direction);
direction = math.mul(math.inverse(trans.Rotation), direction);
quaternion targetRotation = quaternion.LookRotationSafe(direction, math.up());
local.ValueRW.Rotation = math.mul(targetRotation, local.ValueRO.Rotation);
Debug.DrawRay(trans.Position, math.mul(trans.Rotation, direction) * 2, Color.blue);
}
}
}
Here is the turret’s hierachy :

