Unity在游戏内使用键鼠控制摄像机移动的脚本

本文介绍了一个Unity中的简单相机控制器脚本。该脚本通过键盘输入和鼠标操作来控制游戏中的相机移动和旋转,并实现了平滑过渡效果。文章详细解释了如何使用指数提升因子调整移动速度,以及如何设置插值时间来控制相机位置和旋转的平滑度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using UnityEngine;

namespace UnityTemplateProjects
{
    public class SimpleCameraController : MonoBehaviour
    {
        class CameraState
        {
            public float yaw;
            public float pitch;
            public float roll;
            public float x;
            public float y;
            public float z;

            public void SetFromTransform(Transform t)
            {
                pitch = t.eulerAngles.x;
                yaw = t.eulerAngles.y;
                roll = t.eulerAngles.z;
                x = t.position.x;
                y = t.position.y;
                z = t.position.z;
            }

            public void Translate(Vector3 translation)
            {
                Vector3 rotatedTranslation = Quaternion.Euler(pitch, yaw, roll) * translation;

                x += rotatedTranslation.x;
                y += rotatedTranslation.y;
                z += rotatedTranslation.z;
            }

            public void LerpTowards(CameraState target, float positionLerpPct, float rotationLerpPct)
            {
                yaw = Mathf.Lerp(yaw, target.yaw, rotationLerpPct);
                pitch = Mathf.Lerp(pitch, target.pitch, rotationLerpPct);
                roll = Mathf.Lerp(roll, target.roll, rotationLerpPct);
                
                x = Mathf.Lerp(x, target.x, positionLerpPct);
                y = Mathf.Lerp(y, target.y, positionLerpPct);
                z = Mathf.Lerp(z, target.z, positionLerpPct);
            }

            public void UpdateTransform(Transform t)
            {
                t.eulerAngles = new Vector3(pitch, yaw, roll);
                t.position = new Vector3(x, y, z);
            }
        }
        
        CameraState m_TargetCameraState = new CameraState();
        CameraState m_InterpolatingCameraState = new CameraState();

        [Header("Movement Settings")]
        [Tooltip("Exponential boost factor on translation, controllable by mouse wheel.")]
        public float boost = 3.5f;

        [Tooltip("Time it takes to interpolate camera position 99% of the way to the target."), Range(0.001f, 1f)]
        public float positionLerpTime = 0.2f;

        [Header("Rotation Settings")]
        [Tooltip("X = Change in mouse position.\nY = Multiplicative factor for camera rotation.")]
        public AnimationCurve mouseSensitivityCurve = new AnimationCurve(new Keyframe(0f, 0.5f, 0f, 5f), new Keyframe(1f, 2.5f, 0f, 0f));

        [Tooltip("Time it takes to interpolate camera rotation 99% of the way to the target."), Range(0.001f, 1f)]
        public float rotationLerpTime = 0.01f;

        [Tooltip("Whether or not to invert our Y axis for mouse input to rotation.")]
        public bool invertY = false;

        void OnEnable()
        {
            m_TargetCameraState.SetFromTransform(transform);
            m_InterpolatingCameraState.SetFromTransform(transform);
        }

        Vector3 GetInputTranslationDirection()
        {
            Vector3 direction = new Vector3();
            if (Input.GetKey(KeyCode.W))
            {
                direction += Vector3.forward;
            }
            if (Input.GetKey(KeyCode.S))
            {
                direction += Vector3.back;
            }
            if (Input.GetKey(KeyCode.A))
            {
                direction += Vector3.left;
            }
            if (Input.GetKey(KeyCode.D))
            {
                direction += Vector3.right;
            }
            if (Input.GetKey(KeyCode.Q))
            {
                direction += Vector3.down;
            }
            if (Input.GetKey(KeyCode.E))
            {
                direction += Vector3.up;
            }
            return direction;
        }
        
        void Update()
        {
            // Exit Sample  
            if (Input.GetKey(KeyCode.Escape))
            {
                Application.Quit();
				#if UNITY_EDITOR
				UnityEditor.EditorApplication.isPlaying = false; 
				#endif
            }

            // Hide and lock cursor when right mouse button pressed
            if (Input.GetMouseButtonDown(1))
            {
                Cursor.lockState = CursorLockMode.Locked;
            }

            // Unlock and show cursor when right mouse button released
            if (Input.GetMouseButtonUp(1))
            {
                Cursor.visible = true;
                Cursor.lockState = CursorLockMode.None;
            }

            // Rotation
            if (Input.GetMouseButton(1))
            {
                var mouseMovement = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y") * (invertY ? 1 : -1));
                
                var mouseSensitivityFactor = mouseSensitivityCurve.Evaluate(mouseMovement.magnitude);

                m_TargetCameraState.yaw += mouseMovement.x * mouseSensitivityFactor;
                m_TargetCameraState.pitch += mouseMovement.y * mouseSensitivityFactor;
            }
            
            // Translation
            var translation = GetInputTranslationDirection() * Time.deltaTime;

            // Speed up movement when shift key held
            if (Input.GetKey(KeyCode.LeftShift))
            {
                translation *= 10.0f;
            }
            
            // Modify movement by a boost factor (defined in Inspector and modified in play mode through the mouse scroll wheel)
            boost += Input.mouseScrollDelta.y * 0.2f;
            translation *= Mathf.Pow(2.0f, boost);

            m_TargetCameraState.Translate(translation);

            // Framerate-independent interpolation
            // Calculate the lerp amount, such that we get 99% of the way to our target in the specified time
            var positionLerpPct = 1f - Mathf.Exp((Mathf.Log(1f - 0.99f) / positionLerpTime) * Time.deltaTime);
            var rotationLerpPct = 1f - Mathf.Exp((Mathf.Log(1f - 0.99f) / rotationLerpTime) * Time.deltaTime);
            m_InterpolatingCameraState.LerpTowards(m_TargetCameraState, positionLerpPct, rotationLerpPct);

            m_InterpolatingCameraState.UpdateTransform(transform);
        }
    }

}
### Unity 中实现漫游操作的方法与可行性分析 #### 方法概述 在 Unity 中实现漫游操作通常涉及多个方面的技术支持,包括但不限于场景构建、用户交互逻辑编写以及性能优化等方面。具体而言,可以通过以下方式来完成: 1. **场景搭建** 场景的创建可以依赖于专业的三维建模工具如 3ds Max 或 Blender 进行模型设计,并将其导出为 `.fbx` 文件格式以便导入到 Unity 工程中[^2]。此外,还可以利用 Photoshop 等工具制作纹理贴图和其他视觉资源以增强场景的真实感。 2. **角色控制器设置** 使用内置的角色控制器组件或者自定义脚本控制玩家视角下的移动行为。例如,通过键盘输入(WASD 键)或触屏手势驱动摄像机沿指定方向平移及旋转,从而模拟人在实际空间内的行走体验[^3]。 3. **碰撞检测机制建立** 为了防止穿墙现象发生,需合理配置物体间的刚体属性并启用物理引擎来进行实时碰撞响应计算;另外也可以手动调整触发器区域大小进一步细化边界条件判断标准。 4. **路径规划算法应用(可选)** 如果项目需求涉及到NPC自动导航,则可能需要用到A*寻路插件或者其他类似的解决方案帮助对象找到最优路线前往目标地点而不会迷失方向。 5. **多平台适配考量** 针对不同终端设备特性做出相应调整——比如PC端侧重键鼠操控流畅度提升,而对于移动端则更关注手指滑动灵敏性和耗电量平衡等问题解决策略制定. #### 可行性评估 从技术角度来看,在Unity环境下开发具备基本漫游功能的应用程序完全可行。它提供了强大的图形渲染能力和灵活易用API接口支持开发者快速原型迭代测试直至最终产品成型发布整个周期顺利完成。而且社区活跃贡献了许多开源资产库可供借鉴学习降低研发成本缩短上市时间。然而需要注意的是随着项目规模扩大复杂程度增加可能会面临诸如内存占用过高帧率下降卡顿等挑战因此前期做好架构规划后期持续优化至关重要[^1]. ```csharp // 示例代码:简单的 WASD 控制方案 using UnityEngine; public class PlayerMovement : MonoBehaviour { public float speed = 5f; void Update() { // 获取水平轴和垂直轴的方向向量 float moveHorizontal = Input.GetAxis("Horizontal"); float moveVertical = Input.GetAxis("Vertical"); Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical); transform.Translate(movement * speed * Time.deltaTime); } } ``` 上述脚本展示了如何基于用户的按键指令改变游戏角色的位置状态形成初步意义上的自由探索模式效果展示。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值