Unity实现相机漫游功能

创建一个相机,挂载这个脚本即可

public class FilmPlaybackMgr
{
    Camera wanderCamera;
    bool isWanderCam = false;
    int CameraIndex = 1;
    /// 
    /// 玩家输入控制
    /// 
    Vector2 userInput = Vector2.zero;
    Vector2 mouseDelta = Vector2.zero;
    /// 
    /// 鼠标移动灵敏度
    /// 
    float sensitivity = 2;
    /// 
    /// 移动中平滑
    /// 
    float smoothing = 1.5f;
    Vector2 velocity, frameVelocity;
    bool isRunning = false;
    private void Start()
    {
        wanderCamera = GetComponent();
    }
    private void Update()
    {
        GetUserInput();
    }
    void GetUserInput()
    {
        userInput.x = Input.GetAxis("Horizontal");
        userInput.y = Input.GetAxis("Vertical");
        mouseDelta.x = Input.GetAxisRaw("Mouse X");
        mouseDelta.y = Input.GetAxisRaw("Mouse Y");
    }
    public float wanderCameraMoveSpeed = 10;
    [HideInInspector]
    public float wanderCameraRunSpeed=20;
    void WanderCameraCtrl()
    {
        float moveSpeed= isRunning ? wanderCameraRunSpeed : wanderCameraMoveSpeed;
        userInput = userInput * Time.deltaTime * moveSpeed;
        wanderCamera.transform.Translate(userInput.x, 0, userInput.y);
        Vector2 rawFrameVelocity = Vector2.Scale(mouseDelta, Vector2.one * sensitivity);
        frameVelocity = Vector2.Lerp(frameVelocity, rawFrameVelocity, 1 / smoothing);
        velocity += frameVelocity;
        wanderCamera.transform.localRotation = Quaternion.Euler(-velocity.y, velocity.x, 0);
    }
}

简单的相机漫游就完成了,如果有问题欢迎大家评论里面提出来哦,

你可能感兴趣的:(Unity,相机漫游,unity,相机漫游)