Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Pure ECS and transform.forward vector

Discussion in 'Entity Component System' started by vincismurf, Dec 7, 2018.

  1. vincismurf

    vincismurf

    Joined:
    Feb 28, 2013
    Posts:
    200
    How does one get access to the forward vector for a pure ECS system?
     
  2. avvie

    avvie

    Joined:
    Jan 26, 2014
    Posts:
    74
    well I was reading the source the other day and i found nothing this superfluous on the transform system.
    So if you really want a forward vector on some entities, just make a component that holds it and a system that updates it
     
  3. vincismurf

    vincismurf

    Joined:
    Feb 28, 2013
    Posts:
    200
    I also ran across Math.forward(). . . . where you provide a quaternion
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,584
    transform.forward is simply quaternion * vector.forward, which in this case is transform.rotation * Vector3 (1,0,0) Vector3 (0,0,1).

    Then need of course put that into ECS representation.
     
    Last edited: Dec 11, 2018
  5. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    Vector3.forward is (0,0,1)
     
    HTime12 likes this.
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,584
    Ops yes, sorry my wrong. Thx for correction. I must be dreaming about something else ;)
     
  7. recursive

    recursive

    Joined:
    Jul 12, 2012
    Posts:
    669
    I mean, it's technically forward. In the 2D XY plane space defined by basis vectors (0,1,0 ) and (1,0,0). :p
     
  8. Dbgamerstarz

    Dbgamerstarz

    Joined:
    Jul 1, 2017
    Posts:
    3
    For anyone reading this now, I saved a camera reference into a IComponentData and used that to define a transform.forward
     
    ujz likes this.
  9. Sobirux

    Sobirux

    Joined:
    Apr 2, 2019
    Posts:
    6
    How would one add a reference to the Camera class inside IComponentData? I thought you can only add primitive/bittable types to IComponentData.
     
  10. WAYNGames

    WAYNGames

    Joined:
    Mar 16, 2019
    Posts:
    939
    You can make an IComponentData class and use it as a usual IComponentData struct.
    The only thing is that you can only use it on the main thread so all your "job" have to run instead of schedule and also you can't use burst.
    So it may be convenient but it's definitely not 'DOTS' performant.
     
  11. Sobirux

    Sobirux

    Joined:
    Apr 2, 2019
    Posts:
    6
    Thanks for the quick reply. When I try adding a Camera class to an IComponentData struct, I get "ArgumentException: Component contains a field of UnityEngine.Camera, which is neither primitive nor blittable." Am I missing something?
     
  12. thelebaron

    thelebaron

    Joined:
    Jun 2, 2013
    Posts:
    825
    make sure its a class not a struct ie
    public class MyComponent : IComponentData
     
  13. Sarkahn

    Sarkahn

    Joined:
    Jan 9, 2013
    Posts:
    440
    Not sure what all this business with the camera is about but for an entity's forward vector you can do:

    Code (CSharp):
    1. public class MoveForwardSystem : SystemBase
    2. {
    3.     protected override void OnUpdate()
    4.     {
    5.         float dt = Time.DeltaTime;
    6.         Entities.ForEach((ref Translation t, in LocalToWorld ltw) =>
    7.         {
    8.             t.Value += ltw.Forward * dt;
    9.         }).Schedule();
    10.     }
    11. }
    I think that's what at least some of you are looking for?
     
    shotoutgames, bb8_1, jmcusack and 8 others like this.
  14. jasonlawless

    jasonlawless

    Joined:
    Sep 21, 2019
    Posts:
    3
    This needs to be marked at the answer to the original question. Clean, straight to the point, no bs. Well done Sarkahn, and thank you.
     
    MNNoxMortem likes this.
  15. gwydion1

    gwydion1

    Joined:
    Jan 18, 2019
    Posts:
    3
    I SPENT SO LONG LOOKING FOR AN ANSWER thank you so much!!!
     
  16. harishgadhamsettyptw

    harishgadhamsettyptw

    Joined:
    Feb 2, 2022
    Posts:
    1
    Thank you so much for the answer. You saved me lot of time.
     
    bb8_1 likes this.
  17. inSight01

    inSight01

    Joined:
    Apr 18, 2017
    Posts:
    88
    I like to use math.mul(rotation, direction). But you can use math.forward(rotation) which basically uses math.mul internally I believe.


    Code (CSharp):
    1. Entities.ForEach((ref Rotation rotation) =>
    2.         {
    3.             // Forward vector = float3(0f, 0f, 1f)
    4.             float3 forward = math.mul(rotation.Value, new float3(0f, 0f, 1f));
    5.  
    6.             // You can also use math.forward(rotation)
    7.             forward = math.forward(rotation.Value);
    8.         }).Run();
     
    Gekigengar and bb8_1 like this.