as code to move the object left and right. I’ve been googling a bit and found something about input.touch or so but nothing to get me going. I’d like some help on how to make this game so that when a player touches the object it will move along with the finger on the x axis. (so you can “slide” the object from left to right)
void Update () {
if (Input.touchCount > 0) {
// The screen has been touched so store the touch
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved) {
// If the finger is on the screen, move the object smoothly to the touch position
Vector3 touchPosition = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 10));
touchPosition.y = transform.position.y;
transform.position = Vector3.Lerp(transform.position, touchPosition, 20 * Time.deltaTime);
}
}
}
This is the only script with which my object doesn’t disappear. If you wish to enable movement on the y axis as well just delete “touchPosition.y = transform.position.y;”
If you wish to increase the speed at which the object follows the touch just increase the number 20, for a lower speed you can ofcourse decrease the number.