Long time no Knock blog, thank you for your message, attention, private messages and other support, but I seem to have no way to continue to write the previous blog series, because I found online about unity3d content too little, so I can not extricate themselves to write u3d related articles ...
Third Person view
What the third person perspective is. Very simple, CS is a first-person perspective game, the player can not see their role image, only to observe the game outside their own content. The third-person view is clearly a visual angle that can see the character the player controls.
And most of the game lens can not be fixed, definitely to follow the pig's foot, can see the pig's feet, but maintain a certain height and distance, such a perspective is the best.
The u3d uses a lens to render the game screen, typically the main camera. The official has a third-person lens follow script and the pig's foot control script that uses JS to write, but given that U3d's JS syntax is too bizarre, I want to figure out how to write a third-person camera follow-up script that is cool for me.
= = Go to the airport to pick up someone and go back to writing =
OK, the next step is to complete the script. About adding the gameobject of the pig's foot and adding Charactercontroller component here does not explain, the premise is that the pig foot can move up and down in the scene and will not cross the obstacles and then we continue to write down.
That is, our camera is now a fixed view, the pig's feet can be moved and jump at will, as shown:
1. Create a script file CameraFollow.cs
2. Next, analyze the results we want most:
A, the camera maintains a certain height difference from the pig's foot (the y-axis fixed difference) and
B. Distance difference (x,z axis vector is relatively fixed);
C, facing straight ahead of the pig's foot
Finish the goal A:
Camera fixed at 10 meters above the pig's feet public
float camera_height=10.0f;
Camera away from the pig's feet about 10 meters horizontally from public
float camera_distance=10.0f;
Camera and the Transform property of the pig foot
private Transform player;
Private Transform camera;
Use the this for initialization
void Start () {
//Initialize
Player=gameobject.findgameobjectwithtag ("Player"). Transform;
camera = Camera.main.transform;
}
Update is called once per frame
void Update () {
//each frame changes the camera's height
camera.position = new Vector3 (player.po Sition.x, player.position.y+ camera_height, player.position.z);
}
Effect:
We expose the height distance of two variables in the script to public, can be modified in the inspector, first of all to change the height of an approximate satisfaction
At present we do not see the pig's foot, because the camera position is taken the x,z of the pig's foot, only the y-axis plus a fixed height.
So we need to calculate the x,z coordinates of the camera and the x,z coordinates of the pig's foot.
If you take the x,z coordinates of the pig's foot and subtract the distance separately, it is not possible, so that we can only be in a certain direction in the rear of the pig's foot
Modify the assignment statement in update to:
Then we run the game, choose to Maincamera, modify his y-axis rotation, until the lens is just moments behind the pig's feet, and found that this angle has been 45°:
So it is not difficult to understand that, when and only if the camera's Y axis deviates from 45°, x,z the value of a point in the ground plane, the position of the camera is subtracted from the same value on the x,z coordinate, the camera can just see the point in the middle.
That's why. Let me draw a diagram to indicate:
Ignoring the height difference of the camera, our pig's foot at Point O (α,0,α)
Camera in O-apostrophe (0,0,0)
To let the camera just see the pig's foot in the middle and maintain a certain distance, then the front of the camera is the z-axis of the coordinate system is the positive direction, and now to the direction of the Ray O ' O, then the deviation is obviously due to a square diagonal, (this is the perspective of the square), the angle of 45 °.
The question is, do we have to keep 45° to look at the pig's feet, do not forget our camera at any time to rotate to maintain the right foot, then this position in the end how much?
Let's finish the C: always be sure to face the front of the pig:
After adding these two lines of code, let's see where the problem is:
It is obvious that the camera is facing the front of the foot with the same direction, but the position is, when the pig's feet when the y-axis rotation deviation from the z-axis of 45°, the position in the back of the pig's feet, less than 45 ° to the left, greater than 45° right, 180°+45° directly to the front of the pig.
So, the situation we want is:
At 45°, X and z subtract distance
180°+45°=225°, X and z each add distance
At 0°, X does not change, z-distance/sin45° (why, because actually the length of the line at 45° o ' O is actually not distance but distance/sin45°, look at the picture I drew, the Pythagorean theorem)
At 90°, Z does not change, x-distance/sin45°
So in fact, our definition of the distance parameter name is not the same as what he represents, the actual distance should be distance/sin45°
So it is not difficult to generalize, we take the angle β as the variable, the function relation of x and β is:
X-=distance*sinβ
Z-=distance*cosβ
Just to meet the 4 conditions we listed above.
Because the trigonometric function parameters we need in our code are radians instead of angles, here we replace the angle with radians:
X-=distance*sin (β*π/180)
Z-=distance*cos (β*π/180)
So our code should be modified to:
Look at the pig's
//camera. LookAt (player);
And the front of the pig's foot is straight forward (only the rotation of the y-axis)
camera.eulerangles =new Vector3 (camera.eulerangles.x,
player.eulerangles.y,
CAMERA.EULERANGLES.Z);
Gets the y-axis rotational degree of the current lens
float angle = camera.eulerangles.y;
Calculate the distance difference of the x-axis:
float deltax = camera_distance * Mathf.sin (angle * mathf.pi/180);
float Deltaz = camera_distance * Mathf.cos (angle * mathf.pi/180);
Each frame changes the camera's height
camera.position = new Vector3 (player.position.x-deltax,
player.position.y+ camera_height,
Player.position.z-deltaz);
This lookat is no longer necessary, because our lens is rotated to the front of the pig's foot and the position is right behind him, then we must have been watching him.
The final effect is that no matter how the pig's feet go, our lenses are perfectly followed:
Complete script code (more streamlined than the official example):
Using Unityengine;
Using System.Collections; Add script to Component menu [Addcomponentmenu ("Cameracontrol/follow")] public class Camerafollow:monobehaviour {//camera fixed above pig's foot
10 m height public float camera_height=10.0f;
Camera away from the pig's feet about 10 meters horizontally from public float camera_distance=10.0f;
Camera and the Transform property of the pig foot private Transform player;
Private Transform camera;
Use the this for initialization void Start () {//Initialize Player=gameobject.findgameobjectwithtag ("Player"). Transform;
camera = Camera.main.transform; }//update is called once per frame void Update () {//See//camera to pig's feet.
LookAt (player); Positive front of the pig's foot (only the rotation of the y-axis) camera.eulerangles =new Vector3 (camera.eulerangles.x, player.eu
Lerangles.y, camera.eulerangles.z);
Gets the y-axis rotational degree of the current lens float angle = camera.eulerangles.y;
Calculate the distance difference of the x-axis: Float deltax = camera_distance * Mathf.sin (angle * mathf.pi/180); float Deltaz = camera_distance * Mathf.cos (angle * mathf.pi/180);
Each frame changes the camera's height camera.position = new Vector3 (Player.position.x-deltax, Player.po
sition.y+ camera_height, Player.position.z-deltaz);
Debug.Log ("angle:" +angle+ ", DeltaX:" +deltax+ ", Deltaz:" +deltaz "); }
}