4

我将创建 Android 应用程序,通过触摸在 X 和 Z 线上移动游戏对象,游戏对象必须向右、向左、向上或向下移动。我已经编写了这个属于 gameObject 的脚本,我在其中检查了 Raycast 的触摸。

您知道通过触摸移动对象的任何其他方法吗?

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

        } 
    }
} 

}

4

3 回答 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;
    }
于 2013-06-26T05:44:55.537 回答
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");
    }

    }
    }

您可以在以下链接中找到整篇文章 Move To Tap / Click Position - The Game Contriver

于 2014-10-12T04:15:25.637 回答
0

您可以使用它通过触摸移动对象

v3Pos = Vector3(touchDeltaPosition.x -50, touchDeltaPosition.y + 50, distance);
     transform.position = Camera.main.ScreenToWorldPoint(v3Pos);
于 2013-06-22T12:53:20.837 回答