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