Unity3D模型重力感应

关于unity3d模型的重力感应,在这里我们使用unity引擎提供的Input.acceleration重力感应;
例如:
public Transform m_round;
      Transform trans;

      //小球在屏幕的最大范围
      float cross_x=0;
      float cross_y=0;
      float cross_z=0;
      float m_x=0;
      float m_y=0;
      Vector3 screenpioint;
// Use this for initialization
void Start () {

            trans = ( Transform ) Instantiate ( m_round, m_round.position, Quaternion.identity );
         m_x= trans.GetComponent ( ).mesh.bounds.size.x * trans.localScale.x;
           m_y = trans.GetComponent ( ).mesh.bounds.size.y * trans.localScale.y;
           cross_x = Screen.width - m_x;
           cross_y = Screen.width - m_y;
}
      void OnGUI ( )
      {
            //整体显示x,y,z的重力分量
            GUI.Label ( new Rect ( 0, 0, 400, 200 ), "position is" + Input.acceleration + "--x" + trans.GetComponent ( ).mesh.bounds.size.x * trans.localScale.x + "--" + trans.GetComponent ( ).mesh.bounds.size.y * trans.localScale.y );
            GUI.Label ( new Rect ( 2, 30, 400, 200 ), "screenpioint.x is" + screenpioint.x + "screenpioint.y is" + screenpioint.y );
      }
          
// Update is called once per frame
void Update () {
           // 根据重力分量修改小球的位置
       
            screenpioint=Camera.main.WorldToScreenPoint ( trans.position );
            screenpioint.x += Input.acceleration.x * 100 * Time.deltaTime;
            screenpioint.y -= Input.acceleration.y * 100 * Time.deltaTime;

            if (screenpioint.x <= m_x*2)
                  screenpioint.x = m_x*2;
            if (screenpioint.y <=m_y*2)
                  screenpioint.y = m_y*2;
            if (screenpioint.x >= cross_x)
                  screenpioint.x = cross_x;
            if (screenpioint.y >= cross_y)
                  screenpioint.y = cross_y;

            trans.position = Camera.main.ScreenToWorldPoint ( screenpioint );
}
Unity3D模型重力感应
快来试试吧,哇咔咔!
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tianyongheng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值