- Home /
[C#] How to transform world space to local space?
I'm trying to transform vector3.up, vector3.right, gameobject.transform.up etc from world into local so that I can shoot rays in those directions relative to a gameobjects rotation. I've been trying to do so using transform.InverseTransformPoint without success. Its shooting four rays in close to the same direction, none of which are the correct direction.
The problem may be that raycasts expect inputs in world space. So shooting a ray based on an object's rotation usually involves changing from local to world. Not world to local. Yes, it is confusing when you think too much about it.
For example, say you have Raycast(transform.right, ...)
. "right", or (1,0,0), is the direction your local space, which is useless as it is. transform.right
converts to the real, world, direction. It's all in one line, so looks semi-magical. But it's just math, then a raycast, like any other function call.
Answer by tanoshimi · Nov 19, 2014 at 07:00 AM
Every Transform component has its own local up, right, forward vectors - transform.up, transform.right, and transform.forward.
To convert an arbitrary vector from world to local or back, use TransformDirection and InverseTransformDirection: http://docs.unity3d.com/ScriptReference/Transform.InverseTransformDirection.html