Unity - A计划(永久有效期) 扫二维码继续学习 二维码时效为半小时

(197评价)
价格: 4431.00元
关于手机重力感应控制的问题
kongzhixin发起了问答2017-08-24
2
回复
407
浏览
public class PlayerController : MonoBehaviour
{
	public float turnSpeed = 10.0f;//转弯速度
	public float sensitivity = 1.0f;//敏感度,定义旋转时lerp差值的时间
	public float forwardForce = 50.0f;//向前力的大小
	private float maxTurnLean = 50.0f;
	private float maxTilt = 50.0f;
	private float normalizedSpeed = 0.2f;//飞行速度
	private Vector3 euler=Vector3.zero;
	public Transform guiSpeedElement;


	// Use this for initialization
	void Start () {
		Screen.orientation=ScreenOrientation.AutoRotation;//方向自动
		guiSpeedElement.position=new Vector3(30,normalizedSpeed*Screen.height,0);//图标的初始位置
	}
	
	// Update is called once per frame
	void Update () {
		foreach (Touch touch in Input.touches)
		{
			if (touch.phase == TouchPhase.Moved)
			{
				normalizedSpeed = touch.position.y / Screen.height;
				guiSpeedElement.position=new Vector3(30,touch.position.y,0);
			}
		}
	}

	void FixedUpdate()
	{
		GetComponent< Rigidbody>().AddRelativeForce(0,0,normalizedSpeed*forwardForce);
		Vector3 acc = Input.acceleration;
		euler.y = acc.x * turnSpeed;
		euler.z = Mathf.Lerp(euler.z, -acc.x * maxTurnLean, 0.2f);
		euler.x = Mathf.Lerp(euler.x, acc.y * maxTurnLean, 0.2f);
		Quaternion rot = Quaternion.Euler(euler);
		transform.rotation=Quaternion.Lerp(transform.rotation,rot,sensitivity);
	}
}

这是通过手机转动控制飞船传动的代码, 主要问题在FixedUpdate 部分,euler.xyz应该如何于input.accleration 的方向对应.

euler.y = acc.x * turnSpeed;

euler.z = Mathf.Lerp(euler.z, -acc.x * maxTurnLean, 0.2f);

euler.x = Mathf.Lerp(euler.x, acc.y * maxTurnLean, 0.2f);    这些 不太 明白

所有回复
发表回复
你还没有登录,请先 登录或 注册!