- Home /
An Object Following Touch Position Problem
I know this is a common question but I searched and couldnt find an answer to my specific problem. I have an Object that Follows the touch position, my problem is that when I touch the screen with another finger while dragging the object the object tries to follow the new touch poistion and gets positiond between the two fingers instead of following the position of the first finger. this is my code for following the touch position:
function FixedUpdate () {
transform.position.z = 0;
if(Input.touchCount > 0){
if(Input.GetTouch(0).phase == TouchPhase.Began){
if(Input.touchCount == 1){
transform.position = cam.ScreenToWorldPoint(Input.GetTouch(0).position);
transform.position.z = 0;
}
}
}
}
Maybe I should mention that this object is not visible and theres another object that follows it using a joint.
it's possible this very long post could be relevant to you, pls vote it up if useful! :)
http://answers.unity3d.com/questions/292333/how-to-calculate-swipe-speed-on-ios.html
Answer by azmat786n · Dec 04, 2012 at 05:19 PM
//select your camera here
var mainCam:Camera;
//make screen into ray point
var touchPos: Ray = mainCam.ScreenPointToRay(Input.mousePosition);
function Update() {
if(Input.touchCount>0) {
transform.Translate(touchPos.origin.x * Time.deltaTime, touchPos.y * Time.deltaTime, 0);
}
}
i hope this is helpful.. :)
Should I switch Input.mousePosition with Input.GetTouch(0).position?
Inside the Editor you can test Touch positioning with mouse positioning, but when testing it on an actual touchscreen device there will never be mouse input whatsoever, so yes.
you can use mouse position on touch screen but you have to make camera screen point into ray like above script.. it tested on my device also feel easy to use it. :)
Answer by Toastfighter · Apr 11, 2014 at 10:18 PM
I have the same problem – any suggestions? The touch position is not on the right place. And for the code snippet from azmat786n i’ve got the following error message: 'y' is not a member of 'UnityEngine.Ray'. Have anyone an idea how can i fix this?
My code:
using UnityEngine; using System.Collections;
public class PlayerTouch : MonoBehaviour {
public float noSpeed = 0;
void Update ()
{
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
transform.Translate(-touchDeltaPosition.x * noSpeed, touchDeltaPosition.y, 0);
}
}
}
Answer by Vunpac · Nov 21, 2015 at 08:37 PM
I had this problem I solved it using the pointerID
int numberOfTouches = Input.touchCount;
if (numberOfTouches == 1)
{
touchID = Input.GetTouch(0).fingerId;
}
int index = 0;
for (int i = 0; i < nbTouches; i++)
{
if (Input.GetTouch(i).fingerId == touchID)
index = i;
}
transform.position = cam.ScreenToWorldPoint(Input.GetTouch(index).position);
Your answer

Follow this Question
Related Questions
Can you use a Vectrosity points array with iTween? 0 Answers
how make this script follow a target. 1 Answer
How to make the object player followsmooth2D the mouse or the touch? 3 Answers
Player not following touch after camera is rotated? 0 Answers
How to make 2 or more Objects to follow 2 or more fingers 1 Answer