- Home /
Touch position is off screen.
I have this code:
void Start () {
Control = GameObject.FindGameObjectWithTag ("TouchControl");
}
void Update () {
if (Input.touchCount > 0) {
Control.transform.position = Input.GetTouch (0).position;
Debug.Log (Input.GetTouch (0).position);
}
}
I want the Control Game Object to be moved wherever i touch on the screen, but it keeps going far off... Thanks for the help!
Answer by Vicarian · Aug 30, 2017 at 06:25 PM
The touch position is likely in screen space. You'll want to convert it to world space using Camera.ScreenToWorldPoint(Vector3), so:
if (Input.touchCount > 0) {
Control.transform.position = Camera.main.ScreenToWorldPoint(Input.GetTouch (0).position);
Debug.Log (Camera.main.ScreenToWorldPoint(Input.GetTouch (0).position));
}
I am getting: Assets/scripts/TouchScript.cs(16,45): error CS1061: Type UnityEngine.Camera' does not contain a definition for
ScreenToWorldSpace' and no extension method ScreenToWorldSpace' of type
UnityEngine.Camera' could be found. Are you missing an assembly reference?
(Do i just need to add a camera at the beginning?)
Apologies, flubbed the method call in the snippet, it's fixed.
Your answer
Follow this Question
Related Questions
Objects seemingly not being instantiated after touch. 1 Answer
Object following mouse touch on distance 1 Answer
unify touch position with player position 1 Answer
Two finger dragging/rotation of a sprite? 0 Answers
Multiple Cars not working 1 Answer