Search Unity

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

Discussion in 'Entity Component System' started by MagicianArtemka, May 28, 2020.

  1. MagicianArtemka

    MagicianArtemka

    Joined:
    Jan 15, 2019
    Posts:
    46
    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
    Code (CSharp):
    1. newCar.transform.forward = selectedSpawnPoint.transform.forward;
    For Entity, I try to make something like this (this is doesn't work!)
    Code (CSharp):
    1. _entityManager.SetComponentData(newCar, new Rotation
    2.         {
    3.             Value = Quaternion.Euler(selectedSpawnPoint.transform.forward)
    4.         });
    I try to use old guides, but can't do anything with this. Thank you for help :)

    QUESTION CLOSED!
    So, I find a solution for my task :)
    Code (CSharp):
    1. _entityManager.SetComponentData(newCar, new Rotation
    2.         {
    3.             Value = quaternion.LookRotationSafe(selectedSpawnPoint.transform.forward, selectedSpawnPoint.transform.up)
    4.         });
     
    Last edited: May 28, 2020
  2. MhmdAL1111

    MhmdAL1111

    Joined:
    Oct 25, 2017
    Posts:
    30
    Use quaternion.LookRotation:
    Code (CSharp):
    1. Value  = Quaternion.LookRotation(selectedSpawnPoint.transform.forward);
    edit: I think it's better to use Quaternion instead of quaternion in this case because vector3s
     
  3. MagicianArtemka

    MagicianArtemka

    Joined:
    Jan 15, 2019
    Posts:
    46
    Hi :) Actually this does only local rotation. Z-axis direction doesn't changed :(
     
  4. RogueStargun

    RogueStargun

    Joined:
    Aug 5, 2018
    Posts:
    296
    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.
     
  5. MagicianArtemka

    MagicianArtemka

    Joined:
    Jan 15, 2019
    Posts:
    46
    Hi:) Thank you for the answer, but spawnPoint - this is a GameObject :)
     
  6. RogueStargun

    RogueStargun

    Joined:
    Aug 5, 2018
    Posts:
    296
    @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.
     
    MagicianArtemka likes this.