- 最后登录
- 2024-3-11
- 注册时间
- 2013-9-17
- 阅读权限
- 90
- 积分
- 40625
- 纳金币
- 26224
- 精华
- 17
|
方案一:speed
01 public var simulateAccelerometer:boolean = false;
02 var speed = 10.0;
03 function Update () {
04 var dir : Vector3 = Vector3.zero;
05 if (simulateAccelerometer)
06 {
07 dir.x = Input.GetAxis("Horizontal");
08 dir.y = Input.GetAxis("Vertical");
09 }
10 else
11 {
12 dir.x = Input.acceleration.x;
13 dir.y = Input.acceleration.y;
14
15 // clamp acceleration vector to unit sphere
16 if (dir.sqrMagnitude > 1)
17 dir.Normalize();
18 // Make it move 10 meters per second instead of 10 meters per frame...
19 }
20 dir *= Time.deltaTime;
21 // Move object
22 transform.Translate (dir * speed);
23 }
也可以把速度换成力
方案二: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 } |
|
|