找回密码
 立即注册
查看: 6381|回复: 90

[脚本] 调用Unity3d iphone的重力感应

[复制链接]
发表于 2012-6-13 22:43 | 显示全部楼层 |阅读模式
方案一:speed

  1. public var simulateAccelerometer:boolean = false;

  2. var speed = 10.0;

  3. function Update () {

  4.     var dir : Vector3 = Vector3.zero;

  5.     if (simulateAccelerometer)

  6.     {

  7.         dir.x = Input.GetAxis("Horizontal");

  8.         dir.y = Input.GetAxis("Vertical");

  9.     }

  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?

  1. public var force:float = 1.0;

  2. public var simulateAccelerometer:boolean = false;



  3. function FixedUpdate () {

  4.     var dir : Vector3 = Vector3.zero;



  5.     if (simulateAccelerometer)

  6.     {

  7.         // using joystick input instead of iPhone accelerometer

  8.         dir.x = Input.GetAxis("Horizontal");

  9.         dir.y = Input.GetAxis("Vertical");

  10.     }

  11.     else

  12.     {

  13.         // we assume that device is held parallel to the ground

  14.         // and Home button is in the right hand

  15.          

  16.         // remap device acceleration axis to game coordinates

  17.         // 1) XY plane of the device is mapped onto XZ plane

  18.         // 2) rotated 90 degrees around Y axis

  19.         dir.x = Input.acceleration.y;

  20.         dir.y = Input.acceleration.x;

  21.          

  22.         // clamp acceleration vector to unit sphere

  23.         if (dir.sqrMagnitude > 1)

  24.             dir.Normalize();

  25.     }

  26.      

  27.     rigidbody.AddForce(dir * force);

  28. }
复制代码
个人感觉方案一操控起来比较灵活,反应灵敏。方案二操控起来具有惯性,缓冲明显。
发表于 2017-3-8 16:44 | 显示全部楼层
很不错
发表于 2017-3-8 16:11 | 显示全部楼层
楼主是超人
发表于 2017-3-8 16:52 | 显示全部楼层
真心顶
发表于 2017-3-8 16:23 | 显示全部楼层
很好哦
发表于 2017-3-8 16:19 | 显示全部楼层
不错不错
发表于 2017-3-16 09:14 | 显示全部楼层
楼主是超人
发表于 2017-3-16 09:59 | 显示全部楼层
真心顶
发表于 2017-3-16 09:36 | 显示全部楼层
难得一见的好帖
发表于 2017-3-16 09:18 | 显示全部楼层
很好哦
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-4-30 02:04 , Processed in 0.070379 second(s), 22 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表