Unity game Development Math and Physics 1 (object extension horizontal direction movement)

Source: Internet
Author: User

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)

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.