• 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
Question by zero3growlithe · Jun 06, 2012 at 02:51 PM · rotationmeshnormalsface

Get face normal and get its local Euler Angles?

I have a simple script w$$anonymous$$ch checks using raycast and gets mesh face normals and what I want to do is to get it's local rotation so i can make my object rotated alike... is that possible? Here's the code I'm using to get face normals:

 private var Target : Transform;
 var Hit : RaycastHit;
 var layerMask = 1<<8;
 private var faceNormalZ : float = 0;
 private var faceNormalX : float = 0;
 
 function Start () {
     Target = GameObject.FindWithTag("Player").transform;
 }
 
 function Update (){
     transform.position = Target.position;
     transform.localEulerAngles.y = Target.localEulerAngles.y;
     transform.rotation.x = faceNormalX;
     transform.rotation.z = faceNormalZ;
     
     if (Physics.Raycast(transform.position, Vector3.down, Hit, 2, layerMask)){
     var meshCollider = Hit.collider as MeshCollider;}
         if (meshCollider == null || meshCollider.sharedMesh == null)
         return;
             
         var mesh : Mesh = meshCollider.sharedMesh;
         var vertices = mesh.vertices;
         var triangles = mesh.triangles;
         var normals = mesh.normals;
     
         P1 = vertices[triangles[Hit.triangleIndex * 3]];
         P2 = vertices[triangles[Hit.triangleIndex * 3 + 1]];
         P3 = vertices[triangles[Hit.triangleIndex * 3 + 2]];
             center = ((P1 + P2 + P3) / 3);
 
         P1 = normals[triangles[Hit.triangleIndex * 3]];
         P2 = normals[triangles[Hit.triangleIndex * 3 + 1]];
         P3 = normals[triangles[Hit.triangleIndex * 3 + 2]];
             faceNormal = ((P1 + P2 + P3) / 3);
             faceNormalZ = faceNormal.z;
             faceNormalX = -faceNormal.x;
 }
Comment

People who like this

0 Show 0
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
Best Answer

Answer by Berenger · Jun 06, 2012 at 03:01 PM

(Theory) You can't know the rotation of a vector, because it doesn't make sense. It need to be relative to somet$$anonymous$$ng. You might need confirmation of someone good in math, but I t$$anonymous$$nk there is an infinity of rotation from a vector to another, so I t$$anonymous$$nk you need 2 relative vectors.

Anyway, you probably want to use Quaternion.LookRotation.

Comment
zero3growlithe
Rallix

People who like this

2 Show 0 · 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

Answer by Mortoc · Jun 06, 2012 at 02:55 PM

You don't have to dig in to the mesh for t$$anonymous$$s, you can just use:

 someObject.transform.position = Hit.position;
 someObject.transform.up = Hit.normal;
Comment
Stardog
zero3growlithe
nights007

People who like this

3 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 zero3growlithe · Jun 06, 2012 at 05:38 PM 1
Share

well, not really as it won't give me "face" normal but Hit.point normal so if my raycast will hit another object under an angle then my object will rotate to that angle and not to face normal :/

avatar image Mortoc · Jun 06, 2012 at 05:45 PM 2
Share

Ah right, I misread the problem. You can still calculate the face normal by:

 Vector3.Cross(p2 - p1, p2 - p3).normalized

and then set someObject.transform.up to the result of that

avatar image

Answer by DHARMAKAYA · Nov 08, 2016 at 03:31 PM

Stop capping the first letter of vars!!!!! THAT IS THE PROBLEM HERE!!!!

(shaking fist in air)

Comment

People who like this

0 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 zero3growlithe · Nov 08, 2016 at 05:29 PM 1
Share

Haha, this question was asked 4 years ago, I've improved my C# coding skills by just "a lot" since then, dw :))

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Model faces arent showing up even though I reversed my normals. Anyone know what's wrong? 3 Answers

How would I rotate an object without moving one of its faces (locking it) 0 Answers

issues with mobile build 0 Answers

Mesh Normal 1 Answer

Meshes flipping on procedural generation 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