- Home /
Question by
Copenhagenn · Sep 01, 2017 at 07:06 AM ·
dragdraggingdrag objects
[HELP]Simplest Drag Script.
It's been a month since i start learning/creating drag script i already have 5 scripts already, they're working but they're complicated to edit with since i need to change some things to make it work it others
Can someone please give me a code of SIMPLEST DRAG script? What i am trying to achieve is. I have this 3D Object that i want to move in just Z and Y position in Touch. that's it.
Comment
Answer by LightningStorm102 · Jan 02, 2019 at 08:49 PM
Hello, @Copenhagenn!
Have you tried searching Google? There are a bunch of great answers out there :) Anyways, here's a script:
using UnityEngine;
using System.Collections;
public class TouchDrag : MonoBehaviour {
public float speed = 0.1F;
void Update() {
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
transform.Translate(0, touchDeltaPosition.y * speed * Time.deltaTime, touchDeltaPosition.z * speed * Time.deltaTime);
}
}
}