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 flash2099 · May 03, 2015 at 09:15 PM · angleanglesraycastsangleaxis

Raycast at angles

Hi All,

I am currently working on a simple line of sight system where 3 raycasts are used, one to the middle of the target and two either side of the target which can be seen in the image below:

alt text

I am struggling with getting the two raycasts to go either side of the targets position, currently I am using the following line of code:

var offsetRight = Quaternion.Euler(0, 1, 0) * rayDir;

var offsetLeft = Quaternion.Euler(0, -1, 0) * rayDir;

rayDir being the direction towards the target.

I know i need to replace the "1" with the angle from next to the target from the npc, could anyone explain how to do this? I know the answer is probably simple but I am drawing blanks on it.

Thanks for the assistance!

unityanswersexample.jpg (24.6 kB)
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

1 Reply

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

Answer by AlwaysSunny · May 03, 2015 at 09:13 PM

The easy way involves vector math instead of quaternions. It also implies you don't need angles greater than roughly 180 degrees, and that you don't need an exact angle measured in angles. You'll find the angles experimentally by changing N:

(To match your drawing almost perfectly, N would be one red dot's radius)

Use Vector3.Cross to get a vector perpendicular to your original raycast. The end points of your two "cone" rays will be at

 right_ray_endpoint = center_ray_endpoint + cross_result.normalized * N;
 left_ray_endpoint = center_ray_endpoint + cross_result.normalized * -N;


Comment
Add comment · Show 1 · 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 flash2099 · May 04, 2015 at 09:40 AM 0
Share

Hey Thanks for the assistance!

Your solutions seems to work and my 2 end points are consistently the correct spacing apart no matter what my distance, However my cone rotates vertically(sometimes the two points are the characters head and feet ins$$anonymous$$d of being next to the character) depending on the angle I move around the NPC, here is my following code:

 Vector3 rayDir = (target.transform.position -_npc.transform.position).normalized;
         float distance = Vector3.Distance(_npc.transform.position, target.transform.position);
         Vector3 crossResult = Vector3.Cross(_npc.transform.position, (rayDir * distance));
 
 
 var rightRayEndpoint = (rayDir * distance) + crossResult.normalized * 0.5f;
 var leftRayEndpoint = (rayDir * distance) + crossResult.normalized * -0.5f;

Any assistance in where im going wrong will be appreciated!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

user input azimuth and elevation angle. Need Urgent Help 0 Answers

Angle of bones to read the same as the inspector. 2 Answers

Aligning/mapping angles of Input.GetAxis(with .localEulerAngles 0 Answers

How to find direction Vector for a collision surface? 1 Answer

Rotate a gameobject by a specific angle and stop when character doesnt move anymore 0 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