- Home /
Question by
dicemanlol · Feb 13, 2017 at 08:14 AM ·
c#rotationscripting problem2d-platformerconversion
Problem with converting touch position to world space
Hello, I wanted to convert touch position to a world space point and then rotate a RigidBody towards it, I can't nail down why doesn't it work, I would appreciate your help greatly.
void Update ()
{
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
Plane plane = new Plane(Vector3.up, transform.position);
float distance = 0;
if (plane.Raycast(ray, out distance)){
Vector3 pos = ray.GetPoint(distance);
float rotZ = Mathf.Atan2 (pos.y, pos.x) * Mathf.Rad2Deg;
myBody.rotation = Quaternion.Euler (0f, 0f, rotZ + 90);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Multiple Cars not working 1 Answer
Rotate object around another object C# 2 Answers
HoloLens - Smooth Rotation doesn't work 0 Answers