Unity-related logic of camera movement

First look at the requirements:

In fact, I have learned the API and so on, and I feel that I know it at a glance, but I feel very blocked when I write it. It's also a matter of proficiency. 

The most difficult thing is the first one, using the angle to control the slope, and I also specially draw pictures and write formulas. In the end, I found that I thought it was complicated. Not much to say, just look at the train of thought.

 Before completing the first question, look at the third question. The camera should look at a direction above the character's head.

Then we need to get this orientation.

Because Vector3 cannot change x, y, z independently. Therefore, we obtain the position of the observation point at the original position +Vector3.up*height.

 

 After running, you can see that the nowPos point is at (0, 2, 0).

Now that the observed point is found, then we have to complete the first problem, the angle controls the slope.

 As can be seen from the figure, the angle between the camera and nowPos is \beta, so what we need to think about now is how to pass\beta

 To get the position of the camera camera.

 Tips: (1) The quaternion is multiplied by the vector, and the geometric meaning is the \betadegree of rotation of the vector around a certain axis.

              (2) Point + vector, the geometric meaning is that the point moves in the current coordinate system.

 Looking at the picture above, can we rotate the -forward vector \betato get the vector between camera-target.

Then let nowPos move, and find the position of the camera.

One thing to note when we use quaternions:

 The second parameter is the axis of rotation. The axis here is the x-axis of the target.

 

 In this case around the x-axis.

A quaternion is multiplied by a vector, and the result is the rotation about an axis.

 Get the moved vector, but note that what you get at this time is a unit vector. If we want the distance between the camera and nowPos to be variable, we need to set a float variable dis to control it.

 The next step is to move nowPos to get the position of the camera.

 carry out testing:

 Perfect, the first problem is solved smoothly.

The second question is to control the camera distance through the scroll wheel (with maximum and minimum limits)

Regarding this variable, we defined it above. dis variable, we need to associate it with the mouse wheel. Use the Input.GetAxis("xx") function. This function returns a value from -1 to 1.

GetAxis is filled with the string of control keys customized by Unity.

If you don't remember the variable name, you can find it in the Project Setting settings.

 To limit the maximum and minimum values, there is a clamping function in mathf:

 There are three parameters, when parameter one < parameter two, return parameter two. When parameter one is greater than parameter three, parameter three is returned. The rest returns parameter one. Call the clamping function!

 try: 

Perfect, complete the third question, the camera looks at nowPos. Regarding solving this problem, most of the time, you will think of using the LookAt function.

 But LookAt is just a package of quaternions. We use quaternions with more controllable quantities.

 The third point is done. The fourth point, use Vector3 to achieve camera follow. It's a cliché.

The fifth point is the same as the fourth point.

 In addition, camera-related operations are best performed in LateUpdate.

 

Guess you like

Origin blog.csdn.net/qq_42705793/article/details/127638578