IOS development: Unity3D role Controller component Research
You can use it as follows. First open the Unity game engine Editor, right-click the Project view and choose Import Package> Charactr Controller to Import it to our Project. As shown in, the formation of the first person and third person has been added to the Project view. 3rd Person Controller indicates the third Person Controller, and First Person Controller indicates the First Person Controller.
As shown in, we drag the FirstPerson Controller into Hierarchy (hierarchical view. Because the role controller has a certain physical engine, it must be placed on the terrain or surface object. Otherwise, when it receives the physical effect, it will find that there is nothing on the ground to support it, it will fall. After running the game, you will find that the first person in CS has A very similar effect. W, S, A, and D move the person to walk, move the mouse to change the direction of the walk, and the Space key will jump.
The implementation principle of the first person perspective is to create a capsule game object in the game scenario and bind a camera to the capsule object, as shown in, it is bound to "Person Controller. In this scenario, the default camera will become invalid. You can delete the default camera directly. You can press the button to control the movement of the capsule body and modify the orientation of the capsule body with the mouse. Now you will find that the first person perspective has been fully implemented. So far, we do not need to write a line of code. Currently, the skyBox component is used to bind the SkyBox component to the camera. Because the camera object of the first Person perspective is in the "Person Controller", You need to bind the skyBox component to the camera, if you bind it to the default camera, you will not see the effect of the sky.
Next, let's look at the Third-Person perspective, as shown in. In the Project view, drag the 3rd Person Controller into the Hierarchy view. The third-person perspective needs to use our original camera, if we just deleted the camera. In the Hierarchy view, click Creat-> Camera. Select the camera and set its tag to MainCamera In the Inspector view on the right, as shown in. Finally, in the Hierarchy view, select 3rd Person Controller and bind the Camera Transform variable of the Third Person Camera script to the created primary Camera in the Inspector view on the right, after running the game, the mobile protagonist will walk and jump from the third person's perspective, and the camera will always follow behind unless the default source code provided in the role Controller component is modified, the source code can be viewed directly in the monitoring panel view on the right.
Next we will learn how to apply the role Controller component to other models. Create two cubes in Hierarchy view and name them Cube0 (collision object) Cube1 (collision receiving object). Then select the Cube0 object in Hierarchy view, in the Unity navigation bar, choose Component-Character-> select any role control attribute. In addition, the role Controller component must be imported in the Project view. Otherwise, components cannot be bound here. The role Controller component conflicts with the collision component, so the Collider component disappears after the role controller is added. The following is a simple code. Use the Cube0 that has been added to the role Controller component to collide with the Cube1 that has not been added to the role Controller component.
[Code] java code:
Using UnityEngine;
Using System. Collections;
Public class Test: MonoBehaviour {
// Name of the object that actively Colliders
String castName = null;
// Name of the object receiving the collision
String parameter ename = null;
Void OnGUI ()
{
If (castName! = Null & commonename! = Null)
{
// Set the color to black.
GUI. color = Color. black;
// Display the names of the objects that actively collide with the objects that receive the collision
GUI. Label (new Rect (100,100,200, 30), "Name of the object in active collision" + castName );
GUI. Label (new Rect (100,200,200, 30), "Name of the object receiving the Collision" + response ename );
}
}
// Collision between a role Controller component and a Collider Component Object
Void OnControllerColliderHit (ControllerColliderHit hit)
{
// Get the receiving collision name
GameObject hitObject = hit. collider. gameObject;
// When it is not the ground time
If (! HitObject. name. Equals ("Terrain "))
{
// Obtain the names of the objects that actively collide with the objects that receive the collision.
CastName = gameObject. name;
Required ename = hitObject. name;
}
}
}
Bind the above Code to Cube0. After running the game, press the W, A, S, and D buttons to control the movement of the Cube1 cube. When Cube0 and Cube1 collide, the program will enter the OnControllerColliderHit () method. Through parameters, you can get the game object receiving the collision, that is, the Cube1 object, the gameObject is the Cube1 that is currently in active collision. As shown in, when two cubes collide, the GUI has printed the collision information.
Next, let me talk about the Rigid Body component. By default, the model created in Unity does not have a receiving physical engine unless you add a rigid body component or a role Controller component to the model. Let's first talk about the rigid body and use the struggling birds to give an example. After launching a bird, the bird strikes the object with a parabolic track. after the collision, the collision will have different physical effects based on the angle and intensity of the bird's impact, in addition, it almost completely simulates the real physical engine. However, the effects of such a physical engine cannot be bound to the protagonist of an RPG Game, for example. The reason is very simple, because the physical engine added by the rigid body is too real, it will affect the user's operations on the main character, for example, when a user is controlling the movement of a protagonist, he or she collides on a mass object and returns the protagonist back to the original position based on the reverse elasticity of the actual physical engine. However, this is not logical, because the Rigid Body component is too physical, so we need to add the role Controller component to the main character. It is flexible to operate and makes it easier for us to operate the main character.
Next we will bind a rigid body Component to the Cube1 object, select the Cube1 object, and choose Component> physics> Rigidbody from the navigation menu ). Let's take a look at the following code. Use Cube0 with the role Controller component added to collide with Cube1 with the rigid body component. When a collision occurs, compute the collision angle vector of Cube0 with Cube1, then, push it through a rigid body.
[Code] java code:
Using UnityEngine;
Using System. Collections;
Public class Test: MonoBehaviour {
// Collision between a role Controller component and a Collider Component Object
Void OnControllerColliderHit (ControllerColliderHit hit)
{
// Determine whether the collision object has a Rigid Body Component
GameObject hitObject = hit. collider. gameObject;
Rigidbody rigidbody = hitObject. rigidbody;
If (rigidbody! = Null &&! Rigidbody. isKinematic)
{
// The ground also has a rigid body component. Let's take a look.
If (! HitObject. name. Equals ("Terrain "))
{
Rigidbody. AddForce (new Vector3 (hit. moveDirection. x, 0, hit. moveDirection. z) * 10 );
}
}
}
}
You can also bind the same script to the role Controller component in the third person. As shown in, the protagonist moved all the boxes around him.
In general, the role Controller component is applicable to models that require the support of the sensor physical engine but cannot fully depend on the physical engine. You need to compile your own code to compile some things, therefore, it is very suitable for the use of the main character objects in the game.