日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Unity鼠标控制相机上下左右环视360度旋转(Quaternion.AngleAxis)

發布時間:2023/12/16 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Unity鼠标控制相机上下左右环视360度旋转(Quaternion.AngleAxis) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

之前實現的是相機的360度旋轉,用的是LocalEulerAngle,這一篇文章實現用的是Quaternion.AngleAxis,這個方法將繞某個軸旋轉的角度轉為四元數


目前的四元數=初始的四元數*繞X軸轉的四元數*繞Y軸轉的四元數


?理解也比較好理解,就是繞某個軸旋轉的角度,轉換為四元數,然后將轉換后的四元數*初始的四元數=最新的四元數


using UnityEngine;using System.Collections; using System.Collections.Generic;public class SmoothMouseLook : MonoBehaviour {public float sensitivity = 4.0f;[HideInInspector]public float sensitivityAmt = 4.0f;//actual sensitivity modified by IronSights Scriptprivate float minimumX = -360f;private float maximumX = 360f;private float minimumY = -85f;private float maximumY = 85f;[HideInInspector]public float rotationX = 0.0f;[HideInInspector]public float rotationY = 0.0f;[HideInInspector]public float inputY = 0.0f;public float smoothSpeed = 0.35f;private Quaternion originalRotation;private Transform myTransform;[HideInInspector]public float recoilX;//non recovering recoil amount managed by WeaponKick function of WeaponBehavior.cs[HideInInspector]public float recoilY;//non recovering recoil amount managed by WeaponKick function of WeaponBehavior.csvoid Start(){ if (rigidbody){rigidbody.freezeRotation = true;}myTransform = transform;//cache transform for efficiencyoriginalRotation = myTransform.localRotation;//sync the initial rotation of the main camera to the y rotation set in editorVector3 tempRotation = new Vector3(0,Camera.main.transform.eulerAngles.y,0);originalRotation.eulerAngles = tempRotation;sensitivityAmt = sensitivity;//initialize sensitivity amount from var set by player// Hide the cursorScreen.showCursor = false;}void Update(){if(Time.timeScale > 0 && Time.deltaTime > 0){//allow pausing by setting timescale to 0//Hide the cursorScreen.lockCursor = true;Screen.showCursor = false;// Read the mouse input axisrotationX += Input.GetAxisRaw("Mouse X") * sensitivityAmt * Time.timeScale;//lower sensitivity at slower time settingsrotationY += Input.GetAxisRaw("Mouse Y") * sensitivityAmt * Time.timeScale;//reset vertical recoilY value if it would exceed maximumY amount if(maximumY - Input.GetAxisRaw("Mouse Y") * sensitivityAmt * Time.timeScale < recoilY){rotationY += recoilY;recoilY = 0.0f; }//reset horizontal recoilX value if it would exceed maximumX amount if(maximumX - Input.GetAxisRaw("Mouse X") * sensitivityAmt * Time.timeScale < recoilX){rotationX += recoilX;recoilX = 0.0f; }rotationX = ClampAngle (rotationX, minimumX, maximumX);rotationY = ClampAngle (rotationY, minimumY - recoilY, maximumY - recoilY);inputY = rotationY + recoilY;//set public inputY value for use in other scriptsQuaternion xQuaternion = Quaternion.AngleAxis (rotationX + recoilX, Vector3.up);Quaternion yQuaternion = Quaternion.AngleAxis (rotationY + recoilY, -Vector3.right);//smooth the mouse inputmyTransform.rotation = Quaternion.Slerp(myTransform.rotation , originalRotation * xQuaternion * yQuaternion, smoothSpeed * Time.smoothDeltaTime * 60 / Time.timeScale);//lock mouselook roll to prevent gun rotating with fast mouse movementsmyTransform.rotation = Quaternion.Euler(myTransform.rotation.eulerAngles.x, myTransform.rotation.eulerAngles.y, 0.0f);}else{//Show the cursorScreen.lockCursor = false;Screen.showCursor = true; }}//function used to limit anglespublic static float ClampAngle (float angle, float min, float max){angle = angle % 360;if((angle >= -360F) && (angle <= 360F)){if(angle < -360F){angle += 360F;}if(angle > 360F){angle -= 360F;} }return Mathf.Clamp (angle, min, max);}}

FR:海濤高軟(Hunk Xu)
QQ技術交流群:386476712

總結

以上是生活随笔為你收集整理的Unity鼠标控制相机上下左右环视360度旋转(Quaternion.AngleAxis)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。