In Unity3d, the system provides a first-person view model for the movement of one controller, which can be divided into two types:
I. Mobile transform
This move is done by moving directly to the transform attribute of the character model, moving in the form of the update function:
voidUpdate () {if(Input.getkey (KEYCODE.W)) {transform. Translate (Vector3.forward* Time.deltatime *Speed ); } Else if(Input.getkey (KEYCODE.S)) {transform. Translate (Vector3.forward* Time.deltatime *-Speed ); } Else if(Input.getkey (KEYCODE.A)) {transform. Translate (Vector3.right* Time.deltatime *-Speed ); } Else if(Input. GetKey (KEYCODE.D)) {transform. Translate (Vector3.right* Time.deltatime *Speed ); }}
Collision detection for a character model:
1. This method does not trigger a static collision, that is, in the object of the static collider can be worn into, out.
2. A rigid body collision can occur when colliding with an object with a rigid body assembly, and to detect a rigid body collision, the detection script must be attached to an object with a rigid component.
3. The character model can be used for trigger detection.
Second: The use of the system provided by the character model class Charactercontroller object in the Move method of moving, the specific code is as follows:
* Time.deltatime * speed);
Collision detection for a character model:
1. This moving method of the character model in the static collider can be a static collision, that is not able to cross the object, encountered a static collider object can only stop.
2. When colliding with an object with a rigid body, it does not traverse the object and does not trigger any rigid-body collision detection functions, but another function can be used to detect a character model touching a rigid body object.
Examples of Detection functions:
voidOncontrollercolliderhit (Controllercolliderhit hit) {Rigidbody body=Hit.collider.attachedRigidbody; if(BODY = =NULL||body.iskinematic) {return; } Else{Debug.Log ("Touch Gameobject:"+hit.collider.gameObject.name); //destroying Objects//Destroy (Hit.collider.gameObject); //give the object a moving force//body.velocity = new Vector3 (hit.movedirection.x,0,hit.movedirection.z) * 30.0f; }}
3. This move method can also perform trigger detection functions.
Movement and collision analysis of Charactercontroller in Unity3d