Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Disaster · Jan 11, 2011 at 12:34 PM · vector3math

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
8

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);
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Disaster · Jan 11, 2011 at 12:57 PM 0
Share

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.

avatar image duck ♦♦ · Jan 12, 2011 at 10:39 AM 0
Share

doh! I forgot the crucial part which I included in the example lines above! to multiply it by "line"! (amended now)

avatar image
1
Wiki

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

}

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Disaster · Jan 11, 2011 at 03:07 PM 0
Share

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.

avatar image Jeffom · Jan 11, 2011 at 04:01 PM 0
Share

then change the transform.position to transform.forward. and then limit the yAngle and xAngle to your desired direction

avatar image AntLewis · Sep 05, 2012 at 08:31 PM 0
Share

@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); }

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Calculate a new position instead of using Vector3.back 1 Answer

Creating a bouncing game without using physics - Vector3 math problem, 2 Answers

localPosition vs Position Subtraction as vector direction 1 Answer

How do I find slope on a spherical terrain 2 Answers

VR: Controller rotation delta along other axis 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges