Unity3d: Realizing the character steering and moving

Source: Internet
Author: User

Tag: CTO engine implements ANIM Tran data code OID move

There are a lot of people walking control scripts on the Internet that are implemented by means of a ray, but only if you want to control them with keyboard keys. For example, third-person view control, in fact, only need to carry out a simple angle transformation can be. Ideas such as the following:

1, according to the clockwise direction set before, right, after, left respectively for 0,1,2,3.

2, set the initial state of 0, that is, towards the front.

3, the current direction value minus the previous direction value, multiplied by 90° is the steering angle, and then the rotation transformation can be.

Using unityengine;using system.collections;using system.linq;public class Move:monobehaviour{private int State;// Role State private int oldstate=0;//The state of the previous role private int up = 0;//role State forward private int right =1;//role state rightwards private int down = 2;//role State Backward private int left = 3;//role state to public float speed=8;void Start () {}void Update () {if (Input.getkey ("W")) {setState (UP);} else if (Input.getkey ("s")) {setState (down);} if (Input.getkey ("a")) {setState (left);} else if (Input.getkey ("D")) {setState (right);}} void setState (int currstate) {Vector3 transformvalue = new Vector3 ();//define translation vector int rotatevalue = (currstate-state) * 90;tra Nsform.animation.Play ("Walk");//play Character Walk animation switch (currstate) {case 0://role status forward, character moves forward slowly transformvalue = Vector3.forward * time.deltatime * speed;break;case 1://role Status right-time. The character moves slowly to the right transformvalue = vector3.right * time.deltatime * speed;break;case 2://role status backwards. The character moves backwards and forwards slowly transformvalue = Vector3.back * time.deltatime * speed;break;case 3://role status to the left, the character keeps moving slowly to the left Transformvalue = Vector3.left * Time.deltatiMe * speed;break;} Transform. Rotate (Vector3.up, rotatevalue);//rotate role transform. Translate (Transformvalue, Space.world);//translation Role Oldstate = state;//assignment, convenient next time to calculate state = currstate;//Assignment, convenient next calculation}}


Unity3d: Realizing the character steering and moving

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.