开发学院手机开发iPhone 开发 重力感应操控(unity iphone) 阅读

重力感应操控(unity iphone)

 2010-09-25 01:39:00 来源:WEB开发网   
核心提示:重力感应操控(unity iphone)方案一:speedview sourceprint? 01public var simulateAccelerometer:boolean = false;02var speed = 10.0;03function Update () {04 var dir : Vector3

重力感应操控(unity iphone)

方案一:speed

view sourceprint? 01public var simulateAccelerometer:boolean = false;

02var speed = 10.0;

03function 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

view sourceprint? 01public var force:float = 1.0;

02public var simulateAccelerometer:boolean = false;

03

04function 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;

23

24 // clamp acceleration vector to unit sphere

25 if (dir.sqrMagnitude > 1)

26 dir.Normalize();

27 }

28

29 rigidbody.AddForce(dir * force);

30}

个人感觉方案一操控起来比较灵活,反应灵敏。方案二操控起来具有惯性,缓冲明显。

Tags:重力 感应 操控

编辑录入:coldstar [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
赞助商链接