Hexagon

游戏展示

Hexagon

游戏开发

创建 2D 游戏

Main Camera

  • Inspector
    • Camera
      • Background: red

New Sprite

  • Hierarchy
    • Create
      • Sprite: Home
  • Inspector
    • Transform: reset
      • Scale
        • Y: 3.8
        • X: 3.8
    • Sprite Renderer
      • Sprite: Knob
      • Color: black alpha: 40 #00000028

Player

  • Hierarchy
    • Create
      • Sprite: Player
  • Inspector
    • Transform: reset
      • Position
        • Y: 0.6
    • Sprite Renderer
      • Sprite: Knob
    • Rigidbody 2D
      • Body Type
        • Kinematic
    • Circle Collider 2D
      • Is Trigger: True

Script

Player.cs

1
using UnityEngine;
2
3
public class Player : MonoBehaviour
4
{
5
    public float moveSpeed = 600f;
6
    private float movement;
7
8
    void Update()
9
    {
10
        movement = Input.GetAxis("Horizontal");
11
    }
12
13
    private void FixedUpdate()
14
    {
15
        transform.RotateAround(Vector3.zero, Vector3.back, movement * Time.fixedDeltaTime * moveSpeed);
16
    }
17
}

Hexagon

  • Hierarchy
    • Create
      • Effects
        • Line: Hexagon
  • Inspector
    • Position
      • Size: 6
Index X Y Z
0 -0.5 0.8 0
1 -0.95 0 0
2 -0.5 -0.8 0
3 0.5 -0.8 0
4 0.95 0 0
5 0.5 0.8 0

Collider

  • Hierarchy
    • Hexagon
      • Create
        • Empty: Collider
  • Edge Collider 2D
    • Position
      • Size: 6
Index X Y
0 -0.5 0.8
1 -0.95 0
2 -0.5 -0.8
3 0.5 -0.8
4 0.95 0
5 0.5 0.8

Hexagon.cs

1
using UnityEngine;
2
3
public class Hexagon : MonoBehaviour
4
{
5
    public Rigidbody2D rb;
6
    public float shrinkSpeed = 3f;
7
8
    void Start()
9
    {
10
        rb.rotation = Random.Range(0f, 360f);
11
        transform.localScale = Vector3.one * 10f;
12
    }
13
14
    void Update()
15
    {
16
        transform.localScale -= Vector3.one * shrinkSpeed * Time.deltaTime;
17
18
        if (transform.localScale.x < 0.05f)
19
        {
20
            Destroy(gameObject);
21
        }
22
    }
23
}

Frefabs / 预置体

Hexagon 作成 Frefabs

Spawner / 生成器

  • Hierarchy
    • Create
      • Empty: Spawner
1
using UnityEngine;
2
3
public class Spawner : MonoBehaviour
4
{
5
    public float spawnRate = 1f;
6
    public GameObject hexoagonPrefab;
7
    private float nextTimeToSpawn;
8
9
    void Update()
10
    {
11
        if (Time.time >= nextTimeToSpawn)
12
        {
13
            Instantiate(hexoagonPrefab, Vector3.zero, Quaternion.identity);
14
            nextTimeToSpawn = Time.time + 1f / spawnRate;
15
        }
16
    }
17
}

Restart / 重玩

1
diff --git a/Assets/Scripts/Player.cs b/Assets/Scripts/Player.cs
2
index 5887c5c..723bbc4 100644
3
--- a/Assets/Scripts/Player.cs
4
+++ b/Assets/Scripts/Player.cs
5
@@ -1,4 +1,5 @@
6
 <U+FEFF>using UnityEngine;
7
+using UnityEngine.SceneManagement;
8
 
9
 public class Player : MonoBehaviour
10
 {
11
@@ -14,4 +15,9 @@ public class Player : MonoBehaviour
12
     {
13
         transform.RotateAround(Vector3.zero, Vector3.back, movement * Time.fixedDeltaTime * moveSpeed);
14
     }
15
+
16
+    private void OnTriggerEnter2D(Collider2D collision)
17
+    {
18
+        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
19
+    }
20
 }

Camera rotate

1
 using System.Collections;
2
using UnityEngine;
3
4
public class Rotator : MonoBehaviour
5
{
6
    void Update()
7
    {
8
        transform.Rotate(Vector3.forward, Time.deltaTime * 30f);
9
    }
10
}

小结

  • Dynamic
  • Static 不移动
  • kinematic 运动的, 不受重力影响

下载

游戏

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

源码

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

本文标题:Hexagon

文章作者:iOSDevLog

发布时间:2019年12月10日 - 17:40:33

最后更新:2019年12月19日 - 14:28:04

原始链接:https://game.iosdevlog.com/2019/12/10/Hexagon/

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

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