记录步骤:win+R→PSR.exe


一、竖屏设置

分辨率设为9:16

二、主控脚本

  • 添加一个空节点,命名“游戏主控”
  • 新建游戏的主控脚本,命名为MyGame.cs,方便管理(即,一些全局性的逻辑控制)
  • 如有必要,修改主控脚本的Execution Order(优先级)
 1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4
5 public class MyGame : MonoBehaviour
6 {
7 //添加全局性的设置
8 // Start is called before the first frame update
9 void Start()
10 {
11 Application.targetFrameRate = 60;//帧数设为60
12 }
13
14 // Update is called once per frame
15 void Update()
16 {
17
18 }
19 }

三、制作子弹

  • 制作预制体

子弹运动:

 1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4
5 public class Mybullet : MonoBehaviour
6 {
7 public float speed = 3.5f;
8 // Start is called before the first frame update
9 void Start()
10 {
11
12 }
13
14 // Update is called once per frame
15 void Update()
16 {
17 float dy = speed * Time.deltaTime;
18 transform.Translate(0, dy, 0, Space.Self);
19 Vector3 sp = Camera.main.WorldToScreenPoint(transform.position);
20 if (sp.y > Screen.height)
21 {
22 Destroy(this.gameObject);
23 }
24 }

四、制作飞机

  • 控制飞机左右移动
  • 添加预制体资源:public GameObject myPrefab;
  • 断开预制体:unpack Prefab
 1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4
5 public class Myplane : MonoBehaviour
6 {
7 public GameObject MyPrefab;
8 private float interval=0.4f;//间隔时间
9 private float count = 0;
10
11 // Start is called before the first frame update
12 void Start()
13 {
14 Application.targetFrameRate = 60;
15 }
16
17 // Update is called once per frame
18 void Update()
19 {
20 count += Time.deltaTime;
21 if (count >= interval)
22 {
23 count = 0;
24 Fire();
25 }
26 //键盘控制飞机的左右移动
27 float step = 2.5f * Time.deltaTime;
28 if (Input.GetKey(KeyCode.LeftArrow))
29 {
30 transform.Translate(-step, 0, 0);
31 }
32 if (Input.GetKey(KeyCode.RightArrow))
33 {
34 transform.Translate(step, 0, 0);
35 }
36 }
37 void Fire()
38 {
39 GameObject bullet = Instantiate(MyPrefab);
40 bullet.transform.position = transform.position + new Vector3(0, 1f, 0);
41 }
42 }

五、制作怪物

  • 添加到游戏主控MonsterCtrl.cs
  • 不断生成
  • 随机头像
  • 大小优化
  • 随机位置
 1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4
5 public class Mymonster : MonoBehaviour
6 {
7 public float speed = 1.0f;
8 // Start is called before the first frame update
9 void Start()
10 {
11
12 }
13
14 // Update is called once per frame
15 void Update()
16 {
17 float dy = speed * Time.deltaTime;
18 transform.Translate(0, -dy, 0,Space.Self);
19 Vector3 sp = Camera.main.WorldToScreenPoint(transform.position);
20 if (sp.y <0)
21 {
22 Destroy(this.gameObject);
23 }
24 }
25 }
 1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4
5 public class MonsterCtrl : MonoBehaviour
6 {
7 public GameObject monsterPerfab;
8
9 public Sprite[] images;
10
11 // Start is called before the first frame update
12 void Start()
13 {
14 InvokeRepeating("CreatMonster", 0.1f, 2f);
15 }
16
17 // Update is called once per frame
18 void Update()
19 {
20
21 }
22
23 void CreatMonster()
24 {
25 float x = Random.Range(-2, 2);
26 float y = 5;
27 GameObject monster = Instantiate(monsterPerfab);
28 monster.transform.position = new Vector3(x, y, 0);
29
30
31 //随机怪物
32 int index = Random.Range(0, images.Length);
33
34 SpriteRenderer renderer = monster.GetComponent<SpriteRenderer>();
35 renderer.sprite = this.images[index];
36
37 //怪物大小设为100px(像素)
38 Sprite sprite = this.images[index];
39 float imgWidth = sprite.rect.width; // 图像的实际宽度
40 float scale = 100/imgWidth; //缩放比例
41 monster.transform.localScale = new Vector3(scale, scale, scale);
42 }
43 }

六、击中目标

  • “子弹”与“怪物”之间的碰撞处理:
  1. 添加Rigidbody 2D及Collider 2D
  2. 添加Tag:player,Bullet,Monster(用于身份识别
  3. 修改MyBullet.cs,添加碰撞处理
 1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4
5 public class Mybullet : MonoBehaviour
6 {
7 public float speed = 3.5f;
8 // Start is called before the first frame update
9 void Start()
10 {
11
12 }
13
14 // Update is called once per frame
15 void Update()
16 {
17 float dy = speed * Time.deltaTime;
18 transform.Translate(0, dy, 0, Space.Self);
19 Vector3 sp = Camera.main.WorldToScreenPoint(transform.position);
20 if (sp.y > Screen.height)
21 {
22 Destroy(this.gameObject);
23 }
24 }
25 private void OnTriggerEnter2D(Collider2D collision)
26 {
27 if (collision.tag.Equals("Monster"))
28 {
29 Destroy(collision.gameObject);
30 Destroy(this.gameObject);
31 }
32 }
33 }

七、游戏背景

  • 添加到游戏主控background.cs
  • Order in Layor:-10
  • 俩图片无缝连接:Deplicate(复制)
 1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4
5 public class background : MonoBehaviour
6 {
7
8 Transform bg1;
9 Transform bg2;
10
11 public float speed = 1.0f;//向下运动速度
12 // Start is called before the first frame update
13 void Start()
14 {
15 //找出变化路径
16 bg1 = GameObject.Find("/背景/bg1").transform;
17 bg2 = GameObject.Find("/背景/bg2").transform;
18
19 //手动确定背景图片的位置从而无缝连接
20 bg1.position = new Vector3(0, 0, 0);
21 bg2.position = new Vector3(0, 10, 0);
22 }
23
24 // Update is called once per frame
25 void Update()
26 {
27 float dy = speed * Time.deltaTime;
28 bg1.Translate(0, -dy, 0);
29 bg2.Translate(0, -dy, 0);
30 //图片循环显示
31 if (bg1.position.y <= -10)
32 {
33 bg1.Translate(0, 20, 0);
34
35 }
36 if (bg2.position.y <= -10)
37 {
38 bg2.Translate(0, 20, 0);
39 }
40 }
41 }

  • 物理系统:Physics

由物理规律起作用的系统:刚体(RigidBody)→指物理系统中的物体(具有质量、速度、可反弹...)

Add Component→Physics 2D→Rigidbody 2D

刚体有三种类型:

Dynamic:普通刚体,有质量,有速度

Static:静态刚体,质量无穷大,无速度(适用于建筑物、地面等固定不动的物体)

Kinematic:运动学刚体,无质量(忽略物理规律的刚体,一般用于碰撞检测)

只有添加撞体(Collider)组件后,才有碰撞的计算

Add Component→Physics 2D→Box Collider 2D

    • 方形:Box Collider 2D
    • 圆形:Circle Collider 2D
    • 不规则边缘:Edge Collider 2D
    • 胶囊形状:Capsule Collider 2D

刚体的反弹:

  1.在project窗口里,Create→Physics Material 2D...

    • Friction:摩擦系数
    • Bounciness:弹性系数,设为1(完全反弹(无能量损耗)) 

    2.选择“皮球”的Rigidbody2D组件

    • Material:选择....材质  

    3.运行游戏,观察反弹

Unity_飞机大战记录总结的更多相关文章

  1. java版飞机大战 实战项目详细步骤.md

    [toc] 分析 飞机大战 首先对这个游戏分析,在屏幕上的物体都是飞行物,我们可以把建一个类,让其他飞行物继承这个类.游戏中应有英雄机(也就是自己控制的飞机).敌人.而敌人应该分为打死给分的飞机(就是 ...

  2. 用面向对象的编程方式实现飞机大战小游戏,java版

    概述 本文将使用java语言以面向对象的编程方式一步一步实现飞机大战这个小游戏 本篇文章仅供参考,如有写的不好的地方或者各位读者哪里没看懂可以在评论区给我留言 或者邮件8274551712@qq.co ...

  3. Java飞机大战MVC版

    PlaneWar Java飞机大战MVC版 //无聊时偷的雷霆战机素材写了一个飞机大战,本意是练习mvc,但写得还是不清晰 github下载:https://github.com/dejavudwh/ ...

  4. python 飞机大战 实例

    飞机大战 #coding=utf-8 import pygame from pygame.locals import * import time import random class Base(ob ...

  5. bayaim_杀神_全民飞机大战

    bayaim_杀神_全民飞机大战 ------------------------ 系统:IOS QQ:7742469490 王者:30 游戏名:神 级别:98 装备:满鸡 + 新战魂+ 歼31 + ...

  6. python之基础总结(飞机大战)

    一.学习python有一段时间了,总体上手还是挺好的,但是有些东西还是和Java存在着一定的区别,这里主要是通过学习,然后自己去编写一个案例.从中学习到的一些东西,这里分享出来,如果存在不正确的地方还 ...

  7. 23飞机大战__pygame 快速入门

      1. 使用 pygame 创建图形窗口 小节目标 游戏的初始化和退出 理解游戏中的坐标系 创建游戏主窗口 简单的游戏循环 可以将图片素材 绘制 到 游戏的窗口 上, 开发游戏之前需要先知道 如何建 ...

  8. python版飞机大战代码简易版

    # -*- coding:utf-8 -*- import pygame import sys from pygame.locals import * from pygame.font import ...

  9. 飞机大战-面向对象-pygame

    飞机大战 最近学习了python的面向对象,对面向对象的理解不是很深刻. 面向对象是数据和函数的'打包整理',将相关数据和处理数据的方法集中在一个地方,方便使用和管理. 本着学习的目的,在网上找了这个 ...

  10. Java实现飞机大战游戏

    飞机大战详细文档 文末有源代码,以及本游戏使用的所有素材,将plane2文件复制在src文件下可以直接运行. 实现效果: 结构设计 角色设计 飞行对象类 FlyObject 战机类 我的飞机 MyPl ...

随机推荐

  1. .NET 7介绍及环境准备

    环境要求 VS2022 17.4+

  2. ASP.NET Core中使用日志组件NLog

    上一篇文章描述了如何在ASP.NET Core中使用Log4Net记录日志.本篇将使用另外一个组件NLog在ASP.NET Core中记录日志. 1.引入程序集 NLog.Web.AspNetCore ...

  3. Oracle数据泵恢复用户数据实例

    我们测试环境经常会遇到恢复生产数据的情况,我一般比较习惯使用数据泵来搞,这个具体根据自己的业务形态选择适合自己的方式. 此次我们说的是完全恢复用户数据,具体步骤如下: 1.备份数据 expdp tes ...

  4. linux离线安装gcc 和g++

    1.先到有网的机器上下载依赖包 sudo yum install --downloadonly --downloaddir=/home/mjb/soft/gcc gcc sudo yum instal ...

  5. Mac下Apache Tomcat安装配置技巧

    我们在MAC系统中查看网页时,一般都要使用到tomcat,这是因为appache只支持静态网页,但像asp,php,cgi,jsp等动态就需要tomcat来处理.那么该怎么在自己的MAC中安装tomc ...

  6. vue项目中配置svg图标 cli3路径

    1 添加依赖 npm install svg-sprite-loader file-loader -D 2 在components目录下新增一个IconSvg.vue文件 <template&g ...

  7. Windows MFC HTTP 函数流程

    Windows MFC HTTP 函数流程 1 //建立连接 2 pInternetSession = new CInternetSession(AfxGetAppName()); 3 4 5 6 / ...

  8. redis - 常用方法封装总结

    package com.citydo.utils; import org.springframework.data.redis.connection.DataType; import org.spri ...

  9. react+antd upload实现图片宽高、视频宽高尺寸校验

    图片宽高校验方法: // 上传图片尺寸限制 const checkIconWH = (file: any) => { return new Promise<void>(functio ...

  10. Debug --> 攻击方式

    1.CSRF攻击概述: CSRF(Cross Site Request Forgery, 跨站域请求伪造)是一种网络的攻击方式,它在 2007 年曾被列为互联网 20 大安全隐患之一. CSRF攻击原 ...