Movement of objects in horizontal direction
project implementation should pay attention to:
-Camera Settings Projection orthographic
-Start () and Update () execution order and number of executions
-Conversion of screen coordinates and spatial coordinates
-About Time.deltatime
-X + = V; v =-V;
usingUnityengine;usingSystem.Collections;//Uniform motion Public classUnirormmotiontest:monobehaviour {//Position of object floatPosX =0;//speed of the object in the x direction floatSpeed =3;//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;//Screen coordinates //+-----------+ (screen.width, screen.height) //| | //| Screen | //| | //+-----------+ //(0, 0) 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));half width of//boxBoxhalfwidth = transform.localscale.x *0.5F }voidUpdate () {if(transform.localposition.x + boxhalfwidth > screenrighttoppos.x | | transform.localposition.x-boxhalfwidth < screenleftbottompos.x) {//Change DirectionSpeed =-speed; } PosX + = speed * time.deltatime; Transform.localposition =NewVector3 (PosX,0.5F0); }}
Unity game Development Math and Physics 1 (object extension horizontal direction movement)