用到知识点:坐标系转换、Input类
using System.Collections.Generic;
using UnityEngine;
///<summary>
///控制飞机飞行,边界设定
///</summary>
public class Plane : MonoBehaviour
{
private float hor;
private float ver;
private Vector3 dir;
public float flySpeed=1f;
private void Update()
{
hor=Input.GetAxis("Horizontal");
ver = Input.GetAxis("Vertical");
Vector3 screenPoint = Camera.main.WorldToScreenPoint(this.transform.position);
if(screenPoint.x<0&&hor<0)screenPoint.x = Screen.width;
if (screenPoint.x > Screen.width && hor > 0) screenPoint.x = 0;
this.transform.position=Camera.main.ScreenToWorldPoint(screenPoint);
if (screenPoint.y > Screen.height&&ver>0||screenPoint.y<0&&ver<0) ver = 0;
dir = new Vector3(hor, 0, ver);
this.transform.position += dir * flySpeed * Time.deltaTime;
}
}
PlaneDemo.png
网友评论