Movement and collision analysis of Charactercontroller in Unity3d

Source: Internet
Author: User

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

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.