Third-person character movement and free-moving field of view (Rigidbody implementation)

Source: Internet
Author: User

Focus: The operation of the vector. After the speed is obtained horizontally and vertically, the direction is reset to synchronize the direction with the field of view (i.e., the camera is in the same direction as the character)

Here's an example of how to do this:

1. Create a terrain (terrain), two cube (cube) (reference), capsule (Capsule). Place the main camera under the capsule as a sub-object and reset the position information.

(for easy observation you can create several material balls to attach to the object)

2. Move the camera to the game view to achieve the following:

3. Create two scripts one to control movement another control field of rotation: (I created the move and Freelook two scripts)

Move script content:

Using unityengine;using System.collections;public class Move:monobehaviour {public gameobject camer;//camera/ Initializationvoid Start () {}//Update is called once per framevoid update () {Vector3 forward = Camer.transform.Transfor Mdirection (Vector3.forward);//Record camera forward to Vector3 right=camer.transform.transformdirection (vector3.right);// Record the right direction of the camera float H = Input.getaxis ("horizontal"); float V = Input.getaxis ("Vertical"); Vector3 he = H * right + V * forward;//The speed is synthesized getcomponent<rigidbody> (). Moveposition (transform.position+ He * 5 * time.deltatime);//Control Move}}

Freelook Script content:

Using unityengine;using System.collections;public class Freelook:monobehaviour {public Gameobject camer;private float s peed=5.0f;//rpm//use of this for Initializationvoid Start () {}//Update is called once per framevoid update () {CAMER.TRANSF Orm. Rotatearound (This.transform.position,vector3.up,speed*input.getaxis ("Mouse X"));//camera with a character-centric, own y-axis rotation}}

4. Assign the camera to the script:

Third-person character movement and free-moving field of view (Rigidbody implementation)

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.