当前位置: 首页 > ds >正文

【Unity开发】飞机大战项目实现总结

零、最终效果

飞机大战项目演示

一、需求分析

在这里插入图片描述

二、技术路线确定

UI面板->UGUI实现
数据存储->xml实现
核心逻辑功能->空间坐标转换、碰撞检测、资源加载等

三、关键功能实现

1、将玩家限制在屏幕内活动

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class PlayerControl : MonoBehaviour
{public float speed = 10f;//移动前位置信息private Vector3 frontPos;//当前位置信息private Vector3 nowPos;// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){Move();}void Move(){frontPos = this.gameObject.transform.position;this.gameObject.transform.Translate(Vector3.forward * Input.GetAxis("Vertical") * speed * Time.deltaTime, Space.World);this.gameObject.transform.Translate(Vector3.right * Input.GetAxis("Horizontal") * speed * Time.deltaTime, Space.World);//将当前位置信息转为屏幕坐标位置信息nowPos = Camera.main.WorldToScreenPoint(this.transform.position);//左右超出判断if (nowPos.x<=0|| nowPos.x >=Screen.width){this.transform.position = new Vector3(frontPos.x, this.transform.position.y, this.transform.position.z);}//上下超出判断if (nowPos.y <= 0 || nowPos.y >= Screen.height){this.transform.position = new Vector3(this.transform.position.x, this.transform.position.y, frontPos.z);}}
}

2、子弹的不同运动方式

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class BulletControl : MonoBehaviour
{public GameObject player;private float speed = 10f;private float time;// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){//1、面朝向运动if (Input.GetKey(KeyCode.Keypad0)){this.gameObject.transform.Translate(Vector3.forward * speed * Time.deltaTime);}//2、曲线运动if (Input.GetKey(KeyCode.Keypad1)){//通过sin函数实现time += Time.deltaTime;this.gameObject.transform.Translate(Vector3.right * Mathf.Sin(time) * speed * Time.deltaTime);this.gameObject.transform.Translate(Vector3.forward * speed * Time.deltaTime);}//3、右抛物线运动if (Input.GetKey(KeyCode.Keypad2)){//改变旋转角度this.gameObject.transform.rotation *= Quaternion.AngleAxis(speed * 10 * Time.deltaTime, Vector3.up);this.gameObject.transform.Translate(Vector3.forward * speed * Time.deltaTime);}//4、左抛物线运动if (Input.GetKey(KeyCode.Keypad3)){this.gameObject.transform.rotation *= Quaternion.AngleAxis(-speed * 10 * Time.deltaTime, Vector3.up);this.gameObject.transform.Translate(Vector3.forward * speed * Time.deltaTime);}//5、跟踪目标运动if (Input.GetKey(KeyCode.Keypad4)){//不停计算与玩家之间的方向向量 然后得到四元数,自己的角度不断朝目标四元数进行变换this.gameObject.transform.rotation = Quaternion.Slerp(this.gameObject.transform.rotation, Quaternion.LookRotation(player.transform.position - this.gameObject.transform.position),speed*Time.deltaTime);this.gameObject.transform.Translate(Vector3.forward * speed * Time.deltaTime);}}
}

3、鼠标射线检测销毁物体

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class MouseDestory : MonoBehaviour
{// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){if (Input.GetMouseButton(0)){RaycastHit hitInfo;if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo, 1000)){print(hitInfo.transform.gameObject.layer);if (hitInfo.transform.gameObject.layer==3){Destroy(hitInfo.transform.gameObject);}    }}}
}

4、XML配置文件的读取与存储

XML配置文件的读取与存储学习

http://www.xdnf.cn/news/16339.html

相关文章:

  • Python 程序设计讲义(15):Python 的数据运算——位运算
  • Unity VS Unreal Engine ,“电影像游戏的时代” 新手如何抉择引擎?(1)
  • 读书笔记(黄帝内经)
  • 使用Python采集招聘网站数据并智能分析求职信息
  • P1013 [NOIP 1998 提高组] 进制位
  • ESP32S3 Ubuntu vscode如何使用USB-JTAG调试
  • java中如何返回一个可以执行返回操作(return action)的函数或对象
  • 【自用】JavaSE--阶段测试
  • 基于深度学习的胸部 X 光图像肺炎分类系统(二)
  • 学习设计模式《十九》——享元模式
  • ICCV 2025 | CWNet: Causal Wavelet Network for Low-Light Image Enhancement
  • 主要分布在背侧海马体(dHPC)CA1区域(dCA1)的位置细胞对NLP中的深层语义分析的积极影响和启示
  • LeetCode|Day24|383. 赎金信|Python刷题笔记
  • 【Oracle】Oracle权限迷宫破解指南:2步定位视图依赖与授权关系
  • QML WorkerScript
  • 高版本Android跨应用广播通信实例
  • MBPO 算法:让智能体像人一样 “先模拟后实操”—强化学习(17)
  • Linux进程间通信:管道机制全方位解读
  • 卫星物联网:使用兼容 Arduino 的全新 Iridium Certus 9704 开发套件深入探索
  • 如何判断钱包的合约签名是否安全?
  • MySQL基础02
  • 常见半导体的介电常数
  • 【ROS1】09-ROS通信机制——参数服务器
  • 接口多态之我的误解
  • 高可用架构模式——异地多活设计步骤
  • k8s之ingress定义https访问方式
  • 精通Python PDF裁剪:从入门到专业的三重境界
  • Vue工程化 ElementPlus
  • 分布式推客系统开发全解:微服务拆分、佣金结算与风控设计
  • 强制缓存与协商缓存