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

【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.dtcms.com/a/295665.html

相关文章:

  • Unity GC 系列教程第四篇:GC Alloc 优化技巧与实践(下)与 GC 调优
  • DBA常用数据库查询语句(2)
  • 【学习路线】JavaScript全栈开发攻略:前端到后端的完整征程
  • Redis数据库入门教程
  • Windchill用SQL获取所有组织下的所有用户
  • C++11之可变参数模板
  • ac日志报ARP-neighbor-failed问题定位过程
  • langchain+本地embedding模型+milvus实现RAG
  • ChatGPT Agent架构深度解析:OpenAI如何构建统一智能体系统
  • 青少年编程学习的新选择——《CCF GESP 直通车》与《GESP 编程能力等级认证一本通》深度剖析
  • 根据字符串数组的顺序重新排序 List顺序
  • 中国历史朝代顺序以及朝代歌
  • 核心数据结构:DataFrame
  • 【硬件-笔试面试题】硬件/电子工程师,笔试面试题-18,(知识点:传输线阻抗匹配方式)
  • OpenAI最新大模型GPT-4o体验之Code Copilot AI编程大模型
  • 电子书转PDF格式教程,实现epub转PDF步骤
  • Java 大视界 -- Java 大数据在智能家居能源管理与节能优化中的深度应用(361)
  • 多模态数据处理系统:用AI读PDF的智能助手系统分析
  • Maven Scope标签:解锁Java项目依赖管理的秘密武器
  • 安全逆向工程学习路线
  • 《Maven 核心基础笔记(第一天)》
  • 使用maven-shade-plugin解决依赖版本冲突
  • gitlab使用 备份恢复 全量迁移
  • 《从点击到共鸣:论坛前端如何用交互细节编织用户体验》
  • window下lua解释器安装并配置vscode环境
  • 【Practical Business English Oral Scene Interpretation】入职面试No.5~7
  • 承担CANOPEN转PROFINET协议转换功能的网关与台达伺服器的连接
  • 80道面试经典题目
  • 循环神经网络(RNN)详解:从原理到实践
  • rust-结构体使用示例