I am going to create an Android application where I move the gameObject to the X and Z cords, touching, the gameObject should move right, left, up or down. I wrote this script, which belongs to gameObject, where I check the touch of Raycast.
Do you know of any other method of moving an object by touching?
#pragma strict var hit = new RaycastHit(); function Start () { } function FixedUpdate () { for (var i :int = 0; i < Input.touchCount; ++i) { if (Input.GetTouch(0).phase == TouchPhase.Moved ) { var touchDeltaPosition:Vector2 = Input.GetTouch(i).deltaPosition; var touch_pos : Vector3 = new Vector3(Input.GetTouch(i).position.x, Input.GetTouch(i).position.y , 0); var ray : Ray = Camera.main.ScreenPointToRay (touch_pos); Debug.DrawRay (ray.origin, ray.direction * 10, Color.yellow); if (Physics.Raycast (ray, hit)) { Debug.Log(hit.transform.tag); if(hit.transform.tag == "Cubes") { if(Mathf.Abs(Input.GetTouch(i).deltaPosition.x) > Mathf.Abs(Input.GetTouch(i).deltaPosition.y)) { if(Input.GetTouch(i).deltaPosition.x > 0) { transform.Translate(0,0,1); // 'right'; } else { transform.Translate(0,0,-1); //GUItest.text = 'left'; } } else { if(Input.GetTouch(i).deltaPosition.y > 0) { transform.Translate(-1,0,0); // 'up'; } else { transform.Translate(1,0,0); // 'dawn'; } } } } } }
}
source share