Unity实现场景漫游相机
本文实例为大家分享了Unity实现场景漫游相机的具体代码,供大家参考,具体内容如下
前言
拿到场景后总喜欢在场景里面玩一段时间,那这个脚本就是你的不二选择
代码里加了注释,改起来也很方便。
使用方法
把脚本拖拽到场景相机上,开箱即用。
- WASD前后左右移动
- QE为上下
- Shift加速
- 鼠标右键按住旋转视角
- ESC退出游戏
源码
#ifENABLE_INPUT_SYSTEM&&ENABLE_INPUT_SYSTEM_PACKAGE #defineUSE_INPUT_SYSTEM usingUnityEngine.InputSystem; usingUnityEngine.InputSystem.Controls; #endif usingUnityEngine; publicclassSimpleCameraController:MonoBehaviour { #region相机状态 //////相机状态 /// classCameraState { publicfloatyaw; publicfloatpitch; publicfloatroll; publicfloatx; publicfloaty; publicfloatz; publicvoidSetFromTransform(Transformt) { pitch=t.eulerAngles.x; yaw=t.eulerAngles.y; roll=t.eulerAngles.z; x=t.position.x; y=t.position.y; z=t.position.z; } publicvoidTranslate(Vector3translation) { Vector3rotatedTranslation=Quaternion.Euler(pitch,yaw,roll)*translation; x+=rotatedTranslation.x; y+=rotatedTranslation.y; z+=rotatedTranslation.z; } publicvoidLerpTowards(CameraStatetarget,floatpositionLerpPct,floatrotationLerpPct) { 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); } publicvoidUpdateTransform(Transformt) { t.eulerAngles=newVector3(pitch,yaw,roll); t.position=newVector3(x,y,z); } } #endregion CameraStatem_TargetCameraState=newCameraState(); CameraStatem_InterpolatingCameraState=newCameraState(); [Header("MovementSettings移动设置")] [Tooltip("Exponentialboostfactorontranslation,controllablebymousewheel.平移的指数增强因子,可通过鼠标滚轮控制。")] publicfloatboost=3.5f; [Tooltip("Timeittakestointerpolatecameraposition99%ofthewaytothetarget.将相机位置插值到目标位置99%所需的时间。"),Range(0.001f,1f)] publicfloatpositionLerpTime=0.2f; [Header("RotationSettings旋转设定")] [Tooltip("X=Changeinmouseposition.改变鼠标位置。\nY=Multiplicativefactorforcamerarotation.相机旋转的乘性因子。")] publicAnimationCurvemouseSensitivityCurve=newAnimationCurve(newKeyframe(0f,0.5f,0f,5f),newKeyframe(1f,2.5f,0f,0f)); [Tooltip("Timeittakestointerpolatecamerarotation99%ofthewaytothetarget.插值相机旋转99%到目标所需的时间。"),Range(0.001f,1f)] publicfloatrotationLerpTime=0.01f; [Tooltip("WhetherornottoinvertourYaxisformouseinputtorotation.是否将鼠标输入的Y轴反转为旋转。")] publicboolinvertY=false; voidOnEnable() { m_TargetCameraState.SetFromTransform(transform); m_InterpolatingCameraState.SetFromTransform(transform); } Vector3GetInputTranslationDirection() { Vector3direction=newVector3(); 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; } returndirection; } voidUpdate() { Vector3translation=Vector3.zero; #ifENABLE_LEGACY_INPUT_MANAGER //ExitSample按下Esc键退出游戏 if(Input.GetKey(KeyCode.Escape)) { Application.Quit(); #ifUNITY_EDITOR UnityEditor.EditorApplication.isPlaying=false; #endif } //Hideandlockcursorwhenrightmousebuttonpressed按下鼠标右键时隐藏并锁定光标 if(Input.GetMouseButtonDown(1)) { Cursor.lockState=CursorLockMode.Locked; } //Unlockandshowcursorwhenrightmousebuttonreleased松开鼠标右键时解锁并显示光标 if(Input.GetMouseButtonUp(1)) { Cursor.visible=true; Cursor.lockState=CursorLockMode.None; } //Rotation旋转 if(Input.GetMouseButton(1)) { varmouseMovement=newVector2(Input.GetAxis("MouseX"),Input.GetAxis("MouseY")*(invertY?1:-1)); varmouseSensitivityFactor=mouseSensitivityCurve.Evaluate(mouseMovement.magnitude); m_TargetCameraState.yaw+=mouseMovement.x*mouseSensitivityFactor; m_TargetCameraState.pitch+=mouseMovement.y*mouseSensitivityFactor; } //Translation移动 translation=GetInputTranslationDirection()*Time.deltaTime; //Speedupmovementwhenshiftkeyheld按住shift键时加速移动 if(Input.GetKey(KeyCode.LeftShift)) { //原速度*10为按下Shift后的速度 translation*=10.0f; } //Modifymovementbyaboostfactor(definedinInspectorandmodifiedinplaymodethroughthemousescrollwheel)通过增强因子修改移动(在检查器中定义,通过鼠标滚轮在播放模式下修改) boost+=Input.mouseScrollDelta.y*0.2f; translation*=Mathf.Pow(2.0f,boost); #elifUSE_INPUT_SYSTEM //TODO:makethenewinputsystemwork使新的输入系统正常工作 #endif m_TargetCameraState.Translate(translation); //Framerate-independentinterpolation帧率无关插值 //Calculatethelerpamount,suchthatweget99%ofthewaytoourtargetinthespecifiedtime计算lerp的数量,这样我们就可以在指定的时间内到达目标的99% varpositionLerpPct=1f-Mathf.Exp((Mathf.Log(1f-0.99f)/positionLerpTime)*Time.deltaTime); varrotationLerpPct=1f-Mathf.Exp((Mathf.Log(1f-0.99f)/rotationLerpTime)*Time.deltaTime); m_InterpolatingCameraState.LerpTowards(m_TargetCameraState,positionLerpPct,rotationLerpPct); m_InterpolatingCameraState.UpdateTransform(transform); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。