- Home /
Object is not following the touch position
I have a problem – any suggestions? I knew, that there are many topics to this problem. But i can't figured out what's wrong. I want to move an object with touch. If i touch and move to left, the position of my object is not on the same place like my finger/touch. The touch position is not on the right place. Have anyone an idea how can i fix this?
// edit: Actually my code do nothing. I can’t move my object right now.
using UnityEngine;
using System.Collections;
public class PlayerTouch : MonoBehaviour
{
// Use this for initialization
/*void Start ()
{
}*/
// Update is called once per frame
void Update () {
if (Input.touchCount >= 1)
{
foreach(Touch touch in Input.touches)
{
Ray ray = Camera.main.ScreenPointToRay(touch.position);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, 100)){
transform.position = new Vector3(touch.position.x, transform.position.y, touch.position.y);
}
}
}
}
}
A Touch.deltaPosition is a 2D screen coordinate. Your game objects live in 3D world coordinates. There are different techniques for converting between the two depending on your camera settings and rotation. You might be better off searching out the many posts on Draging and Dropping an object on Unity Answers and modifying them for your use.
I’ve found this issue… but can’t solve it :( http://answers.unity3d.com/questions/621539/how-can-i-drag-the-object-with-touch-mobile.html
Have somebody an idea?
I’ve found this issue… but can’t solve it
This doesn't tell us much. Either update your question to include the latest code attempt along with any problems you are having, or open a new question with the new code. Without the code and a clear description of the problem, there is not much we can do.
Sorry robertbu, you’re right. I’ve updated my code right now.
Your answer
![](https://koobas.hobune.stream/wayback/20220613142740im_/https://answers.unity.com/themes/thub/images/avi.jpg)