• unity 相机围绕物体旋转,并且有Y轴角度限制


    unity 相机围绕物体旋转,并且有Y轴角度限制,可通过parameters.offset 修改使目标物不在画面中心

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine.EventSystems;
    using UnityEngine.UI;

    public class CameraController : MonoBehaviour
    {
    public static CameraController instance;
    public Transform ModelRoot; //目标物
    public Transform[] targets; //空物体
    [HideInInspector]
    public int index; // 当前的场景
    private int comeBack = 0; // 返回主页计数
    private Transform curtarget; // 当前摄像机锁定的目标

    [System.Serializable]
    public class CameraParameters 
    {
        public bool smoothFollow;   //  是否开启平滑跟随
        public float followSpeed;   // 跟随速度
        public float distance;      // 相机的初始距离
        public float minDistance;   // 最小距离
        public float maxDistance;   // 最大距离
        public float zoomSpeed;     // 缩放速度
        public float zoomSensitivity;     // 缩放灵敏度
        public float rotationSensitivity; // 旋转灵敏度
        public float yMinLimit; // 垂直最小值
        public float yMaxLimit; // 垂直最大值
        public Vector3 offset;  // 偏移量
    
        public float inFade = 0.6f;     //  材质淡入
        public float outFade = 1.0f;    //  材质淡出
    }
    
    public CameraParameters parameters;  //  相机相关参数
    
    public float x { get; private set; } // 相机X方向旋转量
    public float y { get; private set; } // 相机Y方向旋转量
    public float distanceTarget { get; private set; } // 目标距离 
    
    private Vector3 position;
    private Quaternion rotation = Quaternion.identity;
    private Vector3 smoothPosition;
    private Camera cam;
    [HideInInspector]
    public bool isRay = true;  //  射线检测开关,在主场景才开启射线拾取
    private GameObject rayTarget;
    //  设备开机相关参数
    public static bool isStart = false;
    
    void Awake()
    {
        instance = this;
        parameters = new CameraParameters();
        InitCameraParaDefault();  //    设置相机的默认参数
    
        index = 0;
        curtarget = targets[index];
        Vector3 angles = transform.eulerAngles;
        x = angles.y;
        y = angles.x;
        //目标距离
        distanceTarget = parameters.distance;
        smoothPosition = transform.position;
    
        cam = Camera.main;
    }
    
    void Update()
    {
        if (isRay)
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);//从摄像机发出到点击坐标的射线
            RaycastHit hitInfo;
            if (Physics.Raycast(ray, out hitInfo))
            {
                Debug.DrawLine(ray.origin, hitInfo.point, Color.blue);//划出射线,只有在scene视图中才能看到
    
                rayTarget = hitInfo.collider.gameObject;
            }
            else
            {
                rayTarget = null;
            }
        }
    
        UpdateTransform();
    }
    
    
    #region 相机控制脚本
    //  默认的相机参数设置
    private void InitCameraParaDefault()
    {
        parameters.smoothFollow = false;
        parameters.followSpeed = 2.0f;
        parameters.minDistance = 0.0f;
        parameters.maxDistance = 10.0f;
        parameters.distance = 2f;
        parameters.zoomSpeed = 10.0f;
        parameters.zoomSensitivity = 1.0f;
        parameters.rotationSensitivity = 3.5f;
        parameters.yMinLimit = 0;
        parameters.yMaxLimit = 8;
        parameters.offset = new Vector3(-1.31f, 0.18f, -10.44f);
    
    }
    
    //  单个模型场景的相机参数设置 
    /// 
    /// maxDistance参数大于或等于distance参数
    /// 
    /// 
    /// 
    /// 
    /// 
    private void InitCameraParaOneModel(float distance, float minDistance, float maxDistance, float offsetY = 0.0f)
    {
        parameters.smoothFollow = false;
        parameters.followSpeed = 5.0f;
        parameters.minDistance = minDistance;
        parameters.maxDistance = maxDistance;
        parameters.distance = distance;
        parameters.zoomSpeed = 5.0f;
        parameters.zoomSensitivity = 1.0f;
        parameters.rotationSensitivity = 3.5f;
        parameters.yMinLimit = 0;
        parameters.yMaxLimit = 8;
        parameters.offset = new Vector3(0.0f, offsetY, 0.0f);
    }
    void LateUpdate()
    {
        UpdateInput();
    }
    
    // 获取用户鼠标信息
    public void UpdateInput()
    {
        if (curtarget == null || !cam.enabled) return;
    
        bool rotate = Input.GetMouseButton(0);  //  判断鼠标是否右键按下
    
        //delta rotation
        if (rotate)
        {
            x += Input.GetAxis("Mouse X") * parameters.rotationSensitivity;
            y = ClampAngle(y - Input.GetAxis("Mouse Y") * parameters.rotationSensitivity, parameters.yMinLimit, parameters.yMaxLimit);
        }
    
        // Distance
        distanceTarget = Mathf.Clamp(distanceTarget + zoomAdd, parameters.minDistance, parameters.maxDistance);
    }
    
    // 更新相机位置
    public void UpdateTransform()
    {
        UpdateTransform(Time.deltaTime);
    }
    
    public void UpdateTransform(float deltaTime)
    {
        if (curtarget == null || !cam.enabled) return;
    
        // Distance
        parameters.distance += (distanceTarget - parameters.distance) * parameters.zoomSpeed * deltaTime;
        // Rotation 
        rotation = Quaternion.AngleAxis(x, Vector3.up) * Quaternion.AngleAxis(y, Vector3.right);
    
        // Smooth follow
        if (!parameters.smoothFollow) smoothPosition = curtarget.position;
        else smoothPosition = Vector3.Lerp(smoothPosition, curtarget.position, deltaTime * parameters.followSpeed);
    
        // Position
        position = smoothPosition + rotation * (parameters.offset - Vector3.forward * parameters.distance);
    
        // Translating the camera
        transform.position = position;
        transform.rotation = rotation;
    }
    
    // Zoom input  放大缩小
    private float zoomAdd
    {
        get
        {
            float scrollAxis = Input.GetAxis("Mouse ScrollWheel");
    
            if (scrollAxis > 0) return -parameters.zoomSensitivity;
            if (scrollAxis < 0) return parameters.zoomSensitivity;
            return 0;
        }
    }
    
    // Clamping Euler angles
    private float ClampAngle(float angle, float min, float max)
    {
        if (angle < -360) angle += 360;
        if (angle > 360) angle -= 360;
        return Mathf.Clamp(angle, min, max);
    }
    #endregion
    
    • 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
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186

    }

  • 相关阅读:
    高颜值markdown解析器:粘贴复制代码即可用(不用编译打包)
    使用高斯Redis实现二级索引
    疫苗预约系统,疫苗接种管理系统,疫苗预约管理系统毕设作品
    Java基础(二十五):JDBC
    linux权限
    如何压缩图片大小?减小图片大小方法来啦
    title标签和meta标签怎样设置?有什么含义?
    技术突破,解决natapp免费域名动态变化问题
    HTML期末学生大作业-宠物之家网页作业html+css+javascript
    Linux ————VI编辑器
  • 原文地址:https://blog.csdn.net/weixin_44919646/article/details/132741252