[Unity3d] mobile 3D Game Development: how to use gravity sensing in unity3d

Source: Internet
Author: User

Recommended for learning unity scripts: index on the unity3d Official Website


Gravity sensing is very common in the development of mobile games. Unity3d is a collection of gravity sensing related content.

A simple JS script demonstrates the use of gravity sensing.

Csdngravity. JS:

// Var round: texture2d; // The x y coordinate var x = 0 displayed on the screen; var y = 0; // maximum x y range displayed on the Object Screen var cross_x = 0; var cross_y = 0; function start () {// initialization value cross_x = screen. width-round. width; cross_y = screen. height-round. height;} function ongui () {// The overall x y z gravity sensing gravity component GUI is displayed. label (rect (0,0, 480,100), "position is" + input. acceleration); // draw the object GUI. drawtexture (rect (X, Y, 256,256), round);} function Update () {// modify the position of an object based on the gravity component. Multiply the value by 30 here to make the object move faster x ++ = input. acceleration. x * 30; y + =-input. acceleration. y * 30; // avoid an object exceeding the screen if (x <0) {x = 0;} else if (x> cross_x) {x = cross_x ;} if (Y <0) {Y = 0;} else if (Y> cross_y) {Y = cross_y ;}}

Input here refers to the input in unity, and acceleration is its gravity. x and y represent its gravity component respectively.

After creating the image, you only need to add the texture image:

Press Ctrl + B to create and run the task, and then you can see the gravity sensing effect on the real machine.

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.