我将创建Android应用程序,我通过触摸在X和Z线上移动gameObject,gameObject必须向右,向左,向上或向下移动。我写了这个属于gameObject的脚本,我在那里检查了Raycast的触摸。
您是否知道通过触摸移动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';
}
}
}
}
}
}
}
答案 0 :(得分:1)
采取触摸位置存储它并与当前触摸位置进行比较 计算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;
}
答案 1 :(得分:0)
您可以使用它来移动带触摸的对象
v3Pos = Vector3(touchDeltaPosition.x -50, touchDeltaPosition.y + 50, distance);
transform.position = Camera.main.ScreenToWorldPoint(v3Pos);
答案 2 :(得分: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