Summary:
In this paper, Unity first view Mouselook source code continues to deep analysis, and reference some of the four-dollar mathematical concept to illustrate the principle of work.
Import Resources:
Import characters in the project Bar, which contains firstpersoncharacters related resources.
Source files:
Characters->firstpersoncharacters->script->mouselook.cs
SOURCE Annotated:
public class Mouselook {public float xsensitivity = 2f;//x sensitivity public float ysensitivity = 2f;//y sensitivity public bool clampverticalrotation = true;//clamping angle public float minimumx = -90f;//around x axis rotation angle, bow limit public Floa T maximumx = 90f;//rotation angle around x axis, head limit public bool smooth;//open smooth view over public float Smoothtime = 5f;//smoothing Time P Rivate quaternion m_charactertargetrot;//The target angle of the role rotation (the role rotation will drive the camera to rotate) private quaternion m_cameratargetrot;//camera rotation target angle (camera Rotation does not drive character rotation) public void Init (Transform character, Transform camera) {M_charactertargetrot = Char Acter.localrotation; M_cameratargetrot = camera.localrotation; } public void Lookrotation (Transform character, Transform camera) {Float Yrot = Crossplatforminp Utmanager.getaxis ("Mouse X") * xsensitivity; float Xrot = Crossplatforminputmanager.getaxis ("Mouse Y") * ysensitivity;//Note that MouseX converts to Y-axis rotation, mousey to x-axis rotation m_ch AractertaRgetrot *= Quaternion.euler (0f, Yrot, 0f);//left/Right view M_cameratargetrot *= Quaternion.euler (-xrot, 0f, 0f);//up/Down view if (clampverticalrotation)//clamping x-axis rotation angle [ -90,90], that is, the upper and lower viewing angle, the default turn on M_cameratargetrot = Clamprotationaroundx Axis (M_cameratargetrot); if (smooth) {//swipe view, whether smooth mode is turned on, slerp interpolation, default off character.localrotation = Quaternion.slerp (Chara Cter.localrotation, M_charactertargetrot, Smoothtime * time.deltatime); Camera.localrotation = Quaternion.slerp (camera.localrotation, M_cameratargetrot, Smoothtime * Time.del Tatime); } else {character.localrotation = M_charactertargetrot; Camera.localrotation = M_cameratargetrot; }} quaternion Clamprotationaroundxaxis (quaternion q) {q.x/= q.w; Q.y/= Q.W; Q.z/= Q.W; Q.W = 1.0f; See below for fourThe interpretation of the //rad2deg radians, Deg2rad degrees to radians float AngleX = 2.0f * mathf.rad2deg * Mathf.atan (q.x); Mathf.clamp the value into the specified interval, the value has not changed within the interval, and if the value exceeds the range, it is grouped nearest to the interval endpoint. AngleX = Mathf.clamp (AngleX, MINIMUMX, MAXIMUMX); Tan is trigonometric q.x = Mathf.tan (0.5f * Mathf.deg2rad * AngleX); return q; } }
Four Yuan:
"Unity tricks" four-dollar (quaternion) and rotation
Unity provides four-dollar class, and also shows that if you are not familiar with the system four yuan tens of millions of do not modify the x,y,z,w.
The four-x,y,z,w was modified in Clamprotationaroundxaxis, and if you do not understand the four-dollar number, you cannot understand the code. The following article gives us a general understanding of what a four-dollar number is. We're not studying math, so we don't have to delve into it, as long as it helps us understand the code.
In the article, there is this fragment:
Given a Euler rotation (x, y, Z) (that is, rotate X, y, and z on the x-axis, y-axis, and z-axis, respectively), the corresponding four-dollar number is
x = sin (y/2) sin (z/2) cos (X/2) +cos (y/2) cos (Z/2) sin (X/2)
y = sin (y/2) cos (z/2) cos (X/2) +cos (Y/2) sin (z/2) sin (X/2)
z = cos (y/2) sin (z/2) cos (X/2)-sin (y/2) cos (Z/2) sin (X/2)
w = cos (y/2) cos (z/2) cos (X/2)-sin (Y/2) sin (z/2) sin (X/2)
According to this formula, we learned that Q.X/Q.W =tan < Span class= "Mi" id= "mathjax-span-498" style= ">θ/ 2 < Span style= "" >
we will θ/< Span style= "" > 2 /span> < Span style= "" > θ degree angle , float AngleX = 2.0f * mathf.rad2deg * Mathf.atan (q.x);
Clamp this corner on [ -90,90],mathf.clamp (AngleX, MINIMUMX, MAXIMUMX);
This angle is then converted to the form required by the four-dollar number. Note that this code Q.Y and q.z are always 0, and because q.w=1 so x can be directly assigned to Tanθ/2 .
If Q.W is assigned to Cos θ / 2 , then q.x needs to be assigned the value sin θ/ 2 , according to the four-dollar relationship between the two four-dollar modulus is not the same, the definition mentions that only modulo 1 of the four-dollar number can be used for rotation.
These two four-dollar (sin θ/2, 0,0,cosΘ/2), (Tanθ/2,0,0,1) should be able to represent the same rotation angle, except that the second four-tuple-modulo does not have a 1 conversion.
Limited to my level, if wrong, please correct me, thank you.
Unity First View Mouselook source analysis