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
0
Question by Thor625 · Jul 02, 2013 at 05:07 AM · javascriptvector3anglemethod

Return Angle as Vector 3

Hello, I have to raycast into a direction to check if there is a clear path between two objects. To do that I need a vector 3 of an angle. How would I do this.

Thanks,

Thor

Comment
Add comment · Show 2
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 robertbu · Jul 02, 2013 at 05:28 AM 1
Share

I'm not sure what you are asking. You don't need an angle to get a vector3. If you are casting from object1 to object2, you calculate the direction:

var direction = object2.transform.position - object1.transform.position;

Note you might consider a Physics.Linecast() ins$$anonymous$$d of a Raycast().

avatar image clunk47 · Jul 02, 2013 at 06:38 AM 0
Share

Not sure why u want an angle because the question is unclear. Going off this though, I'll give an example of say, you want to see if there is something in front of a box. Then print how far away this object is from your box.

 //C# Example
 
 using UnityEngine;
 using System.Collections;
 
 public class Example : $$anonymous$$onoBehaviour
 {
     RaycastHit hit;
 
     void Update()
     {
         if(Physics.Raycast(transform.position, transform.forward, out hit, $$anonymous$$athf.Infinity))
         {
             float distance = Vector3.Distance(transform.position, hit.collider.transform.position);
             print("Distance from " + hit.collider.gameObject.name + " is " + distance + " units. ");
         }
     }
 
 }

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by ebogguess · Jul 02, 2013 at 05:29 AM

The vector between two objects is the difference between their transform position vectors. If you can ray cast in that direction, from one of the objects and hit the other, then you have line of sight.

Comment
Add comment · 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
0

Answer by Owen-Reynolds · Jul 02, 2013 at 02:08 PM

If you have angle A, which will be a Quaternion, then a vector pointing that way is A*Vector3.forward; If you have an angle in mind, like 30 degrees on y, 5 tilted up on x, then Quaternion.Euler(-5,30,0)*Vector3.forward; gives a length 1 V3 aimed that way (negative x is up -- left-handed rule.)

Most people would probably just guess that Vector3(1,0.1f,0.8f) was at about that angle, so is good enough.

transform.forward is shorthand for transform.rotation*Vector3.forward; That math is: Unity thinks 0 degrees on x and y is North. So, Quaternion.identity*Vector3.forward gives back Vector3.forward, unchanged, which is North, which is rotation 00.

As the other posters say, you usually got the angle starting with an aim point or a normal or something like that. So it's usually easier to just use the starting point directly and skip ever having the angle.

Comment
Add comment · 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

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

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to get the angle of the terrain under de front and back of my object 1 Answer

Angle Between vectors in Navigation Mesh 1 Answer

How would I move an Object after collision? 1 Answer

Finding Distances With Vector3 1 Answer

Get angle relative to GameObject 1 Answer


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