Character movement in the Unity3d

Source: Internet
Author: User

For third-person games, the protagonist usually adds a character controller to control the movement. It's time to simulate gravity and let the hero stand on the floor. If you use a D control around, W s control before and after, then the control mobile code can write:

 Public classMove:monobehaviour { PublicCharactercontroller character;  Public floatSpeed ; //Use this for initialization    voidStart () {character= This. Getcomponent<charactercontroller>(); speed=1f; }        //Update is called once per frame    voidUpdate () {floatHorizontal = Input.getaxis ("Horizontal");//A D or so        floatVertical = Input.getaxis ("Vertical");//W S up and down        floatMovey =0; floatGravity =-9.8f; Movey= gravity*Time.deltatime; Character. Move (NewVector3 (horizontal, Movey, vertical) * speed *time.deltatime); }}

Use the Input.getaxis method to get the following default axes: "Horizontal" and "Vertical" are mapped to the joystick, A, W, S, D, and arrow keys (direction keys). Horizontal and verical are two numbers between [ -1,1] respectively. With this code, the characters move only on the XZ plane.

You can also use transform to move. Translate method, or use rigidbody and give the rigid body speed. But one thing to note is that if the move command is inside the update () function, the object moves with jitter, because each frame time is not fixed and the distance traveled is long and short. The solution to this problem is to write the move statement in the Fixedupdate () function, because the length of each frame of the function is fixed.

Specific other mobile methods can be seen

Character movement in the Unity3d

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: 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.