【问题标题】:Camera 2D Movement android unity相机 2D 运动 android unity
【发布时间】:2014-08-15 08:26:47
【问题描述】:

我正在触摸我的 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);
    }
}
}

感谢您的帮助

【问题讨论】:

    标签: android camera unity3d


    【解决方案1】:

    移动后只需夹紧位置即可停止。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多