I'm going to create Android app where I move gameObject on X and Z cords by touching,gameObject have to move right, left, up or down. I have written this script which belongs to gameObject where I check the touching by Raycast.
Do you know of any another method to move 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';
}
}
}
}
}
}
}
take touch position store it and compare with current touch position calculate delta and move the object
if(Input.GetMouseButtonDown(0))
{
//print ("up...");
mousePositionXYZ.y= Input.mousePosition.y;
doit=true;
}
if(doit)
{
listGO=GameObject.Find("Main Camera");
if(Input.mousePosition.y > mousePositionXYZ.y )
{
listGO.transform.position.z -=Input.mousePosition.y *Time.deltaTime *0.010;
print ("up...");
}
else if(Input.mousePosition.y < mousePositionXYZ.y )
{
listGO.transform.position.z +=Input.mousePosition.y *Time.deltaTime*0.010;
print ("down...");
}
}
if(Input.GetMouseButtonUp(0))
{
mousePositionXYZ.y= Input.mousePosition.y;
doit=false;
}