- Home /
Touch input not working properly
So i'm just starting to learn touch controls in unity. I want to move an object in world space to the position on screen where i touched it. This is my code used from Brackey's tutorial. But it only works once per play session. Any idea why?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TouchToMove : MonoBehaviour {
// Update is called once per frame
void Update ()
{
if(Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
Vector3 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
touchPosition.z = 0f;
transform.position = touchPosition;
}
}
}
Answer by Bieere · Aug 11, 2019 at 05:03 PM
Could you add a Debug.Log() in the editor to ensure that the touch position isn't the same every single frame? You might be moving it, but always to the same location.
Ok so i solved the problem and it was just a matter of kinda changing the sequence of the code with $$anonymous$$or mods.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TouchTo$$anonymous$$ove : $$anonymous$$onoBehaviour {
// Update is called once per frame
void Update ()
{
//Working
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
Vector3 touchPosition = touch.position;
touchPosition.z = 10f;
transform.position = Camera.main.ScreenToWorldPoint(touchPosition);
}
}
Answer by abssuper20 · Aug 12, 2019 at 04:49 AM
@Bieere i added one like you said and each time the position is different for different areas of the screen touched. Also I noticed that the ScreenToWorldPoint method is causing a problem as for any touch coordinates, it's being converted to the same Vector3.
Your answer
Follow this Question
Related Questions
When applying a force to an object on Touch, how do I add a *cooldown* to it? 0 Answers
What version of unity works with the Samsung Gear VR innovator edition for Note 4? 1 Answer
How to recreate shape shifters 3d mechanic 0 Answers
Unity DJ your game Veemix! 1 Answer
Triggers not working after building game on mobile 0 Answers