2013-06-22 182 views
4

我要创建Android应用程序,通过触摸移动X和Z线上的gameObject,gameObject必须向右,向左,向上或向下移动。我写了这个剧本属于游戏对象在哪里检查由光线投射触摸。unity3d通过触摸移动

你知道的任何其他方法的触摸移动对象?

#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'; 
        } 
       } 
      } 

     } 
    } 
} 

}

回答

0

,你可以用它来与触摸移动对象

v3Pos = Vector3(touchDeltaPosition.x -50, touchDeltaPosition.y + 50, distance); 
    transform.position = Camera.main.ScreenToWorldPoint(v3Pos); 
1

采取触摸位置店,并与当前触摸位置比较 计算三角洲和移动对象

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; 
    } 
0

从游戏发明者该脚本执行的Vector3使用相同.Lerp并以恒定的速度无论这两个点之间的distancce的..

using UnityEngine; 
using System.Collections; 

public class TapToMove : MonoBehaviour { 
//flag to check if the user has tapped/clicked. 
//Set to true on click. Reset to false on reaching destination 
private bool flag = false; 
//destination point 
private Vector3 endPoint; 
//alter this to change the speed of the movement of player/gameobject 
public float duration = 50.0f; 
//vertical position of the gameobject 
private float yAxis; 

void Start(){ 
    //save the y axis value of gameobject 
    yAxis = gameObject.transform.position.y; 
} 

// Update is called once per frame 
void Update() { 

    //check if the screen is touched/clicked 
    if((Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) || (Input.GetMouseButtonDown(0))) 
    { 
     //declare a variable of RaycastHit struct 
     RaycastHit hit; 
     //Create a Ray on the tapped/clicked position 
     Ray ray; 
     //for unity editor 
     #if UNITY_EDITOR 
     ray = Camera.main.ScreenPointToRay(Input.mousePosition); 
     //for touch device 
     #elif (UNITY_ANDROID || UNITY_IPHONE || UNITY_WP8) 
     ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position); 
     #endif 

     //Check if the ray hits any collider 
     if(Physics.Raycast(ray,out hit)) 
     { 
      //set a flag to indicate to move the gameobject 
      flag = true; 
      //save the click/tap position 
      endPoint = hit.point; 
      //as we do not want to change the y axis value based on touch position, reset it to original y axis value 
      endPoint.y = yAxis; 
      Debug.Log(endPoint); 
     } 

    } 
    //check if the flag for movement is true and the current gameobject position is not same as the clicked/tapped position 
    if(flag && !Mathf.Approximately(gameObject.transform.position.magnitude, endPoint.magnitude)){ //&& !(V3Equal(transform.position, endPoint))){ 
     //move the gameobject to the desired position 
     gameObject.transform.position = Vector3.Lerp(gameObject.transform.position, endPoint, 1/(duration*(Vector3.Distance(gameObject.transform.position, endPoint)))); 
    } 
    //set the movement indicator flag to false if the endPoint and current gameobject position are equal 
    else if(flag && Mathf.Approximately(gameObject.transform.position.magnitude, endPoint.magnitude)) { 
     flag = false; 
     Debug.Log("I am here"); 
    } 

    } 
    } 

您可以通过以下链接 Move To Tap/Click Position - The Game Contriver

整篇文章