Help with strafe movement

Hello

This should be a simple problem but i cant get how to strafe my character relative to entity orientation. I can move backward, forward and rotate. But not strafe.

I want to move along the red arrow on the character. In my MonoBehaviour version i used translate.

7539671--931325--upload_2021-10-1_23-38-36.png

    public class CharacterControllerSystem : SystemBase
    {

        protected override void OnUpdate()
        {
            float deltaTime = Time.DeltaTime;
            Entities.ForEach((ref PlayerControllerComponent player, ref Translation translation, ref Rotation rotation) =>
            {

                quaternion direction = quaternion.RotateY(90f);
                translation.Value = translation.Value + (player.inputVertical * math.forward(rotation.Value)) * 5f * deltaTime;
                translation.Value = translation.Value + (player.inputHorizontal * ? * 5f * deltaTime; //Strafe


                if (player.leftKey)
                {
                    player.currentRotation = player.currentRotation - 2f * deltaTime;
                    rotation.Value = quaternion.RotateY(player.currentRotation);
                }
                if (playerControllerComponent.rightKey)
                {
                    player.currentRotation = player.currentRotation + 2f * deltaTime;
                    rotation.Value = quaternion.RotateY(player.currentRotation);                 
                }

            }).Schedule();
        }
    }

Any help is appreciated

I wrote this ages ago in one of my camera scripts but I think the logic should be the same for a character strafe. There might be a nicer way to do it but this seemed to work for me.

var rightNoY = rotation.Value.Right();
rightNoY.y = 0;
rightNoY = math.normalizesafe(rightNoY);

// ...

translation.Value += rightNoY * moveSpeed * input.Move.x;

where Right() is

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float3 Right(this quaternion value)
{
    return math.mul(value, math.right());
}

(this obviously only moves in the xz plane as y has been zeroed. If you wanted to move in all 3 planes just remove the zeroing)

1 Like

Thanks it worked