Box Tower / 堆箱塔

游戏展示

Box Tower

游戏开发

创建 2D 游戏

设置分辨率 480x800

导入资源

Box / 箱子

Project 拖动 Box 资源到 Hierarchy

  • Inspector
    • Rect Transform: reset(图片没有 reset)
      • Scale
        • X: 0.2
        • Y: 0.2

BG / 背景

Project 拖动 Box 资源到 Hierarchy > Main Camera

Tags

选中 Box, 添加 3 种 Tag

  1. Box
  2. Platform
  3. Game Over

Box 添加 Tag: Box

添加 Box Collider 2D

  • Inspector
    • Box Collider 2D
      • Size
        • X: 4.85
        • Y: 4.85

添加 RigidBody 2D

  • Inspector
    • RigidBody 2D
      • Size
        • Mess: 1
        • Linear Drag: 2
        • Angular Drag: 3

Game Over

  • Hirearchy
    • Main Camera
      • Create Empty: Game Over
  • Inspector
    • Tag: Game Over
    • Rect Transform
      • Position
        • Y: -5.18
    • Box Collider 2D
      • Is Trigger: True
      • Size
        • X: 26
        • Y: 0.15

Box Spawner / 箱子生成器

  • Hirearchy
    • Main Camera
      • Create Empty: Box Spawner
  • Inspector
    • Rect Transform
      • Position
        • Y: 3.7

Platform Holder

Project 拖动 Box 资源到 Hierarchy,重命名为:Platform Holder

  • Inspector
    • Tag: Game Over
    • Rect Transform
      • Position
        • Y: -4.8
      • Scale
        • X: 0.7
        • Y: 0.07
    • Add Component += Box Collider 2D

Prefabs

创建新文件夹,Prefabs

  • Project
    • Assets
      • Create
        • Folder: Prefabs

Hierarchy 拖动 BoxPrefabs

删除 Hierarchy > Box

Scripts

创建新文件夹,Scripts

  • Project
    • Assets
      • Create
        • Folder: Scripts
      • Scripts
        • Create
          • C# Script
            • BoxScript
            • GameplayController
            • BoxSwapner
            • CameraFollow

附加脚本

  • Hierarchy
    • Main Camera += CameraFollow
      • Box Swapner += BoxSwapner
    • Create: Gameplay Controller
    • Gameplay Controller += GameplayController
  • Project
    • Assets
      • Prefabs
        • Box -> Inspector
  • Inspector
    • Open Prefabs
      * Add Component += `BoxScript`

BoxSwapner Script

1
using System.Collections;
2
using System.Collections.Generic;
3
using UnityEngine;
4
5
public class BoxSwapner : MonoBehaviour
6
{
7
    public GameObject boxPrefabs;
8
9
    void Start()
10
    {
11
        SwapBox();
12
    }
13
14
    public void SwapBox()
15
    {
16
        GameObject boxObject = Instantiate(boxPrefabs);
17
18
        boxObject.transform.position = transform.position;
19
    }
20
}

Game Controller

删除 BoxSwapner Script 中的 Start()

1
diff --git a/Box-Tower/Assets/Scripts/BoxSwapner.cs b/Box-Tower/Assets/Scripts/BoxSwapner.cs
2
index 66f02dc..102da62 100644
3
--- a/Box-Tower/Assets/Scripts/BoxSwapner.cs
4
+++ b/Box-Tower/Assets/Scripts/BoxSwapner.cs
5
@@ -6,11 +6,6 @@ public class BoxSwapner : MonoBehaviour
6
 {
7
     public GameObject boxPrefabs;
8
 
9
-    void Start()
10
-    {
11
-        SwapBox();
12
-    }
13
     public void SwapBox()
14
     {
15
         GameObject boxObject = Instantiate(boxPrefabs);

BoxScript.cs

1
using UnityEngine;
2
3
public class BoxScript : MonoBehaviour
4
{
5
    private float minX = -2.2f;
6
    private float maxX = 2.2f;
7
8
    private bool canMove;
9
    private float speed = 2f;
10
11
    private Rigidbody2D myBody;
12
13
    private bool gameOver;
14
    private bool ignoreCollision;
15
    private bool ignoreTrigger;
16
17
    private void Awake()
18
    {
19
        myBody = GetComponent<Rigidbody2D>();
20
        myBody.gravityScale = 0f;
21
    }
22
23
    void Start()
24
    {
25
        canMove = true;
26
27
        if (Random.Range(0, 2) > 0)
28
        {
29
            speed *= -1f;
30
        }
31
32
        GameplayController.instance.currentBox = this;
33
    }
34
35
    // Update is called once per frame
36
    void Update()
37
    {
38
        MoveBox();
39
    }
40
41
    private void MoveBox()
42
    {
43
        if (canMove)
44
        {
45
            Vector3 temp = transform.position;
46
            temp.x += speed * Time.deltaTime;
47
48
            if (temp.x > maxX)
49
            {
50
                speed *= -1f;
51
52
            }
53
            else if (temp.x < minX)
54
            {
55
                speed *= -1f;
56
            }
57
58
            transform.position = temp;
59
        }
60
    }
61
}

GameplayController.cs

1
using UnityEngine;
2
3
public class GameplayController : MonoBehaviour
4
{
5
    public static GameplayController instance;
6
7
    public BoxSwapner boxSwapner;
8
9
    [HideInInspector]
10
    public BoxScript currentBox;
11
12
    public CameraFollow cameraScript;
13
    private int moveCount;
14
15
    private void Awake()
16
    {
17
        if (instance == null)
18
        {
19
            instance = this;
20
        }
21
    }
22
23
    private void Start()
24
    {
25
        boxSwapner.SpawnBox();
26
    }
27
}

效果:箱子可以左右来回的移动

完善代码

具体代码请查看 GitHub

导出游戏

  • File
    • Build Settings (Shift + Cmd + B)

Build Settings

  • Player Settings

小结

两个物体 A,B 两者都有碰撞体 collider(Box Collider,Sphere Collider,Capsule Collider 等)当两物体相撞时,会进入 OnTriggerEnter()OnCollisionEnter() 中的哪一个呢?

  1. 只会进入一个,A 和 B 的脚本只会进入同一个
  2. A 或者 B collider 有一个勾选 isTrigger 或者两者都勾选 isTrigger A 和 B都可以进入 OnTriggerEnter() 方法,但是不可进入OnCollisionEnter方法。
  3. A 和 B collider 都不勾选 isTrigger,A 和 B 能进入 OnCollisionEnter() 方法但是不能进入 OnTriggerEnter() 方法。
A isTrigger B isTrigger OnCollisionEnter OnTriggerEnter

下载

游戏

Release: https://github.com/GameDevLog/GameDevLogTemplete/releases

源码

GitHub: https://github.com/GameDevLog/GameDevLogTemplete

本文标题:Box Tower / 堆箱塔

文章作者:iOSDevLog

发布时间:2019年12月06日 - 19:44:46

最后更新:2019年12月19日 - 15:40:59

原始链接:https://game.iosdevlog.com/2019/12/06/Box-Tower/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

iOSDevLog wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!