Unity game Development Math and Physics 2 (control the movement of objects through the keyboard)

Source: Internet
Author: User

Control the movement of an object through the keyboard

Implementation needs to be noted:

    • Get keyboard-appropriate methods
    • Boundary detection
    • Velocity processing of the resultant force direction
    • Pythagorean theorem
usingUnityengine;usingSystem.Collections;//Control the movement of the object through the keyboard Public classkeycontrolmotiontest:monobehaviour{//X position of Object    floatPosX =0;//Y position of Object    floatPosY =0;//speed of the object in the x direction    floatSpeedx =1;//speed of the object in the y direction    floatSpeedY =1;//The top right pixel of the screen in world space coordinatesVector3 Screenrighttoppos;//The left lower pixel of the screen in world space coordinatesVector3 Screenleftbottompos;half width of//box    floatBoxhalfwidth;ReadOnly floatSqrt2; Public keycontrolmotiontest()    {the root of//2Sqrt2 = Mathf.sqrt (2); }voidStart () {//Convert the pixel on the right of the screen to the coordinates of world spaceScreenrighttoppos = Camera.main.ScreenToWorldPoint (NewVector3 (Screen.width, Screen.height,0));//Convert the pixel at the bottom right of the screen to the coordinates of world spaceScreenleftbottompos = Camera.main.ScreenToWorldPoint (NewVector3 (0,0,0));//box half width, because box is squareBoxhalfwidth = transform.localscale.x *0.5F//Initial positionTransform.localposition =NewVector3 (PosX, PosY,0); }voidUpdate () {//Hold left button        if(Isleftkey ()) {//Hold left button and press the UP key            if(Isupkey ())                {PosX-= Speedx/sqrt2 * TIME.DELTATIME;            PosY + = Speedy/sqrt2 * TIME.DELTATIME; }//Hold left button and press and hold down key            Else if(Isdownkey ())                {PosX-= Speedx/sqrt2 * TIME.DELTATIME;            PosY-= Speedy/sqrt2 * TIME.DELTATIME; }Else{PosX-= Speedx * TIME.DELTATIME; }        }//Hold right button        Else if(Isrightkey ()) {//Hold right button and press the UP key            if(Isupkey ())                {PosX + = Speedx/sqrt2 * TIME.DELTATIME;            PosY + = Speedy/sqrt2 * TIME.DELTATIME; }//Right-click and hold down the key            Else if(Isdownkey ())                {PosX + = Speedx/sqrt2 * TIME.DELTATIME;            PosY-= Speedy/sqrt2 * TIME.DELTATIME; }Else{PosX + = Speedx * TIME.DELTATIME; }        }//press and hold the key        Else if(Isupkey ())        {PosY + = SpeedY * TIME.DELTATIME; }//press and hold down the key        Else if(Isdownkey ())        {PosY-= SpeedY * TIME.DELTATIME; }//Boundary detection right        if(PosX + boxhalfwidth >= screenrighttoppos.x)        {PosX = Screenrighttoppos.x-boxhalfwidth; }//boundary detection left        if(Posx-boxhalfwidth <= screenleftbottompos.x)        {PosX = screenleftbottompos.x + boxhalfwidth; }//boundary detection on        if(PosY + boxhalfwidth >= screenrighttoppos.y)        {PosY = Screenrighttoppos.y-boxhalfwidth; }//Boundary detection        if(Posy-boxhalfwidth <= Screenleftbottompos.y)        {PosY = Screenleftbottompos.y + boxhalfwidth; }//Set the position of the current objectTransform.localposition =NewVector3 (PosX, PosY,0); }BOOLIsleftkey () {//Hold left button or a key, all left        if(Input.getkey (keycode.leftarrow) | | Input.getkey (Keycode.a)) {return true; }Else{return false; }    }BOOLIsupkey () {//Hold up the key or W key, all upward        if(Input.getkey (keycode.uparrow) | | Input.getkey (KEYCODE.W)) {return true; }Else{return false; }    }BOOLIsdownkey () {//Hold down the key or the S key, all down        if(Input.getkey (keycode.downarrow) | | Input.getkey (KEYCODE.S)) {return true; }Else{return false; }    }BOOLIsrightkey () {//Hold right button or D key, all right        if(Input.getkey (keycode.rightarrow) | | Input.getkey (KEYCODE.D)) {return true; }Else{return false; }    }}

Unity game Development Math and Physics 2 (control the movement of objects through the keyboard)

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.