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 legion_44 · Jan 08, 2014 at 04:49 PM · c#raycastmeshvector3face

Get forward direction of face hitted by Raycast.

Hi all. I want to get forward direction (Vector3) of face, that I hitted by Raycast. Like transform.forward but not forward of transform but forward of face (or tris). I'm using C#. If you need additional info, please ask.

EDIT2: What I mean by 'face forward'. Ex.: When You select Transform on scene (and have axis selected on 'Local'), You see blue arrow indicating Z axis - object forward. I need the same but for face.

EDIT1: Here is some code, hope that will give you better look at what i need.

 RaycastHit hit;
 Ray ray;
 Transform t;
 
 void Update()
 {
   if(Input[...])
   {
     if(Physics.Raycast(ray, out hit, 100))
     {
       clone = Intantiate(t, hit.point, Quaternion.identity)
       clone.LookAt(HERE I NEED FORWARD DIRECTION OF FACE);
     }
   }
 }

Thanks in advance, Paul

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
1
Best Answer

Answer by robertbu · Jan 08, 2014 at 04:53 PM

You don't provide your code, so I'm unsure of what info you need. In order to find the direction of the face, you need to use a form of Raycast() that takes a RaycastHit parameter. Then you can use the RaycastHit.normal to find the normal to the face. Depending on what you define as 'forward' you may need to negate the value.

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 legion_44 · Jan 08, 2014 at 05:14 PM 0
Share

Hi, edited question with code. Hope that gives you better look. One more q: do I have to specify hitted face to use RaycastHit.normal? Or it's automatically recognize face that Raycast hit?

avatar image robertbu · Jan 08, 2014 at 05:44 PM 0
Share

There are a number of problems with the code you posted, but I get the general idea. Transform.LookAt() takes a position in 3D space. For your use here, you can use Quaternion.LookRotation().

 Intantiate(t, hit.point, Quaternion.LookRotation(hit.normal));

If for some reason you wanted to use LookAt(), you could do it this way:

 clone.LookAt(transform.position + hit.normal);

Or you can assign the rotation:

 clone.rotation = Quaternion.LookRotation(hit.normal);

Both of these assume you want to look away from the face. If you want to look towards the face, negate hit.normal.

Final note, both LookAt() and LookRotation() have an optional second parameter that defines 'up' for the rotation. Depending on how you are using this code, you may need this second parameter.

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

Multiple Cars not working 1 Answer

How to Instantiate a prefab on the surface of a curved structure or mesh 1 Answer

Door only opening once on Raycast 1 Answer

Modify mesh problems 1 Answer

Rotating a raycast around the x,z axis 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