#if ENABLE_INPUT_SYSTEMusingUnityEngine.InputSystem;#endifusingUnityEngine;namespaceUnityTemplateProjects{publicclassSimpleCameraController:MonoBehaviour{classCameraState{publicfloat yaw;publicfloat pitch;publicfloat roll;publicfloat x;publicfloat y;publicfloat z;publicvoidSetFromTransform(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;}publicvoidTranslate(Vector3 translation){Vector3 rotatedTranslation = Quaternion.Euler(pitch, yaw, roll)* translation;
x += rotatedTranslation.x;
y += rotatedTranslation.y;
z += rotatedTranslation.z;}publicvoidLerpTowards(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);}publicvoidUpdateTransform(Transform t){
t.eulerAngles =newVector3(pitch, yaw, roll);
t.position =newVector3(x, y, z);}}constfloat k_MouseSensitivityMultiplier =0.01f;CameraState m_TargetCameraState =newCameraState();CameraState m_InterpolatingCameraState =newCameraState();[Header("Movement Settings")][Tooltip("Exponential boost factor on translation, controllable by mouse wheel.")]publicfloat boost =3.5f;[Tooltip("Time it takes to interpolate camera position 99% of the way to the target."),Range(0.001f,1f)]publicfloat positionLerpTime =0.2f;[Header("Rotation Settings")][Tooltip("Multiplier for the sensitivity of the rotation.")]publicfloat mouseSensitivity =60.0f;[Tooltip("X = Change in mouse position.\nY = Multiplicative factor for camera rotation.")]publicAnimationCurve mouseSensitivityCurve =newAnimationCurve(newKeyframe(0f,0.5f,0f,5f),newKeyframe(1f,2.5f,0f,0f));[Tooltip("Time it takes to interpolate camera rotation 99% of the way to the target."),Range(0.001f,1f)]publicfloat rotationLerpTime =0.01f;[Tooltip("Whether or not to invert our Y axis for mouse input to rotation.")]publicbool invertY =false;#if ENABLE_INPUT_SYSTEMInputAction movementAction;InputAction verticalMovementAction;InputAction lookAction;InputAction boostFactorAction;bool mouseRightButtonPressed;voidStart(){var map =newInputActionMap("Simple Camera Controller");
lookAction = map.AddAction("look",binding:"/delta");
movementAction = map.AddAction("move",binding:"/leftStick");
verticalMovementAction = map.AddAction("Vertical Movement");
boostFactorAction = map.AddAction("Boost Factor",binding:"/scroll");
lookAction.AddBinding("/rightStick").WithProcessor("scaleVector2(x=15, y=15)");
movementAction.AddCompositeBinding("Dpad").With("Up","/w").With("Up","/upArrow").With("Down","/s").With("Down","/downArrow").With("Left","/a").With("Left","/leftArrow").With("Right","/d").With("Right","/rightArrow");
verticalMovementAction.AddCompositeBinding("Dpad").With("Up","/pageUp").With("Down","/pageDown").With("Up","/e").With("Down","/q").With("Up","/rightshoulder").With("Down","/leftshoulder");
boostFactorAction.AddBinding("/Dpad").WithProcessor("scaleVector2(x=1, y=4)");
movementAction.Enable();
lookAction.Enable();
verticalMovementAction.Enable();
boostFactorAction.Enable();}#endifvoidOnEnable(){
m_TargetCameraState.SetFromTransform(transform);
m_InterpolatingCameraState.SetFromTransform(transform);}Vector3GetInputTranslationDirection(){Vector3 direction = Vector3.zero;#if ENABLE_INPUT_SYSTEMvar moveDelta = movementAction.ReadValue<Vector2>();
direction.x = moveDelta.x;
direction.z = moveDelta.y;
direction.y = verticalMovementAction.ReadValue<Vector2>().y;#elseif(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;}#endifreturn direction;}voidUpdate(){// Exit Sampleif(IsEscapePressed()){
Application.Quit();#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying =false;#endif}// Hide and lock cursor when right mouse button pressedif(IsRightMouseButtonDown()){
Cursor.lockState = CursorLockMode.Locked;}// Unlock and show cursor when right mouse button releasedif(IsRightMouseButtonUp()){
Cursor.visible =true;
Cursor.lockState = CursorLockMode.None;}// Rotationif(IsCameraRotationAllowed()){var mouseMovement =GetInputLookRotation()* k_MouseSensitivityMultiplier * mouseSensitivity;if(invertY)
mouseMovement.y =-mouseMovement.y;var mouseSensitivityFactor = mouseSensitivityCurve.Evaluate(mouseMovement.magnitude);
m_TargetCameraState.yaw += mouseMovement.x * mouseSensitivityFactor;
m_TargetCameraState.pitch += mouseMovement.y * mouseSensitivityFactor;}// Translationvar translation =GetInputTranslationDirection()* Time.deltaTime;// Speed up movement when shift key heldif(IsBoostPressed()){
translation *=10.0f;}// Modify movement by a boost factor (defined in Inspector and modified in play mode through the mouse scroll wheel)
boost +=GetBoostFactor();
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 timevar 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);}floatGetBoostFactor(){#if ENABLE_INPUT_SYSTEMreturn boostFactorAction.ReadValue<Vector2>().y *0.01f;#elsereturn Input.mouseScrollDelta.y *0.01f;#endif}Vector2GetInputLookRotation(){// try to compensate the diff between the two input systems by multiplying with empirical values#if ENABLE_INPUT_SYSTEMvar delta = lookAction.ReadValue<Vector2>();
delta *=0.5f;// Account for scaling applied directly in Windows code by old input system.
delta *=0.1f;// Account for sensitivity setting on old Mouse X and Y axes.return delta;#elsereturnnewVector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));#endif}boolIsBoostPressed(){#if ENABLE_INPUT_SYSTEMbool boost = Keyboard.current !=null? Keyboard.current.leftShiftKey.isPressed :false;
boost |= Gamepad.current !=null? Gamepad.current.xButton.isPressed :false;return boost;#elsereturn Input.GetKey(KeyCode.LeftShift);#endif}boolIsEscapePressed(){#if ENABLE_INPUT_SYSTEMreturn Keyboard.current !=null? Keyboard.current.escapeKey.isPressed :false;#elsereturn Input.GetKey(KeyCode.Escape);#endif}boolIsCameraRotationAllowed(){#if ENABLE_INPUT_SYSTEMbool canRotate = Mouse.current !=null? Mouse.current.rightButton.isPressed :false;
canRotate |= Gamepad.current !=null? Gamepad.current.rightStick.ReadValue().magnitude >0:false;return canRotate;#elsereturn Input.GetMouseButton(1);#endif}boolIsRightMouseButtonDown(){#if ENABLE_INPUT_SYSTEMreturn Mouse.current !=null? Mouse.current.rightButton.isPressed :false;#elsereturn Input.GetMouseButtonDown(1);#endif}boolIsRightMouseButtonUp(){#if ENABLE_INPUT_SYSTEMreturn Mouse.current !=null?!Mouse.current.rightButton.isPressed :false;#elsereturn Input.GetMouseButtonUp(1);#endif}}}