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
1
Question by Rsan · Jan 15, 2012 at 04:28 AM · raycastnormalslocalglobal

How do I return local normals?

I'm using a raycast to return the normal of the surface it hits, but I understand that it's in world coordinates. If I rotate the cube and fire the raycast at the same face, it returns a different value.

Is there a way to return normals in the local coordinates of the object? I'm sorry if the wording in this question is a little confusing.

Edit
The script I'm working on:

 private var ray : Ray;
 private var hit : RaycastHit;
 
 var localNormal : Vector3;
 
 function Update ()
 
 {
 
     ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             
     if (Physics.Raycast (ray, hit)) 
 
         {          
         
             var localNormal = hit.transform.InverseTransformDirection(hit.normal);
             
             Debug.Log(localNormal);
                     
             transform.position = hit.point;       
                       
         }
         
 }
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
2

Answer by aldonaletto · Jan 15, 2012 at 04:57 AM

You can convert the normal to local coordinates with transform.InverseTransformDirection. Supposing hit is the RaycastHit used in Raycast, you can do the following:

 var localNormal = hit.transform.InverseTransformDirection(hit.normal);


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 Rsan · Jan 15, 2012 at 05:30 AM 0
Share

I implemented it into the script, but now it's returning a single value for each object, regardless of which face the raycast hits. I'll paste my script into the original question.

avatar image aldonaletto · Jan 15, 2012 at 01:54 PM 0
Share

I tested this script: whenever the mouse pointer is over the owner object, the object starts moving towards the camera, thus you can't even click in another face. This happens because you're continuously raycasting, thus each time you hit a face the object is moved to the hit point (a little closer to the camera) - and it's hit again in the next update, and moved to the new hit point, and so on. Only moving the mouse out of the object you may have a chance to stop the process.
I changed the Update function a little: now it only does the raycast when the left mouse button is pressed. With this arrangement, you can clearly see that the normal returned is local:

function Update ()
{
    if (Input.Get$$anonymous$$ouseButtonDown(0)){
        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast (ray, hit)) 
        {          
            var localNormal = hit.transform.InverseTransformDirection(hit.normal);
            Debug.Log(localNormal);
            transform.position = hit.point;       
        }
    }
}

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Moving A Ray Relative To Parent, Not World 1 Answer

How to open and close a door parented to a rotating object? 0 Answers

rotate 90% in local space 4 Answers

Accessing variables from seperate scripts 1 Answer

Local Velocity for Rigidbody? 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