- Home /
Draw line to a specific angle
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.
Answer by duck · Jan 11, 2011 at 12:45 PM
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);
Thanks for your reply. The code doesn't work as DrawLine is expecting a Vector3 where we have a Quaternion, I'm guessing turning it into a Vector should be fairly straight forward.
doh! I forgot the crucial part which I included in the example lines above! to multiply it by "line"! (amended now)
Answer by Jeffom · Jan 11, 2011 at 01:40 PM
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);
}
I can see what this is doing, but I'm trying to draw the line from the objects transform.forward. So if you imagine the transform.forward is 0, I want to draw the line from that point outwards but at an angle.
then change the transform.position to transform.forward. and then limit the yAngle and xAngle to your desired direction
@Jeffom, I tried implementing the change you suggested above (replacing transform.position with transform.forward), but it didnt work; is the code supposed to be the following?
private void DrawLine (float distance, float xAngle, float yAngle) {
float x = transform.forward.x + distance $$anonymous$$athf.Sin (xAngle $$anonymous$$athf.Deg2Rad) $$anonymous$$athf.Cos (yAngle $$anonymous$$athf.Deg2Rad);
float y = transform.forward.y + distance $$anonymous$$athf.Sin (xAngle $$anonymous$$athf.Deg2Rad) $$anonymous$$athf.Sin (yAngle $$anonymous$$athf.Deg2Rad);
float z = transform.forward.z + distance $$anonymous$$athf.Cos (xAngle $$anonymous$$athf.Deg2Rad);
Vector3 finalPos = new Vector3 (x, y, z);
Debug.DrawLine (transform.position, finalPos, new Color (0.0f, 0.0f, 1.0f, 0.3f), 0.8f); }