#Changing localTransform Rotation

1 messages · Page 1 of 1 (latest)

slow geyser
#

Here's the code I'm trying to get to work.

public partial struct MovementSystem : ISystem
{
    public void OnUpdate(ref SystemState state)
    {
        foreach (var (localTransform, velocity, forward) in
                SystemAPI.Query<RefRW<LocalTransform>,RefRO<VelocityComponent>, RefRW<ForwardComponent>>())
        {
            localTransform.ValueRW.Position += velocity.ValueRO.Velocity;
            //Problem
            Quaternion rotationQuaternion = math.normalize(Quaternion.Euler(0f, 0f, velocity.ValueRO.Rotation * 10000));
            Debug.Log("rotationQuaternion: " + rotationQuaternion + "velocity.ValueRO.Rotation: " + velocity.ValueRO.Rotation);
            localTransform.ValueRW.Rotate(rotationQuaternion);
            //localTransform.ValueRW.RotateZ(velocity.ValueRO.Rotation);


            var cosAngle = math.cos(velocity.ValueRO.Rotation);
            var sinAngle = math.sin(velocity.ValueRO.Rotation);
            var x = forward.ValueRO.Forward.x;
            var y = forward.ValueRO.Forward.y;
            forward.ValueRW.Forward = math.normalize(new float3(cosAngle * x - sinAngle * y, sinAngle * x + cosAngle * y, 0f));
            

            //This works on local transform, but doesn't work in drawing
            //localTransform.ValueRW.Rotation = math.normalize(Quaternion.Euler(forward.ValueRO.Forward));
        }
    }
}

The only version that actually applies rotation is the lowest one, setting the rotation manually. However, the entity in question has a Sprite2D as a child, and the sprite2D localtransform does not update when I set the localtransform on the parent. Using the localtransform.rotate/rotatez does not update the parents localtransform at all.

long knot
#

LocalTransform.Rotate and similar members don't modify the instance on which they're called. They return a modified value that needs to be assigned back.
https://docs.unity3d.com/Packages/com.unity.entities@1.2/api/Unity.Transforms.LocalTransform.Rotate.html#Unity_Transforms_LocalTransform_Rotate_Unity_Mathematics_quaternion_
LocalTransform is purely relative to local space in the parent (global when there's no parent). A child entity would not have its LocalTransform value modified when the parent is moved, the whole hierarchy is moved along with the parent, so the child still has the same transformation relative to the parent.