Search Unity

ECS - Convert rotation to normalised vector.

Discussion in '2D' started by Reloque, Nov 25, 2019.

  1. Reloque

    Reloque

    Joined:
    Apr 28, 2015
    Posts:
    207
    This seems strange, I have a pretty straightforward thing, I have an entity, I need to move it along it's own rotation for a normalised vector.

    Can't figure it out. I have a rotation, a quaternion and I would assume I'd be able to get a vector from it. But no luck. I can't convert that quaternion to a vector.

    Code (CSharp):
    1.  Vector3.Normalize(target.Value);
    2. "Cannot implicitly convert type 'UnityEngine.Vector3' to 'Unity.Mathematics.quaternion"
    I've tried a bunch of different things as well, but in the end it's probably my lack of properly understanding quaternions. So, how do I convert a quaternion to a vector that I then can add to my physics body like this;

    Code (CSharp):
    1. physicsVelocity.Linear = vector;
     
  2. Reloque

    Reloque

    Joined:
    Apr 28, 2015
    Posts:
    207
    This code works, but please tell me a better way, this hacky bit can't be good

    Code (CSharp):
    1.                 Quaternion currentRotation = rotation.Value;
    2.                 var angle = 360 - currentRotation.eulerAngles.z;
    3.                 var rad = angle * Mathf.Deg2Rad;
    4.  
    5.                 // covert the angle to a 2d vector
    6.                 Vector2 v2 = new Vector2(Mathf.Cos(rad), Mathf.Sin(rad));
    7.                 float3 velocity;
    8.  
    9.                 velocity = new float3(v2.y, v2.x, 0f);
    10.                 physicsVelocity.Linear = velocity;