Unity Learning -- 003: Role controller (medium), unity -- 003

Source: Internet
Author: User

Unity Learning -- 003: Role controller (medium), unity -- 003

In the previous article "unity learning-003: Role controller (I)", I checked the basic implementation methods of role control. The mouse and keyboard control were rough.

This article mainly introduces several role control methods encapsulated by unity,

Next, we will introduce several mainstream role control methods in the next article.

If you don't talk much about it, start:

It is still the last model dragged.

  

However, this time, the terrain, materials, and sky boxes are added, and the natural light are added. For how to add the terrain, see Add the terrain here.

I am not in a bad mood. Next I will introduce several built-in role control operations in unity. (These methods are all built-in APIs of unity)

1. CharacterController. move () method

Before using this method, add the CharacterController component to the role.

      

Then add a code script to the role.

// Speed public float speed = 6.0F; // High Jump speed public float jumpSpeed = 8.0F; // gravity public float gravity = 20366f; // Mobile target private Vector3 moveDirection = Vector3.zero; void Update () {// obtain the role controller CharacterController = GetComponent <CharacterController> (); // determine whether the role is located if (controller. isGrounded) {// set the moving vector moveDirection = new Vector3 (Input. getAxis ("Horizontal"), 0, Input. getAxis ("Vertical"); // change the role orientation to moveDirection = transform. transformDirection (moveDirection); moveDirection * = speed; // you can specify if (Input. getButton ("Jump") moveDirection. y = jumpSpeed;} moveDirection. y-= gravity * Time. deltaTime; // mobile controller. move (moveDirection * Time. deltaTime );}

Here we change the direction of the role and then change its position. Obviously, the movement direction here is not influenced by the orientation of the role. It is an absolute position, the world coordinate system,

2. CharacterController. simple () method

Public float speed = 3.0F; public float rotateSpeed = 3.0F; void Update () {CharacterController = GetComponent <CharacterController> (); // switch the role to transform. rotate (0, Input. getAxis ("Horizontal") * rotateSpeed, 0); Vector3 forward = transform. transformDirection (Vector3.forward); float curSpeed = speed * Input. getAxis ("Vertical"); // simple mobile controller. simpleMove (forward * curSpeed );}

This is a simple movement. Unlike the previous method, it is the coordinate system of itself, and the direction changes with the direction of the role,

In addition, it is difficult to achieve the jump here,

3. rigidbody. MovePosition ()

 

private Vector3 speed =Vector3.zero;void FixedUpdate() {        speed = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));rigidbody.MovePosition(rigidbody.position + speed *3* Time.deltaTime);}

By moving a role through a rigid body, the rigid body and the role controller are different. The role controller contains some attributes of the Rigid Body and does not affect the force.

This section will constantly update the role control methods encountered. After introducing Camera, we will try to implement several mainstream operation methods.

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.