我正在触摸我的Android设备中的相机并且它随触摸移动但我想限制其在X轴和Y轴上的移动,这是允许我移动相机的代码。
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour
{
public float speed = 0.1F;
void Update() {
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
transform.Translate(-touchDeltaPosition.x * speed * Time.deltaTime, - touchDeltaPosition.y * speed * Time.deltaTime, 0);
}
}
}
感谢您的帮助
答案 0 :(得分:0)
移动后只需夹住位置即可将其停止。
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
transform.Translate(-touchDeltaPosition.x * speed * Time.deltaTime, - touchDeltaPosition.y * speed * Time.deltaTime, 0);
transform.x = Mathf.Clamp(transform.x, minimumX, maximumX);
transform.y = Mathf.Clamp(transform.y, minimumY, maximumY);
}