Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Vivekveri · Mar 26, 2018 at 07:22 AM · raycastingtrigonometryvector3 operations

How to get the angle between an object and a plane based on the plane's normal?

I have an gun which raycasts onto a plane and I want to find the angle between the plane and the object based on the normal of the raycast hit. Also, I want to isolate the pitch and yaw seperately.

Currently I use this approach to find the pitch and yaw separately but it is not relative to the normal.

 Vector3 targetDir=raycastHitPos - gunNozzle.transform.position;
 float pitchAngle = Vector3.SignedAngle(new Vector3(0, targetDir.y, 0),gunNozzle.transform.forward, gunNozzle.transform.right);
 float yawAngle = Vector3.SignedAngle(new Vector3(targetDir.x, 0, 0), gunNozzle.transform.forward, gunNozzle.transform.up);

I tried

 Vector3 targetDir = Vector3.Cross(gunNozzle.transform.forward.normalized,normal.normalized);

but it doesn't work :/. How do I fit the raycast hitpoint normal into this code? Fairly new to Vector math so please guide me on how to achieve this! Thank You!

EDIT 1:

This image shows my current implementation and what I need

The yellow arrow is the facing direction and the green arrow is the normal of the red plane. These are the values which I get when facing exactly opposite to the normal. The second image shows the values which I want. I want the same angles whichever way I face the plane(or the plane is facing). The values are only for representation purposes, I'm fine with any range!

I've also tried
Reflecting the vector and then trying to find the angle between the hitpoint and the reflected vector but it only gives a value of either 180 or 0.

I also want to use Vector3.SingedAngle() because I want negative values!

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

3 Replies

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

Answer by Vivekveri · Mar 27, 2018 at 01:29 PM

I Found the solution, it's only for pitch currently, will update it once I get the Yaw sorted out! I figured it would be easier to find the angle between the normal and the gun's nozzle but with only y and z values (since I didn't want to take yaw into account) and surprisingly, it works!

I shall accept this as an answer and will get back if i face problems with yaw

This is the code for the pitch only angle

 float pitchAngle = Vector3.SignedAngle(normal, new Vector3(0, gunNozzle.transform.forward.y, gunNozzle.transform.forward.z), gunNozzle.transform.right);

Thank you for everyone who answered, commented and saw this question!

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
2

Answer by tormentoarmagedoom · Mar 26, 2018 at 08:12 AM

Good day.

I just did the same to create a "compass". I used the function Vector3.Angle , you need two vectors, and you get the angle between them. Think about which vector defines your plane (0,1,1), or (1,0,1) or (1,1,0). Check the API and sure you will get it!

 If helped, accept the answer! :D

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 Vivekveri · Mar 26, 2018 at 10:36 AM 0
Share

@tormentoarmagedoom - Thank you for the reply, but I want to use SignedAngle() because I need negative values! Your answer did help me to try out something new, but it did not work for me :/, Sorry.

avatar image tormentoarmagedoom Vivekveri · Mar 26, 2018 at 04:02 PM 0
Share

Ah yea is true! it only gives positive angles from 1 to 180.

I had to create a system to know if was to right or to left. By creating the vector between the two original vectors, i can know in wich direction is (top-right, top-left, bot-right or bot-left)

 OriginalVector1-OriginalVector2 = DiferenceVector.

If DiferenceVector.x >0 means is top, if < 0 means is bot If DiferenceVector.y >0 means is right, if <0 means is left.

Thats what i did :D

avatar image
0

Answer by Firas4d · Mar 26, 2018 at 03:07 PM

The cross operation you did will always result in a third vector that is perpendicular to both operand vectors and parallel to the plane surface which will later play a big role deciding the sign of the angle if you used it as a third parameter inside SignedAngle method as following : Vector3.SignedAngle(objectForwardVector, planeNormal, crossResult);

So if we applied that on your example image then you will get the signed angle between the green and yellow arrows which will be 180 or -180 degrees (depends on the direction of the cross product result) and will never be 90, but at least you will get a result that is always relative to the plane normal.

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 Vivekveri · Mar 27, 2018 at 01:24 PM 0
Share

Thank you! it does work, but not quite how I wanted it, I found a go-to solution! Thank you! :)

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

80 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

calculate Vector3 offset relative to surface normal 1 Answer

FOV that brings gameObjects close 2 Answers

Is it possible to raycast multiple objects of the same tag (Enemy) 1 Answer

problem with raycasts and imported mesh 0 Answers

how to access variables in a function script attached to a clone 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