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. Dismiss Notice

Weird interactions between transform.forward and quaternions

Discussion in 'Scripting' started by JustASHadow, Nov 5, 2014.

  1. JustASHadow

    JustASHadow

    Joined:
    Oct 28, 2014
    Posts:
    7
    I'm trying to generate a cone out of line renderers, but i'm struggling to figure out the direction of each line. My code works perfectly when my object has a Y rotation of 0 or 180 degrees, but as it turns the cone becomes more and more flat, turning into more of a 2D shape at 90 and 270 Y rotation. Here's my code:

    Code (CSharp):
    1. void DrawLine (Vector3 Di, int i)
    2.     {
    3.         if(Physics.Raycast(gameObject.transform.position, Di, out hit, MaxLenght))
    4.         {
    5.             line.SetPosition(i, gameObject.transform.position);
    6.             line.SetPosition(i+1, hit.point);
    7.             if(hit.transform.gameObject.name=="Player" && InvisScript.isInvis==false)
    8.             {
    9.                 isAngry=true;
    10.                 playerSpottedPosition=hit.point;
    11.             }
    12.         }
    13.         else {
    14.             line.SetPosition(i, gameObject.transform.position);
    15.             line.SetPosition(i+1, transform.position + Di*MaxLenght);
    16.         }
    17.     }
    18.  
    19.     void Gen (int n)
    20.     {
    21.         int i;
    22.         isAngry = false;
    23.         for (i=0; i<n; i++) {
    24.             Vector2 alpha, r;
    25.             r.y = MaxR*Mathf.Cos(Mathf.Deg2Rad * i*360/n);
    26.             r.x = MaxR*Mathf.Sin(Mathf.Deg2Rad * i*360/n);
    27.             alpha.x = Mathf.Atan(r.x / MaxLenght) * Mathf.Rad2Deg;
    28.             alpha.y = Mathf.Atan(r.y / MaxLenght) * Mathf.Rad2Deg;
    29.  
    30.             Vector3 Di = (Quaternion.Euler(alpha.x,alpha.y,0) * transform.forward).normalized;
    31.             DrawLine(Di, 2*i);
    32.         }
    If I calculate the Z rotation (r.z = MaxR*Mathf.Sin(Mathf.Deg2Rad * i*360/n); alpha.z = Mathf.Atan(r.z / MaxLenght) * Mathf.Rad2Deg) I get the same problem, just 45 degrees earlier. There's something I'm missing about these axes...
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    I would approach this differently. Work out the equations for your cone in local space, i.e. relative to 0,0,0 and oriented around the +Z axis. Then just use transform.TransformPoint (to get the endpoint) or transform.TransformDirection (to get the direction) on each line, and draw that.
     
    JustASHadow likes this.
  3. JustASHadow

    JustASHadow

    Joined:
    Oct 28, 2014
    Posts:
    7
    That worked! Thanks man!
     
    JoeStrout likes this.
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    Don't thank me, I'm happy to help. (But if you really want to show your gratitude, you could always help me out with my KickStarter!)