# Move forward based on rotation in ECS

I’m having trouble figuring out how to get an entity to move forward (based on its rotation) using ECS. Essentially I have the entity’s Translation and Rotation but I can’t figure out how to turn that into a usable float3. I’ve tried using math.forward(rotation.Value) but that always returns (0,0,1) regardless of how the entity rotates (around the Z axis that is). I’m assuming the (0,0,1) value is because of the axis change from 3d to 2d. I’ve also looked at math.up() but that involves no rotation whatsoever.

I ended up doing some matrix conversion math just to get this to work. Is there an easier way to do this that I just overlooked? Or do we just not have the tools needed yet in the DOTS system to work well in 2d?

Below is an example of the current code:

``````using UnityEngine;
using Unity.Entities;
using Unity.Jobs;
using Unity.Transforms;
using Unity.Mathematics;

public class MoveDirectionSystem : JobComponentSystem
{
private struct MoveDirectionJob : IJobForEach<Translation, Rotation>
{
public float deltaTime;

public void Execute(ref Translation translation, ref Rotation rotation)
{
//Up
float3 upwardVector = new float3(
(2 * (rotation.Value.value.x * rotation.Value.value.y - rotation.Value.value.w * rotation.Value.value.z)),
(1 - 2 * (rotation.Value.value.x * rotation.Value.value.x + rotation.Value.value.z * rotation.Value.value.z)),
(2 * (rotation.Value.value.y * rotation.Value.value.z + rotation.Value.value.w * rotation.Value.value.x)));

translation.Value += upwardVector * deltaTime;
}
}

protected override JobHandle OnUpdate(JobHandle inputDeps)
{
if (Input.GetKey(KeyCode.N))
{
MoveDirectionJob job = new MoveDirectionJob { deltaTime = Time.deltaTime };
return job.Schedule(this, inputDeps);
}

return default;
}
}
``````

It’s not clear if you want to omit the XY rotations or not and only have Z rotation.

Anyway, you can simply rotate a unit-vector:

``````var direction = math.mul(rotation.Value, new float3(0f, 1f, 0f));
``````
7 Likes

That was the simple answer I was looking for! Thank you for the help!

1 Like