Unity 3D Game Engine – Android – Touch Drag – Moving a 3D Object – JS
Inside Hierarchy create:
– Cube (Game Object), position X=0 Y=0.5 Z=0
– Main Camera, move it to see the Cube from top; position X=0 Y=8 Z=0, rotation X=90 Y=0 Z=0, attach the script ‘TouchMove.js’
TouchMove.js
var object : GameObject; function Update () { for (var touch : Touch in Input.touches){ var ray = Camera.main.ScreenPointToRay(touch.position); var hit : RaycastHit; if (Physics.Raycast (ray, hit, 100)) { if(touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved) { var cameraTransform = Camera.main.transform.InverseTransformPoint(0, 0, 0); object.transform.position = Camera.main.ScreenToWorldPoint(new Vector3 (touch.position.x, touch.position.y, cameraTransform.z - 0.5)); } } } }
Hieararchy> Main Camera> Inspector> TouchMove.js> DRAG AND DROP Cube (Game Object) over public var object