unity重力感应控制摄像机旋转

有时候我们要用到重力感应,这个百度就能搜到他的解释或是原理,但具体怎么用,还需自己去实践,本人就重力感应控制摄像机旋转做一个举例:因为效果需要平板或是手机测试,所以效果图就不放了,感兴趣的朋友可以自己测试一下!
直接附上源码
 
using UnityEngine;
using System.Collections;
public class zhongLiTest : MonoBehaviour {
 public float xRotation =0F;
 public float yRotation =0F;
 void Update() {
  xRotation += Input.acceleration.x;
  yRotation += Input.acceleration.y;
  transform.eulerAngles = new Vector3(yRotation, xRotation, 0);
  if (xRotation < -5)
   xRotation = -5;
  else if (xRotation >5)
   xRotation = 5;
  if (yRotation < -5)
   yRotation = -5;
  else if (yRotation >5)
   yRotation = 5;
 }
}

你可能感兴趣的:(unity)