Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Eeppi · Jan 09, 2013 at 03:50 PM · rotationraycastsurface

How I can know surface angle?

Hi, I have to know rotation of surface i'm looking, but I dont know how I should implement it? I'm not searching complete code, but even idea how I could do that?

alt text

Like this, arrows are showing surfaces, I need to know rotation of surface i'm looking. I was thinking multiple raycasts, but not sure how to implement that. Any idea?


EDIT:

New picture (I'm bad at describing, sorry!), maybe it helps someone.

alt text

^ This raycast should find angle information.

surfaces.png (8.8 kB)
surface.png (13.5 kB)
Comment
Add comment · Show 6
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 gardian06 · Jan 09, 2013 at 04:09 PM 0
Share

since a RaycastRit contains a normal you could use that to deter$$anonymous$$e the angle of the face being hit, but these normals are only really useful for "simple" geometries.

avatar image Eeppi · Jan 09, 2013 at 04:39 PM 0
Share

How I can access that angle? I'm pretty new with raycasting since I have used it only with really simple things.

avatar image Statement · Jan 09, 2013 at 05:02 PM 0
Share

Do you mean the normal or the angle around Z axis? Do you mean the angle relative to the connecting edge or an absolute angle? What is the sort of value that you want to derive for the bottom, sloped and right edge for example?

avatar image Eeppi · Jan 09, 2013 at 05:44 PM 0
Share

I dont really know why someone added negative vote to my question, I can add more information if it's needed and that's why there is comment box. I didn't find good answer using search since it's not really general question. @Statement ♦♦: I mean normal, object z-axis isn't enought since it's same on every surface. I added new image to first post, maybe it helps.

avatar image gardian06 · Jan 09, 2013 at 06:09 PM 0
Share

I am still unclear as to what this will be used for: pathfinding, ricochets, other. what? if it is pathfinding or ricochets there are easier methods then raycasting, but maybe application would help.

Show more comments

2 Replies

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

Answer by Loius · Jan 09, 2013 at 06:10 PM

A raycast hit contains the ".normal" attribute, which is a Vector3 representing the hit plane's normal (the direction its "outside" face is facing).

You can use Vector3.Angle(normal, yourRay) to get the angle between those, if that's what you're looking for.

Comment
Add comment · Show 4 · 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 Wolfram · Jan 09, 2013 at 06:12 PM 2
Share

You'll need -yourRayDirection ins$$anonymous$$d of yourRay, otherwise the value returned is 180°-angle. Also note that smooth surface normals and probably also bumpmaps will modify the normal, and therefore the angle.

avatar image Eeppi · Jan 10, 2013 at 02:47 PM 0
Share

Thanks for the answer. I'm probably idiot, but I can't get this working. $$anonymous$$y code is like this now:

var hit : RaycastHit;

var direction = transform.TransformDirection(Vector3.forward);

var ray = transform.position;

var normal: Vector3 = Vector3(hit.normal.x, hit.normal.y, hit.normal.z);

if(Physics.Raycast(ray,direction,hit)){

var test = Vector3.Angle(normal,ray);

Debug.Log(test);

}

avatar image Wolfram · Jan 10, 2013 at 02:50 PM 2
Share

As I've written above, you need to use "-direction", not "ray". Also, directly use hit.normal ins$$anonymous$$d of creating a local variable, it's faster. And currently you're assigning "normal" before calling Physics.Raycast, so the value will be incorrect.

avatar image Wolfram · Jan 10, 2013 at 02:51 PM 2
Share

Also note that "transform.TransformDirection(Vector3.forward)" is simply "transform.forward", which is also faster.

avatar image
1

Answer by hotozi · May 23, 2019 at 08:14 PM

Don't break your head i found the simplest and best answer for calculation... RaycastHit has a transform in which it is linked to the collider once it is called

 Rayhit.transform.eulerAngles

''

 var Rayhit: RaycastHit;
 var HitRay= Physics.Raycast(transform.position,transform.forward,Rayhit,1);
 if(HitRay)
 print(Rayhit.transform.eulerAngles.y);

''

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 Bunny83 · May 24, 2019 at 12:15 AM 3
Share

What does the object rotation on the y axis has to do with the surface angle? Yes, if your object is aligned with the principal axis you can work out the surface angle from the object rotation. However if you have a look at the OPs image the object has several different surfaces at different angles. So your answer is not really simpler nor the best answer. The best answer was already given by Loius over 6 years ago.


Apart from that you should avoid giving example code in UnityScript as it has been deprecated long time ago and is no longer supported in the latest Unity versions.


ps: i removed your second answer on the other similar question. Please do not bump old questions which already have been answered. Especially don't spam old outdated code to somewhat similar questions. Take the time to read the question and answer that question.

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

14 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

Related Questions

How to make an object rotate to raycast hit from mouse? Unity 3D 1 Answer

How to get a vector rotation? 1 Answer

Steering while following terrain surface normal 0 Answers

How to keep local rotation when using raycasts? 1 Answer

Can't figure out how to make my raycast obstacle avoidance less jittery 3 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