Detail on Plane ball gravity sensing of unity3d Game Engine
Original by Yu Song MomoArticleIf you reprint it, please note: Reprinted to my independent domain name blogYusong Momo program Research Institute, Original address: http://www.xuanyusong.com/archives/537
Mobile phone gravity sensing should be no stranger to most developers. In the next-generation smartphone Android IOS WP7, many games use the built-in gravity sensing function of mobile phones, the powerful unity3d game engine is of course perfectly supported. Today, Momo will take you to learn about mobile phone gravity sensing in the 3D world. In this chapter, we aim to move a ball in the screen by shaking the mobile phone's gravity acceleration. In the previous Android series development articles, it seems that the principles are the same. Wow ~~
First, let's take a look at the gravity distribution chart of unity3d on the iPhone. As shown in, we can clearly see the gravity components in the three directions of x y z. In unity3d, the weight ranges from-1.0 to + 1.0.
X axis: The Home button rotates 90 degrees of gravity to the right of the next phone face to + 1.0 degrees of gravity to the left to rotate 90 degrees of gravity to-1.0
Y axis: the weight of the Home button at the back of the mobile phone is + 1.0. The weight of the Home button at the bottom of the mobile phone is-1.0.
Z axis: the weight of the mobile phone to the ground is + 1.0, and the weight of the mobile phone to the sky is-1.0.
OK! With these three sets of important values, we can control the mobile phone's gravity sensing. Then let's take a look at the small example of the ball gravity sensing game.
When unity3d is enabled, We will bind a script to the camera to respond to the user's control of the mobile phone to gravity sense the movement of the game ball.
In the important class of input, unity3d helps us encapsulate the method of gravity acceleration.
Input. Acceleration. X; gravity sensor X-axis gravity component
Input. Acceleration. Y; gravity of the Y axis
Input. Acceleration. Z; gravity sensing of the Z axis
New. js
// Var round: texture2d; // var x = 0; var y = 0; // The maximum x y range displayed on the Ball 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 GUI of the ball. drawtexture (rect (X, Y, 256,256), round);} function Update () {// modify the position of the Ball Based on the gravity component. Multiply the value by 30 here to make the ball move faster x ++ = input. acceleration. x * 30; y + =-input. acceleration. y * 30; // avoid the ball 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 ;}}
Gravity sensing pictures are not very good at capturing. Let's take a look at the following. The ball can move based on the gravity of my mobile phone. In the upper-left corner of the screen, the current x y z gravity is printed.
Here, some friends may ask me how to add gravity sensing to 3D objects? In fact, the method is the same, because it is the same principle. For example, I can bind a script to a model.Input. Acceleration gets the gravity sensing component of the current mobile phone, and then calculates the location of the current model based on this component. You can give it a try. It's actually very simple ~~
Finally, you are welcome to discuss unity3d game development with Momo. In general, this chapter is relatively simple ~~~ Attached to the unity3d project, I will not upload the xcode project and export it by myself.
:Http://www.xuanyusong.com/archives/537