Unity场景漫游

#if ENABLE_INPUT_SYSTEM && ENABLE_INPUT_SYSTEM_PACKAGE
#define USE_INPUT_SYSTEM
    using UnityEngine.InputSystem;
    using UnityEngine.InputSystem.Controls;
#endif

using UnityEngine;

public class SimpleCameraController : MonoBehaviour
{
    // WASD前后左右移动
    // QE为上下
    //左Shift加速
    //鼠标右键按住旋转视角
    //ESC退出游戏


    #region 相机状态
    /// <summary>
    /// 相机状态
    /// </summary>
    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);
        }
    }
    #endregion

    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. 将相机位置插值到目标位置99%所需的时间。"), 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. 插值相机旋转99%到目标所需的时间。"), Range(0.001f, 1f)]
    public float rotationLerpTime = 0.01f;

    [Tooltip("Whether or not to invert our Y axis for mouse input to rotation. 是否将鼠标输入的Y轴反转为旋转。")]
    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))
        {
            //Debug.Log("执行了");

            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()
    {
        Vector3 translation = Vector3.zero;

//#if ENABLE_LEGACY_INPUT_MANAGER

            // Exit Sample 按下Esc键退出游戏
            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 移动
            translation = GetInputTranslationDirection() * Time.deltaTime;

            // Speed up movement when shift key held 按住shift键时加速移动
            if (Input.GetKey(KeyCode.LeftShift))
            {
                //原速度*10为按下Shift后的速度
                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);

//#elif USE_INPUT_SYSTEM
//             TODO: make the new input system work 使新的输入系统正常工作
//#endif

        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 计算lerp的数量,这样我们就可以在指定的时间内到达目标的99%
        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);


    }
}
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Unity场景漫游源文件是指用Unity软件创建的、包含了场景元素和功能的源代码文件。这种源文件通常包括场景中的各种游戏对象、脚本、材质、贴图等资源,并通过脚本控制它们之间的交互和行为。 Unity是一款流行的游戏开发引擎,它使用基于组件的编辑器来创建具有良好可视化效果的3D和2D场景。通过编写脚本,开发者可以控制游戏对象的移动、碰撞、动画等功能,从而实现场景漫游的效果。 在Unity场景漫游源文件中,可以添加场景中的游戏对象,如角色、道具、建筑等。同时,可以对这些对象进行操作,如控制角色移动、与其他对象碰撞、触发事件等。通过使用Unity提供的编辑器界面,开发者可以对不同游戏对象应用不同的材质和贴图,给场景增加丰富的视觉效果。 此外,Unity场景漫游源文件还包括脚本文件,通过编写脚本,可以定义游戏对象的行为、逻辑和交互方式。脚本可以通过C#或Unity的内置语言UnityScript编写,可以用于控制游戏对象的运动、视角切换、触发事件等。 总之,Unity场景漫游源文件是一种包含了场景元素和功能的源代码文件,通过编辑和编写脚本,可以创建出丰富多样的场景漫游效果。这使得开发者能够用Unity来创建出多样化且互动性强的游戏场景。 ### 回答2: Unity场景漫游源文件是指在Unity游戏引擎中创建的用于场景漫游的文件。这些源文件通常包括了场景中的各种元素,如建筑物、地形、角色、物体等。 首先,Unity场景漫游源文件的基本格式是Unity场景文件(.unity)或预制件(.prefab)。Unity场景文件是包含了场景的所有元素和设置的文件,而预制件则是将特定的游戏对象打包成一个可重复使用的元素,方便在不同的场景中使用。 在Unity场景漫游源文件中,可以包括各种游戏对象,比如玩家角色、敌人、道具、地形等。这些游戏对象可以通过Unity的编辑器进行添加、移动、旋转和缩放等操作。此外,源文件中还可以包含材质、纹理、光照和特效等组件,以创建更加真实和生动的场景Unity场景漫游源文件也包含了各种场景设置,如摄像机的位置和视角、环境光照的设置以及碰撞体积的定义等。这些设置可以通过代码或者编辑器进行修改,以实现不同的漫游效果和交互体验。 使用Unity场景漫游源文件,开发人员可以通过修改场景中的元素和设置,实现不同的游戏场景,触发不同的事件和交互。此外,开发人员还可以通过脚本编写增加更多的游戏逻辑和行为,以实现更加丰富和吸引人的游戏体验。 总之,Unity场景漫游源文件是用于构建和编辑Unity游戏引擎中的3D场景漫游的文件。它包含了场景中的元素、设置和逻辑,开发人员可以通过修改源文件来创建各种不同的游戏场景。 ### 回答3: Unity场景漫游源文件是指用Unity引擎创建的3D场景的源代码文件。在Unity中,我们可以使用场景漫游源文件来创建虚拟现实和增强现实应用程序,以及各种游戏场景场景漫游源文件通常包含了场景中的对象、材质、贴图、光照、摄像机、脚本等元素的设置和属性。通过编辑这些源文件,我们可以调整和优化场景的表现形式、交互方式及其他相关功能。 场景漫游源文件一般以Unity工程文件的格式保存,其中包含了场景的层级结构和相关组件的细节参数。在这些源文件中,我们可以添加、删除和移动游戏对象,更改其位置、旋转和比例,调整材质和光照效果,以及添加脚本代码来实现各种交互和动画效果等。 通过Unity的编辑器界面,我们可以直接查看和编辑场景漫游源文件。同时,Unity也提供了一些预设的组件和资源,例如地形、粒子系统、音频等,使得场景漫游源文件的创建更加便捷和丰富。 除了用于创建应用程序和游戏场景场景漫游源文件还可以在开发过程中进行版本控制、备份和共享。团队成员可以通过使用版本控制系统,协同编辑和合并场景文件,以便在开发周期中共同推进项目。 总而言之,Unity场景漫游源文件是创建3D场景的基础,通过对文件的编辑和调整,我们可以实现各种虚拟场景的创建和漫游
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值