我将创建 Android应用程序,我通过触摸在X和Z线上移动gameObject,gameObject必须向右,向左,向上或向下移动.我写了这个属于gameObject的脚本,我在那里检查了Raycast的触摸. 你知道通过触摸移动Ob
你知道通过触摸移动Object的任何其他方法吗?
#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'; } } } } } }
}
采取触摸位置存储它并与当前触摸位置进行比较计算delta并移动对象
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; }