游戏展示
游戏开发
创建 2D 游戏
略
设置分辨率 480x800
略
导入资源
略
Box / 箱子
从 Project 拖动 Box 资源到 Hierarchy
- Inspector
- Rect Transform: reset(图片没有 reset)
- Scale
- X:
0.2
- Y:
0.2
- X:
- Scale
- Rect Transform: reset(图片没有 reset)
BG / 背景
从 Project 拖动 Box 资源到 Hierarchy > Main Camera
Tags
选中 Box
, 添加 3 种 Tag
- Box
- Platform
- Game Over
为 Box
添加 Tag: Box
添加 Box Collider 2D
- Inspector
- Box Collider 2D
- Size
- X:
4.85
- Y:
4.85
- X:
- Size
- Box Collider 2D
添加 RigidBody 2D
- Inspector
- RigidBody 2D
- Size
- Mess:
1
- Linear Drag:
2
- Angular Drag:
3
- Mess:
- Size
- RigidBody 2D
Game Over
- Hirearchy
- Main Camera
- Create Empty:
Game Over
- Create Empty:
- Main Camera
- Inspector
- Tag:
Game Over
- Rect Transform
- Position
- Y:
-5.18
- Y:
- Position
- Box Collider 2D
- Is Trigger:
True
- Size
- X:
26
- Y:
0.15
- X:
- Is Trigger:
- Tag:
Box Spawner / 箱子生成器
- Hirearchy
- Main Camera
- Create Empty:
Box Spawner
- Create Empty:
- Main Camera
- Inspector
- Rect Transform
- Position
- Y:
3.7
- Y:
- Position
- Rect Transform
Platform Holder
从 Project 拖动 Box 资源到 Hierarchy,重命名为:Platform Holder
- Inspector
- Tag:
Game Over
- Rect Transform
- Position
- Y:
-4.8
- Y:
- Scale
- X:
0.7
- Y:
0.07
- X:
- Position
- Add Component +=
Box Collider 2D
- Tag:
Prefabs
创建新文件夹,Prefabs
- Project
- Assets
- Create
- Folder:
Prefabs
- Folder:
- Create
- Assets
从 Hierarchy 拖动 Box 到 Prefabs
删除 Hierarchy > Box
Scripts
创建新文件夹,Scripts
- Project
- Assets
- Create
- Folder:
Scripts
- Folder:
- Scripts
- Create
- C# Script
BoxScript
GameplayController
BoxSwapner
CameraFollow
- C# Script
- Create
- Create
- Assets
附加脚本
- Hierarchy
- Main Camera +=
CameraFollow
- Box Swapner +=
BoxSwapner
- Box Swapner +=
- Create:
Gameplay Controller
- Gameplay Controller +=
GameplayController
- Main Camera +=
- Project
- Assets
- Prefabs
- Box ->
Inspector
- Box ->
- Prefabs
- Assets
- Inspector
- Open Prefabs
* Add Component += `BoxScript`
- Open Prefabs
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 | [ ] |
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)
- Player Settings
小结
两个物体 A,B 两者都有碰撞体 collider(Box Collider,Sphere Collider,Capsule Collider 等)当两物体相撞时,会进入 OnTriggerEnter()
和 OnCollisionEnter()
中的哪一个呢?
- 只会进入一个,A 和 B 的脚本只会进入同一个
- A 或者 B collider 有一个勾选 isTrigger 或者两者都勾选
isTrigger
A 和 B都可以进入OnTriggerEnter()
方法,但是不可进入OnCollisionEnter方法。 - A 和 B collider 都不勾选 isTrigger,A 和 B 能进入
OnCollisionEnter()
方法但是不能进入OnTriggerEnter()
方法。
A isTrigger | B isTrigger | OnCollisionEnter | OnTriggerEnter |
---|---|---|---|
✅ | ✅ | ✅ | |
✅ | ✅ | ||
✅ | ✅ | ||
✅ |
下载
游戏
Release: https://github.com/GameDevLog/GameDevLogTemplete/releases