Unity如何使用相机停止移动对象

时间:2019-01-05 11:46:20

标签: unity3d camera 2d-games touchmove

我需要触摸相机移动的帮助。我有简单的2D游戏。我添加了背景作为精灵,它的工作正常。但是,当我在该背景上添加一些图像时,它不会随着相机移动。当我添加一些精灵而不是图像时,它随背景一起移动,因此效果很好。但是有什么区别,如何设置相机移动时图像不移动?以及如何设置一些随相机移动的精灵?希望这是可以理解的:)谢谢

我的移动背景脚本看起来像这样,但可能应该只是一些统一的组件设置...

using UnityEngine;
using UnityEngine.EventSystems;

public class TouchController : MonoBehaviour
{
    float touchStart = 0f;
    Vector3 cameraDestination;
    public float cameraSpeed = 0.1f;
    public float maxPosX = 2900f;
    public float minPosX = -2900f;
    public bool DragUI = false;

    public static bool IsPointerOverGameObject()
    {
        if (EventSystem.current.IsPointerOverGameObject())
            return true;

        if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began)
        {
            if (EventSystem.current.IsPointerOverGameObject(Input.touches[0].fingerId))
                return true;
        }
        return false;
    }
    void Start()
    {
        cameraDestination = Camera.main.transform.position;
    }
    void Update()
    {

        // Check if the mouse was clicked over a UI element
        if (IsPointerOverGameObject() && Input.GetMouseButtonDown(0))
        {
            Debug.Log("Clicked on the UI");
            DragUI = true;
        }
        if (Input.GetMouseButtonDown(0))
        {
            touchStart = Input.mousePosition.x;
        }
        if (Input.GetMouseButtonUp(0))
        {
            float delta = Input.mousePosition.x - touchStart;
            if (delta < -50f && !DragUI)
            {
                if (cameraDestination.x < maxPosX)
                {
                    cameraDestination = new Vector3(Camera.main.transform.position.x + 1600,
                    Camera.main.transform.position.y, Camera.main.transform.position.z);
                    // move the camera right
                    Debug.Log(cameraDestination.x + " cam dest x right");

                }
            }
            else if (delta > 50f && !DragUI)
            {
                if (cameraDestination.x > minPosX)
                {
                    cameraDestination = new Vector3(Camera.main.transform.position.x - 1600,
                    Camera.main.transform.position.y, Camera.main.transform.position.z);
                    Debug.Log(cameraDestination.x + " cam dest x left");
                }
            }
            else { DragUI = false;}
        }
        if (Vector3.Distance(Camera.main.transform.position, cameraDestination) > 0.1f && !DragUI)
        {
            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);
                DragUI = false;
            }
            else
            {
                Camera.main.transform.position = new Vector3(Camera.main.transform.position.x + cameraSpeed,
                    Camera.main.transform.position.y, Camera.main.transform.position.z);
                DragUI = false;
            }
        }
    }
}

0 个答案:

没有答案