How to implement the control so that the object moves by the finger?

  • Not enough information). Explain in more detail what you want to receive. What is your 2D or 3D game? In general, the task is not complicated, everything is solved with the help of a simple script, but again, such a touch ... maybe a few touches, do you need to process only one first touch? - KingPeas
  • I need to process 1 touch. A 3D game with a camera on top, you need an object to move along the x and y axes - Aimon Z.
  • * x and z. Coordinates - Aimon Z.

1 answer 1

Touch returns the coordinate in pixels. All you need to translate it into world coordinates. If you know at what distance you have a camera from the object, then calmly transfer the point of the screen to the world coordinates of Camera.ScreenToWorldPoint

I usually do in such cases as follows. I create an empty object, it will be our goal where we need to move. When we catch the touch we calculate the position in which we should have an object and move our target object to this point. And then we make the move to the goal. If you just need the object to move to a given point without logic along the shortest path, then I use the following script:

public class FollowMe : MonoBehaviour { public Transform Target = null; public bool Smoothing = true; public float TimeMoving = 0.5f; public Vector3 Scale = Vector3.one; // Use this for initialization private void OnEnable() { /*if (Target) { transform.position = Target.position; transform.rotation = Target.rotation; }*/ } // Update is called once per frame void Update() { if (Target != null) { if (Smoothing) { transform.position += ReScale(Vector3.Lerp(transform.position, Target.position, Time.deltaTime / TimeMoving) - transform.position); transform.rotation = Quaternion.Lerp(transform.rotation, Target.rotation, Time.deltaTime / TimeMoving); } else { transform.position += ReScale(Target.position - transform.position); transform.rotation = Target.rotation; } } } private Vector3 ReScale(Vector3 change) { change.Scale(Scale); return change; } }