该实例是Unity实现在移动端通过触摸屏幕进行对飞机的移动和转向,用于学习Touch类的相关知识
public class AirControl:MonoBehaviour{
private Transform m_transform; //飞机实例
public float speed = 500f; //飞行速度
private float rotationz = 0.0f; //Z轴旋转量
public float rotateSpeed_AxisZ=45f; //绕Z轴旋转速度
private Vector2 touchPosition; //触摸点坐标
private float screenWeight; //屏幕宽度
void Start () {
m_transform = this.transform;
this.gameObject.GetComponent<Rigidbody>().useGravity = false; //取消重力
screenWeight = Screen.width; //获取屏幕宽度
}
void Update () {
//设置速度
m_transform.Translate(new Vector3(0, 0, speed * Time.deltaTime));//设置飞机飞行速度
m_transform.Find("propeller").transform.Rotate(new Vector3(0, 1000f * Time.deltaTime, 0));//设置螺旋桨旋转速度
#region 控制转向
rotationz = m_transform.eulerAngles.z; //获取飞机对象绕Z轴的旋转量
if (Input.touchCount > 0) //当触摸数量大于0
{
for(int i=0;i<Input.touchCount;i++)