标题: 重力感应操控(unity iphone)方案二 [打印本页] 作者: 奔跑的小兔 时间: 2012-4-23 14:04 标题: 重力感应操控(unity iphone)方案二 方案二:Force
public var force:float = 1.0;
02 public var simulateAccelerometer:boolean = false;
03
04 function FixedUpdate () {
05 var dir : Vector3 = Vector3.zero;
06
07 if (simulateAccelerometer)
08 {
09 // using joystick input instead of iPhone accelerometer
10 dir.x = Input.GetAxis("Horizontal");
11 dir.y = Input.GetAxis("Vertical");
12 }
13 else
14 {
15 // we assume that device is held parallel to the ground
16 // and Home button is in the right hand
17
18 // remap device acceleration axis to game coordinates
19 // 1) XY plane of the device is mapped onto XZ plane
20 // 2) rotated 90 degrees around Y axis
21 dir.x = Input.acceleration.y;
22 dir.y = Input.acceleration.x;
24 // clamp acceleration vector to unit sphere
25 if (dir.sqrMagnitude > 1)
26 dir.Normalize();
27 }
28
29 rigidbody.AddForce(dir * force);
30 }
转自:http://www.cnblogs.com/lm3515/archive/2010/09/24/1833957.html