Unity-限制相机移动

时间:2018-11-25 08:52:13

标签: unity3d swipe

我有2D项目,我希望能够左右左右移动相机...我找到了一个教程,通过滑动来移动相机,这样效果很好,但是如何设置移动相机的最大距离呢?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TouchController : MonoBehaviour
{
float touchStart = 0f;
Vector3 cameraDestination;
public float cameraSpeed = 0.1f;
Camera m_MainCamera;
// Use this for initialization
void Start()
{
    cameraDestination = Camera.main.transform.position;
}
// Update is called once per frame
void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        touchStart = Input.mousePosition.x;
    }
    if (Input.GetMouseButtonUp(0))
    {
        float delta = Input.mousePosition.x - touchStart;
        if (delta < -50f)
        {
            cameraDestination = new Vector3(Camera.main.transform.position.x + 500,
                Camera.main.transform.position.y, Camera.main.transform.position.z);
            // move the camera right
        }
        else if (delta > 50f){
            cameraDestination = new Vector3(Camera.main.transform.position.x - 500,
                Camera.main.transform.position.y, Camera.main.transform.position.z);
            // move the camera left
        }
    }
    if (Vector3.Distance(Camera.main.transform.position, cameraDestination) > 0.1f)
    {
        if (Camera.main.transform.position.x > cameraDestination.x)
        {
            Camera.main.transform.position = new Vector3(Camera.main.transform.position.x - cameraSpeed,
                Camera.main.transform.position.y, Camera.main.transform.position.z);
        }
        else
        {
            Camera.main.transform.position = new Vector3(Camera.main.transform.position.x + cameraSpeed,
                Camera.main.transform.position.y, Camera.main.transform.position.z);
        }
    }
}

}

1 个答案:

答案 0 :(得分:2)

您的问题尚不清楚,但是我想您想为相机的位置设置一个限制。为此,您需要2个点-最小点(x =允许相机具有的最小X值,y =允许相机具有的最小Y值)和最高点(相同,但代表上限)。

if (cameraDestination.x < maxPosition.x && cameraDestination.x > minPosition.x) //Ensures the camera's X value is within the allowed range
if (cameraDestination.y < maxPosition.y && cameraDestination.y > minPosition.y) //Ensures the camera's Y value is within the allowed range

然后,每次移动相机时,请检查以下条件,然后再移动:


sudo apt remove jekyll
sudo apt-get --autoremove
which jekyll
sudo rm /bin../jekyll