Weird interactions between transform.forward and quaternions

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:

void DrawLine (Vector3 Di, int i)
    {
        if(Physics.Raycast(gameObject.transform.position, Di, out hit, MaxLenght))
        {
            line.SetPosition(i, gameObject.transform.position);
            line.SetPosition(i+1, hit.point);
            if(hit.transform.gameObject.name=="Player" && InvisScript.isInvis==false)
            {
                isAngry=true;
                playerSpottedPosition=hit.point;
            }
        }
        else {
            line.SetPosition(i, gameObject.transform.position);
            line.SetPosition(i+1, transform.position + Di*MaxLenght);
        }
    }

    void Gen (int n)
    {
        int i;
        isAngry = false;
        for (i=0; i<n; i++) {
            Vector2 alpha, r;
            r.y = MaxR*Mathf.Cos(Mathf.Deg2Rad * i*360/n);
            r.x = MaxR*Mathf.Sin(Mathf.Deg2Rad * i*360/n);
            alpha.x = Mathf.Atan(r.x / MaxLenght) * Mathf.Rad2Deg;
            alpha.y = Mathf.Atan(r.y / MaxLenght) * Mathf.Rad2Deg;

            Vector3 Di = (Quaternion.Euler(alpha.x,alpha.y,0) * transform.forward).normalized;
            DrawLine(Di, 2*i);
        }

If I calculate the Z rotation (r.z = MaxRMathf.Sin(Mathf.Deg2Rad * i360/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…

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.

1 Like

That worked! Thanks man!

1 Like

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!)