Unity3D 触摸移动

4
我将创建一个Android应用程序,通过触摸移动gameObject的X和Z坐标,gameObject必须向右、向左、向上或向下移动。我编写了此脚本,它属于检查Raycast触摸的gameObject。
您知道其他通过触摸移动对象的方法吗?
#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';
                    }
                }
            }

        } 
    }
} 

}

3个回答

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

The Game Contriver的脚本使用Vector3.Lerp实现相同的效果,而且速度恒定,不受两点之间距离的影响。

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");
    }

    }
    }

您可以在以下链接找到整篇文章 移动到点击位置 - The Game Contriver

0

你可以使用这个来通过触摸移动物体

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接