- Home /
Touch point to world coordinate and local coordinate
How do you convert a touch point to world coordinate and local coordinate?
Answer by mkovac · Mar 20, 2012 at 09:38 AM
Beware that 2D touch point when transformed to 3D scene becomes a ray rather than single point. This ray hits everything painted on the touched screen point. You will have to do a raycast to see what exactly was hit and where
Touch position can be translated to ray as follows:
Ray ray = Camera.main.ScreenPointToRay( Input.touches[0].position );
This ray is in world space and if you want to know what it hits (i.e. what is touched on screen with finger), do a raycast. If something was hit, exact hit position in world space will be stored in hitInfo:
RaycastHit hitInfo;
if (Physics.Raycast( ray, out hitInfo ))
{
Vector3 worldSpaceHitPoint = hitInfo.point;
}
If you want to know local position of hit on object which was hit by raycast, you will simply transform worldSpaceHitPoint into object's local space. The hit object is also stored in hitInfo:
Vector3 localSpaceHitPoint = hitInfo.transform.worldToLocalMatrix.MultiplyPoint( worldSpaceHitPoint );
Your answer
![](https://koobas.hobune.stream/wayback/20220613024241im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Local vs Global Transform 1 Answer
Transform global quaternion from local quaternion 2 Answers
Camera rapidly changes it's position when the parent changes. 0 Answers
Clamping Velocity.z when touching an object (Major Edit) 1 Answer
Simulating different gravity, how to make falling platform fall in the desired direction? 1 Answer