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.