操控iphone重力感应.docxVIP

  • 2
  • 0
  • 约1.87千字
  • 约 3页
  • 2020-06-11 发布于山东
  • 举报
操控iphone重力感应 Posted on 2013年05月03日 by U3d / \o 查看 Unity3D脚本/插件 中的全部文章 Unity3D脚本/插件/被围观 93 次 方案一:speed,也可以把速度换成力. 01 public var simulateAccelerometer:boolean = false; 02 03 var speed = 10.0; 04 05 function Update () { 06 07 var dir : Vector3 = Vector3.zero; 08 09 if (simulateAccelerometer) 10 11 { 12 13 dir.x = Input.GetAxis(“Horizontal”); 14 15 dir.y = Input.GetAxis(“Vertical”); 16 17 } 18 19 else 20 21 { 22 23 dir.x = Input.acceleration.x; 24 25 dir.y = Input.acceleration.y; 26 27 // clamp acceleration vector to unit sphere 28 29 if (dir.sqrMagnitude 1) 30 31 dir.Normalize(); 32 33 // Make it move 10 meters per second instead of 10 meters per frame... 34 35 } 36 37 dir *= Time.deltaTime; 38 39 // Move object 40 41 transform.Translate (dir * speed); 42 43 } 方案二:Force   01 public var force:float = 1.0; 02 03   public var simulateAccelerometer:boolean = false; 04 05   function FixedUpdate () { 06 07   var dir : Vector3 = Vector3.zero; 08 09   if (simulateAccelerometer) 10 11   { 12 13   // using joystick input instead of iPhone accelerometer 14 15   dir.x = Input.GetAxis(“Horizontal”); 16 17   dir.y = Input.GetAxis(“Vertical”); 18 19   } 20 21   else 22 23   { 24 25   // we assume that device is held parallel to the ground 26 27   // and Home button is in the right hand 28 29   // remap device acceleration axis to game coordinates 30 31   // 1) XY plane of the device is mapped onto XZ plane 32 33   // 2) rotated 90 degrees around Y axis 34 35   dir.x = Input.acceleration.y; 36 37   dir.y = Input.acceleration.x; 38 39   // clamp acceleration vector to unit sphere 40 41   if (dir.sqrMagnitude 1) 42 43   dir.Normalize(); 44 45   } 46 47   rigidbody.AddForce(dir * force); 48 49   } 结论:方案一,操控起来比较灵活,反应灵敏。方案二,操控起来具有惯性,缓冲明显。

文档评论(0)

1亿VIP精品文档

相关文档