iOS development: Unity3d Role Controller Component Research

Source: Internet
Author: User
Tags bind requires

The way to use it is to first open the Unity Game Engine Editor, and then right click the import Package-> charactr Controller (role Controller) in Project view to import it into our project. As the following illustration shows, the first-person and third-person build has been added to the project view. The 3rd person Controller represents the third personal controller, and the first person Controller represents the number one controller.

As shown in the following illustration, we drag the Firstperson controller into the hierarchy (hierarchical view). Because the role controller has a certain physical engine, it must be placed above the terrain or surface object, otherwise it will fall when it receives physical effects and finds nothing on the ground to support it. Then run the game you will find and Cs in the first person effect is very similar, W, S, A, D Mobile characters walk, move the mouse to change the direction of walking, the space bar characters will jump.

The principle of the first person perspective is to create a capsule object in the game scene, and to bind a camera to the capsule body object, the camera object is shown in the following figure, it is bound in "person Controller". The default camera in the scene is invalidated, and the default camera can be deleted directly. Through the key to control the capsule movement, through the mouse to modify the direction of the capsule body, you will find that the first person perspective has been fully implemented, so far we do not need to write a line of code. The Sky box in the present scene I am binding in the camera using the Skybox component, because the camera object in the first person view is in the "personal Controller", so you need to bind the Skybox component to this camera. If you bind to the default camera then you will not see the effect of the sky.

Let's look at the third-person perspective, as shown in the following illustration, in Project view, dragging 3rd person controller into the hierarchy view. Third-person perspective requires the use of our original camera, if the camera has just been deleted. Click Creat->camera in the Hierarchy view. Then select the camera and set its tag to Maincamera in the right Inspector View, as shown in the following figure. Finally, in the hierarchy view, select 3rd person Controller, in the right Inspector View, the Camera Camera variable of the third person Transform script is bound to the primary camera that was just created, At this point to run the game in a third-person perspective to move the protagonist walking and jumping, the camera will always follow in the back unless the role controller component in the default source code, the source code in the right monitor panel view directly point open can be viewed.

Here we learn about the application of role controller components among other models. First, create two cubes (Cube objects) in the hierarchy view named: CUBE0 (objects that emit collisions) Cube1 (objects that receive collisions), and then select CUBE0 objects in Hierarchy view. Then the Unity navigation menu bar Select Component (component)-character-> Select any role control attribute. In addition, the role controller component must be imported in Project view, or the component will not be bound here. The role controller component conflicts with the collision component, so the collider component disappears when the role controller is added. Here we implement a simple code that uses the CUBE0 that adds the role controller component to collide with Cube1 that does not add role controller components.

[Code]java Code:

Using Unityengine;

Using System.Collections;

public class Test:monobehaviour {

The object name of the active collision

string castname = null;

Object name to receive collision

string receivename = null;

void Ongui ()

{

if (castname!= null && receivename!=null)

{

Set the color of the display to black

Gui.color = Color.Black;

Displays the object name of the active collision and the object receiving the collision

Gui. Label (New Rect (100,100,200,30), "Active Collision object Name" +castname);

Gui. Label (New Rect (100,200,200,30), "object name to receive collision" +receivename);

}

}

A collision between a role controller component and a Collider Component object

void Oncontrollercolliderhit (Controllercolliderhit hit)

{

Get Receive collision Name

Gameobject hitobject = Hit.collider.gameObject;

When it's not the ground time

if (!hitobject.name.equals ("Terrain"))

{

The object that gets the collision and the name of the object that receives the collision

Castname = Gameobject.name;

Receivename = Hitobject.name;

}

}

}

Bind the above code to the CUBE0 and run the game W, A, S, D buttons to control the CUBE1 cube movement. When the CUBE0 and CUBE1 collision, the program will enter the method Oncontrollercolliderhit (), through the parameters can be received collision of the game object is the Cube1 object, and Gameobject is the current active collision Cube1. As shown in the following figure, when two cubes collide, using the GUI already prints the collision information.

And then I'm talking about the Rigidbody component, the default model created in unity is that it does not have a receiving physical engine, unless you add a Rigidbody component or role controller component to the model. Let's talk about the Rigidbody first, and use the struggling birds to give examples. After the bird is fired, the bird hits the object with a parabola trajectory, and the object that is struck by the collision will have different physical effects depending on the angle and intensity of the bird's impact and almost completely simulate the real physical engine. But the effect of this physical engine cannot be tied to the protagonist of a RPG game. The reason is simple, because the physical engine that the rigidbody adds is too real to affect the user's action on the protagonist, for example, when the user hits the larger object in the control of the protagonist's movement, and the real physical engine bounces the protagonist back to its original position by the object's bounce force. But this is illogical because the rigidbody component is too physical, so we need to add the role controller component to the protagonist, which is more flexible and easier to manipulate.

Next we bind the Rigidbody component to the Cube1 object, select the Cube1 object and then select Component->physics->rigidbody (rigidbody) in the navigation menu bar. Let's take a look at this code, use the CUBE0 that adds the role controller component to collide with the Cube1 that adds the Rigidbody component, and when he collides, calculates the collision angle vector of the CUBE0 collision Cube1, then exerts a force on him through the rigidbody to push it away.

[Code]java Code:

Using Unityengine;

Using System.Collections;

public class Test:monobehaviour {

A collision between a role controller component and a Collider Component object

void Oncontrollercolliderhit (Controllercolliderhit hit)

{

Determine if a collision object has a Rigidbody component

Gameobject hitobject = Hit.collider.gameObject;

Rigidbody rigidbody = hitobject.rigidbody;

if (rigidbody!= null &&!rigidbody.iskinematic)

{

The ground is also equipped with rigid body components, here to judge

if (!hitobject.name.equals ("Terrain"))

{

Rigidbody. Addforce (New Vector3 (hit.movedirection.x, 0, HIT.MOVEDIRECTION.Z) * 10);

}

}

}

}

You can also bind the same script directly to the role controller component of the third person. As shown in the following illustration, the protagonist moves the surrounding boxes and pushes them away.

In general, the role controller component is suitable for the use of the actor object in the game because it needs to be supported by the physical engine, but not completely dependent on the physical engine, which requires code to write something.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.