Hi there,
I'm trying to draw a line from my gameObject which leaves at a certain angle, please see this:
var l:Vector3 = transform.position + ( transform.forward * visionDistance );
Debug.DrawLine(transform.position, l, Color.blue);
This draws a line directly forward. What I want to do is draw a line that draws forwards but at a specific angle offset. I tried modifying the x property of the l vector, but it didn't work as intended.
duck
January 11, 2011, 12:45pm
2
You can rotate a vector by multiplying a rotation (a Quaternion ) by you vector. Eg
var rotatedVector = someRotation * someVector;
To illustrate, you can get the equivalent of transform.forward, by calculating:
var myForward = transform.rotation * Vector3.forward;
So to rotate your "line" by a certain amount around the object's local up axis, you could use something like this:
var line = transform.position + ( transform.forward * visionDistance );
var rotatedLine = Quaternion.AngleAxis( angle, transform.up ) * line;
Debug.DrawLine(transform.position, rotatedLine, Color.blue);
Jeffom
January 11, 2011, 1:40pm
3
I think your problem is finding the final position for the draw line, by applying the parameric sphere equation i think you can solve this
public float distance;
public float xAngle;
public float yAngle;
void OnDrawGizmos()
{
float x = transform.position.x + distance * Mathf.Sin(xAngle * Mathf.Deg2Rad) * Mathf.Cos(yAngle * Mathf.Deg2Rad);
float y = transform.position.y + distance * Mathf.Sin(xAngle * Mathf.Deg2Rad) * Mathf.Sin(yAngle * Mathf.Deg2Rad);
float z = transform.position.z + distance * Mathf.Cos(xAngle * Mathf.Deg2Rad);
Vector3 finalPos = new Vector3(x, y, z);
Debug.DrawLine(transform.position, finalPos, Color.green);
}