Unity Helicopter Flight HeliHell Source Code Analysis (1) - Camera Slow Motion Tracking

Suddenly, I found a bunch of source codes of Unity game Demo in Baidu's online disk. In order to learn these contents better, I decided to record my understanding of these source codes in the form of blog posts, and facilitate some white people like me to learn knowledge. In the future, try to write an article about it every one to two weeks.
This article starts with the simplest one. This source code package is called HeliHell. It is available in the unity store, but has not been updated for a long time.

Effect Preview

Demo includes a simple terrain and a UH1N helicopter. After running, use the mouse to control the helicopter to rotate, move forward and backward, and use the direction keys to control the helicopter to move up and down, and move left and right. Click to download: 3wfg
 Unity helicopter flight

All knowledge points

  1. Acquisition of mouse and keyboard direction key input
  2. Understanding of Transform translation and rotation
  3. Role of rigid body components and hinge joints
  4. Realization of camera slow motion following

Script list

After importing the resource package, there is a Game scene in the Scenes folder and 7 JS scripts in the Scripts folder. There are six scripts actually used in the scenario, namely, heli land, heli-physics,helileaves,heliSmoke,PropellerRotate,UberFollow。 The functions of these scripts are as follows:

  1. UberFollow: Very clever camera tracking;
  2. Heli physics: aircraft control;
  3. Heli land: acquisition of aircraft landing status;
  4. Helileaves: particle effect control, leaves on the ground when taking off;
  5. HeliSmoke: particle effect control, smoke on the ground during takeoff;
  6. PropellerRotate: rotation of rotor and main shaft.

For convenience, I rewrite these js scripts into C # scripts.

UberFollow script

This script is a camera tracking script.
Principle: create an empty object focus under target according to the given offset, and gradually move the camera closer to the focus object in FixedUpdate until the coordinates of the two coincide. Its essence lies in the following five statements:

 Vector3 temp = Vector3.zero; temp.x = (1.0f - distanceSmooth) * transform.position.x + distanceSmooth * focus.position.x; temp.z = (1.0f - distanceSmooth) * transform.position.z + distanceSmooth * focus.position.z; temp.y = (1.0f - heightSmooth) * transform.position.y + heightSmooth * focus.position.y; transform.position = temp; transform.LookAt(target);

The author gives two Smooth smoothing parameters here, and we can only analyze one axis.

 //Take the X axis as an example temp.x = (1.0f - distanceSmooth) * transform.position.x + distanceSmooth * focus.position.x;

Assuming that the focus coordinate remains unchanged, that is, the target does not move, then the focus.position can be regarded as a fixed value. Assuming that the camera does not coincide with the focus position at this time, and the camera x coordinate is 0, distanceSmooth = 0.15, According to the times of FixedUpdate, the camera X coordinate has the following calculation results.

 //First time: 0+first //Second time: first * 0.85+first //The third time: first * 0.85 * 0.85+first * 0.85+first //The fourth time: first * 0.85 * 0.85 * 0.85+first * 0.85 * 0.85+first * 0.85+first

Do you see anything? Let's do a calculation:

 //Second - first=first * 0.85; //Third - second=first * 0.85 * 0.85; //Fourth - third=first * 0.85 * 0.85 * 0.85; //…… //Easy to get, N+1 - N=first * 0.85 ^ N

According to the formula we have summarized, the increment of the camera moving forward every time FixedUpdate continues to decrease, that is to say, the moving speed of the camera is getting slower and slower, and it has a slow motion effect!
So when will the camera stop moving? After thinking, when the camera position coincides with the focus position, the equation can be simplified as follows

 temp.x = (0.85 + 1.5) * focus.transform.x = foucus.transform.x;

At this time, the position of the camera will not change.

When the focus position continues to change, the position of the focus at a certain time can be regarded as unchanged. After the next frame starts, the camera catches up but fails to catch up, and the position of the focus increases after the end of this frame. Then I can start a new chase. It should not be difficult to understand this dynamic process. Finally, there will always be a moment when you stop, and the camera will slowly catch up with you. Well, the principle of other axes is the same.

After reading this script, I can't help sighing: how beautiful these three statements are, how clever these two parameters are, and how exquisite this script is. Code is like poem, poem is like code.

Postscript

To be continued.

References

1. [Header] [Unity] Unity Japan UnityChanSD Role

Zimiao haunting blog (azimiao. com) All rights reserved. Please note the link when reprinting: https://www.azimiao.com/4175.html
Welcome to the Zimiao haunting blog exchange group: three hundred and thirteen million seven hundred and thirty-two thousand

Comment

*

*

Comment area

  1. kk 01-06 10:28 reply

    I downloaded the code, but the operation perspective is different from yours. It is seen from the remote perspective, and the helicopter can't operate

    • hare 01-07 17:40 reply

      Please refer to https://www.chiark.greenend.org.uk/ ~sgtatham/bugs-cn.html