V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
vueli
V2EX  ›  问与答

在 Unity 3D 中遇到无法控制摄像头的问题,请教一下谢谢啦

  •  
  •   vueli · 282 天前 · 275 次点击
    这是一个创建于 282 天前的主题,其中的信息可能已经有所发展或是发生改变。

    主要的功能就是楼层拆解,打开楼层后,镜头拉进

    这个是摄像机的脚本,不知道会不会锁住摄像机导致我无法控制

    using UnityEngine;
    
    public class CameraTargetMove : MonoBehaviour
    {
        public Transform target;
        public Vector3 pivotOffset = Vector3.zero;
        public float distance = 117.0f;
        public float minDistance = 2f;
        public float maxDistance = 150f;
        public float zoomSpeed = 1f;
        public float xSpeed = 250.0f;
        public float ySpeed = 250.0f;
        public bool allowYTilt = true;
        public float yMinLimit = -90f;
        public float yMaxLimit = 90f;
        private float x = 0.0f;
        private float y = 0.0f;
        private float targetX = 0f;
        private float targetY = 0f;
        public float targetDistance = 117f;
    
        public bool userControlEnabled = false;  // 用户控制开关
        private bool isFirstFrame = true;
        private void Start()
        {
            var angles = transform.eulerAngles;
            targetX = x = angles.x;
            targetY = y = ClampAngle(angles.y, yMinLimit, yMaxLimit);
            targetDistance = distance;
            userControlEnabled = true;
        }
    
        private void LateUpdate()
        {
            if (!target) return;
    
            if (userControlEnabled) {
                var scroll = Input.GetAxis("Mouse ScrollWheel");
                if (scroll > 0.0f) targetDistance -= zoomSpeed;
                else if (scroll < 0.0f)
                    targetDistance += zoomSpeed;
                targetDistance = Mathf.Clamp(targetDistance, minDistance, maxDistance);
               // if (Input.GetMouseButton(1) || (Input.GetMouseButton(0) && (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl))))
                if (Input.GetMouseButton(0))
                    {
                    targetX += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
                    if (allowYTilt)
                    {
                        targetY -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
                        targetY = ClampAngle(targetY, yMinLimit, yMaxLimit);
                    }
                }
    
                x = targetX;
                y = targetY;
                Quaternion rotation = Quaternion.Euler(y, x, 0);
                distance = targetDistance;
                Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.position + pivotOffset;
                transform.rotation = rotation;
                transform.position = position;
    
            }
           
        }
    
    
        private static float ClampAngle(float angle, float min, float max)
        {
            if (angle < -360) angle += 360;
            if (angle > 360) angle -= 360;
            return Mathf.Clamp(angle, min, max);
        }
    }
    
    

    在其他脚本处使用

    Camera.main.transform.LookAt(new Vector3(0.1f, 97f, -8.7f));
    
    

    img

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1084 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 18:25 · PVG 02:25 · LAX 11:25 · JFK 14:25
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.