Unity camera roaming and switching cameras

Create a camera follow script
Create a camera walk script
Create a camera switch script
Place walk and follow on public variables of the switch script
Insert image description here
Follow script

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

public class CameraScroll : MonoBehaviour
{
    
    
    public Camera cam; // 相机
    private float camFOV; // 相机视野
    public float zoomSpeed; // 缩放速度

    private float mouseScrollInput; // 鼠标滚动输入
    // Start is called before the first frame update
    void Start()
    {
    
    
        camFOV = cam.fieldOfView;
        // 获取到相机视野
    }

    // Update is called once per frame
    void Update()
    {
    
    
        // 获取到鼠标的滚轮的状态
        mouseScrollInput = Input.GetAxis("Mouse ScrollWheel");
        // 改变鼠标视野的范围值
        camFOV -= mouseScrollInput  * zoomSpeed;
        camFOV = Mathf.Clamp(camFOV, 30, 60); // 规划鼠标视野范围
        // 将鼠标视野改变的范围用线性插值求出
        cam.fieldOfView = Mathf.Lerp(cam.fieldOfView, camFOV, zoomSpeed);
    }
}

roaming script

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

public class CameraRoam : MonoBehaviour
{
    
    // 相机移动速度 屏幕尺寸厚度
    public float camSpeed = 20;
    public float screenSizeThickness = 10;

    void Update()
    {
    
    
        // 开始保存相机的位置
        Vector3 pos = transform.position;
        // 鼠标超出上边屏幕 相机向上移动
        if(Input.mousePosition.y >= Screen.height - screenSizeThickness)
        {
    
    
            pos.z += camSpeed * Time.deltaTime;
        }
        // 鼠标超出下边屏幕 相机向下移动
        if (Input.mousePosition.y <= screenSizeThickness)
        {
    
    
            pos.z -= camSpeed * Time.deltaTime;
        }
        // 鼠标超出右边屏幕 相机向右移动
        if (Input.mousePosition.x >= Screen.width - screenSizeThickness)
        {
    
    
            pos.x += camSpeed * Time.deltaTime;
        }
        // 鼠标超出左边屏幕 相机向左移动
        if (Input.mousePosition.x <= screenSizeThickness)
        {
    
    
            pos.x -= camSpeed * Time.deltaTime;
        }
        // 鼠标移动方向同步到相机
        transform.position = pos;
    }
}

switch script

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

public class CamSwitchManager : MonoBehaviour
{
    
    // 相机跟随 相机漫游 相机切换
    public CameraFollow CamFollowScript;
    public CameraRoam camRoamScript;
    bool camViewChanged = false;
    void Start()
    {
    
    // 可以切换到相机漫游
        camRoamScript.enabled = false;
    }

    // Update is called once per frame
    void Update()
    {
    
    
        
        if(camViewChanged == false)
        {
    
    // 点击E切换到场景漫游 让后可以切换到相机跟随
            if (Input.GetKeyDown(KeyCode.E))
            {
    
    
                camViewChanged = true;
                camRoamScript.enabled = true;
                CamFollowScript.enabled = false;
            }
        }
        else if(camViewChanged == true)
        {
    
    // 点击E切换到相机跟随 让后可以切换到场景漫游
            if (Input.GetKeyDown(KeyCode.E))
            {
    
    
                camViewChanged = false;
                camRoamScript.enabled = false;
                CamFollowScript.enabled = true;
            }

        }
    }
}

Guess you like

Origin blog.csdn.net/qq_60839745/article/details/128749095