当前位置: 首页 > 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.lryc.cn/news/598166.html

相关文章:

  • DigitalOcean 一键模型部署,新增支持百度开源大模型ERNIE 4.5 21B
  • Socket编程入门:从IP到端口全解析
  • element-plus 组件 ElMessage、ElLoading 弹框 和加载css 样式展示异常总结
  • SQL基础⑫ | 视图篇
  • 若用dnf下载的nginx和源文件下载的nginx冲突
  • 【学习路线】JavaScript全栈开发攻略:前端到后端的完整征程
  • Baumer工业相机堡盟工业相机如何通过YoloV8深度学习模型实现卫星图像识别(C#代码,UI界面版)
  • 20-ospf技术
  • Java Map.Entry 核心解析
  • IPSec VPN -- 野蛮模式
  • OSPF开放式最短路径优先
  • C# 泛型(泛型方法)
  • Python中常用标准库(时间库、随机库、正则表达式)
  • pytest官方Tutorial所有示例详解(一)
  • 基于Node.js开发的开源博客平台ghost安装和使用
  • MySQL高可用部署
  • 云原生MySQL Operator开发实战(一):Operator基础与CRD设计
  • Spring MVC中常用注解_笔记
  • nuxt更改页面渲染的html,去除自定义属性、
  • 【Word Press进阶】自定义区块的行为与样式
  • go项目实战二
  • Linux应用开发基础知识——进程学习2(exec函数、system函数、popen函数)(三)
  • 数据挖掘顶刊TKDE论文分享│ST-LLM+:面向交通预测的图增强时空大语言模型
  • 第五章 Freertos物联网实战 微信小程序篇
  • 从0开始学习R语言-Day56--空间变系数模型
  • Django基础(八)———数据库外键及表关系
  • Transformer Masked loss原理精讲及其PyTorch逐行实现
  • Kubernetes 集群架构和Pod创建流程
  • 【unity游戏开发入门到精通——组件篇】unity的粒子系统力场 (Particle System Force Field)实现如旋风、吸引力、风吹效果等
  • Unity GC 系列教程第四篇:GC Alloc 优化技巧与实践(下)与 GC 调优