- Home /
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;
}
}
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);
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.
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
Follow this Question
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