Set a "world" transform.forward for pure ECS Entity

Hello everybody,

My question is - how can I set up a forward vector for my Entity via SetComponentData. I want to get transform.forward vector from an object on the scene and make my Entity look forward in the same direction.


generator picture
I want my object on spawn will have the same z-axis direction as for spawnPoint.

For common GameObject, I will do something like this

newCar.transform.forward = selectedSpawnPoint.transform.forward;

For Entity, I try to make something like this (this is doesn’t work!)

_entityManager.SetComponentData(newCar, new Rotation
        {
            Value = Quaternion.Euler(selectedSpawnPoint.transform.forward)
        });

I try to use old guides, but can’t do anything with this. Thank you for help :slight_smile:

QUESTION CLOSED!
So, I find a solution for my task :slight_smile:

_entityManager.SetComponentData(newCar, new Rotation
        {
            Value = quaternion.LookRotationSafe(selectedSpawnPoint.transform.forward, selectedSpawnPoint.transform.up)
        });

Use quaternion.LookRotation:

Value  = Quaternion.LookRotation(selectedSpawnPoint.transform.forward);

edit: I think it’s better to use Quaternion instead of quaternion in this case because vector3s

Hi :slight_smile: Actually this does only local rotation. Z-axis direction doesn’t changed :frowning:

I think you are mixing the old api with the new one a bit too much here.

I think you should be getting localToWorld.Forward of your spawnPoint entity and simply set the localToWorld.Forward of your spawning entity to be the same value.

Hi:) Thank you for the answer, but spawnPoint - this is a GameObject :slight_smile:

@MagicianArtemk In that case get the
Quaternion.LookRotation(transform.forward, transform.up) of your spawnPoint entity and simply set the localToWorld.Forward of your spawning entity to that value.

Quaternions and rotations in the new Unity.Mathematics/ECS are a bit cleaner imo.

EDIT: I see you already found the solution.

1 Like