I have such a code. It moves the object along the Z axis, but it moves only in one direction no matter where the finger moves to the right to the left, but it is necessary that, depending on which way the finger moves! How to do it?
using UnityEngine; public class Follower : MonoBehaviour { #region ================================= PRIVATE FIELDS private readonly float _speed = 0.1f; private Vector2 _startPos; #endregion #region ============================== PRIVATE METHODS private void Update() { if (Input.touchCount > 0) { var touch = Input.GetTouch(0); switch (touch.phase) { case TouchPhase.Began: _startPos = touch.position; break; case TouchPhase.Moved: var dir = touch.position - _startPos; var pos = transform.position + new Vector3(transform.position.x, transform.position.y, dir.y); transform.position = Vector3.Lerp(transform.position, pos, Time.deltaTime * _speed); break; } } } #endregion }
var pos = transform.position + new Vector3(transform.position.x, transform.position.y, dir.y);Do you take the y-coordinate ofdir.y, and not the x-coordinate? - Alexander Danilovsky