PlaneDemo

作者: 亮剑无悔 | 来源:发表于2020-05-14 16:50 被阅读0次

    用到知识点:坐标系转换、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

    相关文章

      网友评论

          本文标题:PlaneDemo

          本文链接:https://www.haomeiwen.com/subject/jrbgnhtx.html