The movement of the character of Unity3d--the keyboard movement from the angle of 45°

Source: Internet
Author: User

This knowledge point I want to be divided into several chapters to write, respectively, to record different ways of moving the different situations.

The first is the keyboard under the 45-degree angle camera to control the character movement (for example), write one of the simplest mobile script (this script can also be used for ordinary small test)

Before this, you have to hang up the character controller component for the character and then get it.

voidMove () {//first get the direction of the longitudinal axis        floatH = Input.getaxis ("Horizontal"); floatv = Input.getaxis ("Vertical"); //set the direction of the moveVector3 forward =transform.        Transformdirection (Vector3.forward); //set the speed of the move        floatCurrentspeed = Movespeed *v; //Direction * Speed is the character's moving distance.Cc. Simplemove (Forward *currentspeed); }

This is still relatively simple, usually need some role to move the test can use this script, but not rigorous, careful study will find it can not withstand scrutiny, so after my transformation has the following code:

IEnumerator Move_ () {
// get the direction of the longitudinal axis first
floatH = Input.getaxis ("Horizontal"); floatv = Input.getaxis ("Vertical"); // determine the positive or negative direction of the longitudinal axis (W forward, S back) if(v>0) {animation. Play (Run.name); // direction forward Vector3 forward=transform. Transformdirection (Vector3.forward);
// calculation speed
floatCurrentspeed = Movespeed *v; //Get the horizontal direction and rotate along the y axis at a certain speed This. Transform. Rotate (0, Trunspeed*time.deltatime*h,0); // start moving cc. Simplemove (Forward*currentspeed); // set an indicator to determine if a turn is required, in else it is necessary to judge the horizontal control direction of rotation in the stationary state Isturn=false; // a rotation indicating that the default is True if(Isrot) {
// If you need to turn around, the direction is front of the model, that is, 0 degrees, using a four-dollar number to interpolate the role rotation quaternion Target
= Quaternion.euler (0,0,0); This. transform.rotation = Quaternion.slerp ( This. Transform.rotation,target,0.3f);
// wait 0.3 seconds after the interpolation operation to close the behavior under this indicator (PS: This method is running under update)
yield return NewWaitforseconds (0.3f); Isrot=false; } }
// method basic Ibid, except for some special needs to explain the rest no longer the statement
Else if(v<0) {animation. Play (Run.name); // since the opposite is true, the real direction needs to be changed. Vector3 Back=transform. Transformdirection (Vector3.back); floatCurrentspeed = Movespeed *v; // Here the horizontal direction is obtained and the rotation along the Y axis needs to be multiplied by-1, in the opposite direction, the horizontal axis also needs to be reversed, otherwise rotating around the Y axis will reverse This. Transform. Rotate (0, trunspeed*time.deltatime*h*-1,0); Cc. Simplemove ( back*currentspeed); Isturn=true; if(Isrot) {quaternion target= Quaternion.euler (0, the,0); This. transform.rotation = Quaternion.slerp ( This. Transform.rotation,target,0.3f); yield return NewWaitforseconds (0.3f); Isrot=false; } }
// No action is set to idle state, in this state Isrot needs to be initialized for the next rotation
Else{ if(Isturn = =false){ This. Transform. Rotate (0, Trunspeed*time.deltatime*h,0); } Else{ This. Transform. Rotate (0, trunspeed*time.deltatime*h*-1,0); } Isrot=true; Animation. Play (Idle.name); } }

The movement of the character of Unity3d--the keyboard movement from the angle of 45°

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.