Control components
Both the role control component and the rigid body component have the physical engine function. You must bind a game object to achieve the corresponding physical effect. In addition, only one of the two game objects can exist and cannot coexist. Rigid Body components can accurately simulate all physical effects in the real world, while role controllers are not so precise.
Before adding a role controller, You need to determine whether to introduce the standard resource package of the role controller into the project. You can add a role controller in component -- physics -- character controller.
After adding the role controller, you need to control the role Controller component in the script. Create a game script and use getcomponent <charactercontroller> () to obtain the role Controller component object. Then, the role is moved by calling simplemove.
In this example, the role Controller component is used to control the selection and movement of cube objects and to detect the collision of physical engines:
Using unityengine; using system. collections; public class script_06_05: monobehaviour {// role controller object private charactercontroller = NULL; // the speed at which the role moves private float movespeed = 30366f; // Private float rotatespeed = 3.0f; void start () {// get the role controller object controller = getcomponent <charactercontroller> ();} void ongui () {// control the role rotation if (guilayout. repeatbutton ("rotate left") {transform. rotate (0,-rotatespeed, 0);} If (guilayout. repeatbutton ("rotate right") {transform. rotate (0, rotatespeed, 0);} // controls the role to move if (guilayout. repeatbutton ("Move Forward") {controller. simplemove (vector3.forward * movespeed);} If (guilayout. repeatbutton ("move backward") {controller. simplemove (vector3.forward *-movespeed);} If (guilayout. repeatbutton ("Move left") {controller. simplemove (vector3.right *-movespeed);} If (guilayout. repeatbutton ("Move right") {controller. simplemove (vector3.right * movespeed );}}}
After running:
Flight and mobile
The role Controller component can also implement the flight and landing functions through the move () method, where the parameter is the flight angle. Unlike simplemove (), the move () method is applicable only to moving in different directions.
During development, try to use the move method because it is more flexible. The following example shows how to take off and land the Cube:
Using unityengine; using system. collections; public class script_06_06: monobehaviour {// role controller object private charactercontroller = NULL; // the speed at which the role moves private float movespeed = 3.0f; // Private float rotatespeed = 3.0f; void start () {// get the role controller object controller = getcomponent <charactercontroller> ();} void ongui () {// control the role rotation if (guilayout. repeatbutton ("rotate left") {transform. rotate (0,-rotatespeed, 0);} If (guilayout. repeatbutton ("rotate right") {transform. rotate (0, rotatespeed, 0);} // controls the role to move if (guilayout. repeatbutton ("Move Forward") {vector3 forward = transform. transformdirection (vector3.forward); controller. move (forward * movespeed);} If (guilayout. repeatbutton ("move backward") {vector3 forward = transform. transformdirection (vector3.forward); controller. move (forward *-movespeed);} // control the role's flight and landing if (guilayout. repeatbutton ("") {transform. translate (0, 1, 0);} If (guilayout. repeatbutton ("landing") {transform. translate (0,-1, 0 );}}}
In the above Code, note that the transform. transformdirection () method gets the direction of the current main character when moving
Run:
Click to take off:
Click to land:
Collision monitoring
Role controllers can sense the collision between game objects. When monitoring the collision between them, you need to call the oncontrollercolliderhit () method of the parent class.
Using unityengine; using system. collections; public class script_06_07: monobehaviour {// role controller object private charactercontroller = NULL; // the speed at which the role moves private float movespeed = 3.0f; // role rotation speed private float rotatespeed = 3.0f; // collision game object private gameobject colliderobj = NULL; void start () {// get the role controller object controller = getcomponent <charactercontroller> () ;}void ongui () {// control the role rotation if (guilayout. repeatbutton ("rotate left ")){ Transform. rotate (0,-rotatespeed, 0);} If (guilayout. repeatbutton ("rotate right") {transform. rotate (0, rotatespeed, 0);} // controls the role to move if (guilayout. repeatbutton ("Move Forward") {vector3 forward = transform. transformdirection (vector3.forward); controller. move (forward * movespeed);} If (guilayout. repeatbutton ("move backward") {vector3 forward = transform. transformdirection (vector3.forward); controller. move (forward *-movespeed);} // control the role flight and landing I F (guilayout. repeatbutton ("") {transform. translate (0, 1, 0);} If (guilayout. repeatbutton ("landing") {transform. translate (0,-1, 0);} // If (controller. collisionflags = collisionflags. sides) {If (colliderobj! = NULL) {GUI. color = color. black; GUI. label (New rect (200,100,200,100), "collision game object:" + colliderobj. name) ;}} void oncontrollercolliderhit (controllercolliderhit hit) {// The Game object colliderobj = hit. gameobject ;}}
After running:
[Unity 3D] Study Notes: control components