• CinemachineVirtualCamera实现自由相机


    在这里插入图片描述

    using Cinemachine;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class FreeCameraController : MonoBehaviour
    {
        public CinemachineVirtualCamera vcamera;
        public Transform target;
        public float TopClamp = 85.0f;
        public float BottomClamp = -10.0f;
        public float Speed = 3.0f;
        public bool Invert;
    
        private float cinemachineTargetYaw;
        private float cinemachineTargetPitch;
    
        private Cinemachine3rdPersonFollow vcameraFollow;
        private float vcameraDistance;
        void Start()
        {
            cinemachineTargetPitch = target.rotation.eulerAngles.x;
            cinemachineTargetYaw = target.rotation.eulerAngles.y;
    
            vcameraFollow = vcamera.GetCinemachineComponent<Cinemachine3rdPersonFollow>();
            vcameraDistance = vcameraFollow.CameraDistance;
        }
    
        void LateUpdate()
        {
            CameraRotation();
        }
    
        private void CameraRotation()
        {
            var input = new Vector2(Input.GetAxis("Mouse X"), -Input.GetAxis("Mouse Y"));
            bool left = Input.GetMouseButton(0);
            bool right = Input.GetMouseButton(1);
            if (Invert)
            {
                bool temp = left;
                left = right;
                right = temp;
            }
            if (right)
            {
                if (input.sqrMagnitude >= 0.01f)
                {
                    cinemachineTargetYaw += input.x * Speed;
                    cinemachineTargetPitch += input.y * Speed;
                }
    
                cinemachineTargetYaw = ClampAngle(cinemachineTargetYaw, float.MinValue, float.MaxValue);
                cinemachineTargetPitch = ClampAngle(cinemachineTargetPitch, BottomClamp, TopClamp);
    
                transform.rotation = Quaternion.Euler(0.0f, cinemachineTargetYaw, 0.0f);
                target.rotation = Quaternion.Euler(cinemachineTargetPitch, cinemachineTargetYaw, 0.0f);
            }
            if (left)
            {
                transform.Translate(-input.x, 0f, input.y);
            }
            vcameraDistance -= Input.GetAxis("Mouse ScrollWheel") * 10.0f;
            vcameraDistance = Mathf.Clamp(vcameraDistance, 2.0f, 20.0f);
            vcameraFollow.CameraDistance = Mathf.Lerp(vcameraFollow.CameraDistance, vcameraDistance, Time.deltaTime * 5.0f);
    
        }
    
        private static float ClampAngle(float lfAngle, float lfMin, float lfMax)
        {
            if (lfAngle < -360f) lfAngle += 360f;
            if (lfAngle > 360f) lfAngle -= 360f;
            return Mathf.Clamp(lfAngle, lfMin, lfMax);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
  • 相关阅读:
    亚马逊云Amazon OpenSearch Serverless“利刃在手,‘向量’八方“
    Tauri icon图标生成
    (2023 最新版)IntelliJ IDEA 下载安装及配置教程
    Docker 平台架构&底层原理二
    (web前端网页制作课作业)使用HTML+CSS制作非物质文化遗产专题网页设计与实现
    【Linux】系统进程的概念
    民国漫画杂志《时代漫画》第14期.PDF
    redis--springboot使用redis
    点成分享 | 一文读懂什么是微流控芯片
    MAVEN在IDEA中的使用
  • 原文地址:https://blog.csdn.net/u010197227/article/details/125498180