我正在为Unity5.1.1f1中的Android开发迷宫游戏,我在用加速度计控制我的球时遇到了麻烦。一开始我试过:
public class PlayerController : MonoBehaviour {
public GameObject sphere;
public Camera camera;
public float speed=200;
private Rigidbody myRigidBody;
void Start()
{
myRigidBody = gameObject.GetComponent<Rigidbody> ();
}
void FixedUpdate()
{
float moveH = Input.acceleration.x;
float moveV = -Input.acceleration.z;
Vector3 move = new Vector3 (moveH, 0.0f, moveV);
myRigidBody.AddForce (move * speed*Time.deltaTime);
}
}但是球并没有像它应该的那样移动。然后我又尝试了另一个解决方案。但我的球还是不能向右移动。似乎有时很难左/右/向前/向后移动,也有可能我的球会向相反的方向旋转。
public class PlayerController : MonoBehaviour {
public float speedAc = 10;
//accelerometer
private Vector3 zeroAc;
private Vector3 curAc;
private float sensH = 10;
private float sensV = 10;
private float smooth = 0.5f;
private float GetAxisH = 0;
private float GetAxisV = 0;
// Use this for initialization
void Start () {
ResetAxes();
}
//accelerometer
void ResetAxes(){
zeroAc = Input.acceleration;
curAc = Vector3.zero;
}
void FixedUpdate () {
curAc = Vector3.Lerp(curAc, Input.acceleration-zeroAc, Time.deltaTime/smooth);
GetAxisH = Mathf.Clamp(curAc.x * sensH, -1, 1);
GetAxisV = Mathf.Clamp(-curAc.z * sensV, -1, 1);
Vector3 movement = new Vector3 (GetAxisH, 0.0f, GetAxisV);
GetComponent<Rigidbody>().AddForce(movement * speedAc);
}
}有人能帮帮我吗?
发布于 2015-11-02 11:35:42
我在另一个问题中回答了这个问题:Unity 3D realistic accelerometer control --这里有两个控件变体,只需复制您认为合适的一个。
如果你有更多的问题,问。
https://stackoverflow.com/questions/33467828
复制相似问题