top of page
Akaranan Weerayanphan


KeepFigh Hero
Genre : 2D , Action
Platform : PC (Play in Browser)
Mode : Single Player
"Keep Fight Hero" เป็นเกมแนว 2D Action ที่ผู้เล่นจะรับบทเป็นฮีโร่ที่ต้องทำหน้าที่เป็นผู้ปกป้องมนุษยชาติจากมอนสเตอร์ที่บุกเข้ามาจากทางซ้ายและขวาของหน้าจอ เกมจะทดสอบความไวของผู้เล่นในการตัดสินใจและการโจมตีให้ทันเวลา
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Lean.Pool;
public class PlayerControll : MonoBehaviour
{
private float horizontal;
private bool isFacingLeft = true;
[SerializeField] private Transform attackPoint;
[SerializeField] private float attackRange = 0.5f;
[SerializeField] private LayerMask enemyLayer;
[SerializeField] private GameOver gameOver;
[Header("Score")]
private Score _score;
[SerializeField] GameObject scoremanager;
[Header("HP")]
[SerializeField] GameObject HPmanager;
private Health _hp;
private bool _gameOver;
[SerializeField] private Animator animator;
[Header("Sound")]
[SerializeField] private SoundManager soundmanager;
[SerializeField] private Sound attack;
[Header("ScoreAndDamage")]
[SerializeField] private ScoreAndHP _scoreAndHp;
private void Start()
{
_gameOver = false;
_hp = HPmanager.GetComponent<Health>();
_score = scoremanager.GetComponent<Score>();
}
void Update()
{
_gameOver = gameOver.gameOver;
if (!_gameOver)
{
horizontal = Input.GetAxisRaw("Horizontal");
Flip();
if (Input.GetKeyDown(KeyCode.Space))
{
animator.SetTrigger("isAttack");
Attack();
soundmanager.Play(attack);
}
}
}
private void Flip()
{
if(isFacingLeft&&horizontal <0f || !isFacingLeft&&horizontal>0f)
{
isFacingLeft = !isFacingLeft;
Vector3 localScale = transform.localScale;
localScale.x *= -1f;
transform.localScale = localScale;
}
}
private void Attack()
{
Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayer);
foreach(Collider2D hit in hitEnemies)
{
_score.AddScore(_scoreAndHp._score);
LeanPool.Despawn(hit.gameObject);
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
LeanPool.Despawn(collision.gameObject);
_hp.HPUpdate(_scoreAndHp._damage);
}
}
PlayerControll
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Lean.Pool;
public class Health : MonoBehaviour
{
[SerializeField] private Text _healthText;
private int _hp;
[SerializeField] private Animator animator;
[SerializeField] private PlayerControll Player;
[SerializeField] private Spawner spawner;
[SerializeField] private GameObject Defeat;
[SerializeField] private GameOver _gameOver;
[Header("Sound")]
[SerializeField] private SoundManager soundmanager;
[SerializeField] private Sound hit,defeat;
// Start is called before the first frame update
void Start()
{
_hp = 3;
UpdateHPText();
}
public void HPUpdate(int damage)
{
_hp -= damage;
if (_hp != 0) animator.SetTrigger("Hit");
UpdateHPText();
soundmanager.Play(hit);
if (_hp == 0)
{
Dead();
soundmanager.Play(defeat);
}
}
private void UpdateHPText()
{
_healthText.text = "Hp : " + _hp;
}
void Dead()
{
animator.SetTrigger("Dead");
_gameOver.ShowUI();
Defeat.SetActive(true);
}
}
Health
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Lean.Pool;
public class Spawner : MonoBehaviour
{
[SerializeField] private GameOver gameOver;
[SerializeField] private Transform[] spawnPoint;
[SerializeField] private GameObject enemy;
private float enemySpawnDelay = 1f;
private int randomSpawnPoint;
private bool _gameOver;
void Start()
{
_gameOver = false;
StartCoroutine(Spawn(enemy, enemySpawnDelay));
}
IEnumerator Spawn(GameObject go, float delay)
{
while (!_gameOver)
{
randomSpawnPoint = Random.Range(0, spawnPoint.Length);
Quaternion spawnRotation = Quaternion.identity;
LeanPool.Spawn(go, spawnPoint[randomSpawnPoint].position, spawnRotation);
// wait for the delay
yield return new WaitForSeconds(delay);
}
}
private void Update()
{
_gameOver = gameOver.gameOver;
}
}
Spawner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Score : MonoBehaviour
{
[SerializeField] private Text _scoreText;
private int _score;
[SerializeField] private Spawner spawner;
[SerializeField] private GameObject victory;
[SerializeField] private GameOver _gameOver;
[Header("Sound")]
[SerializeField] private SoundManager soundmanager;
[SerializeField] private Sound win;
void Start()
{
_score = 0;
_scoreText.text = "Score : " + _score;
}
void Update()
{
if(_score==50)
{
Victory();
soundmanager.Play(win);
}
}
public void AddScore(int increment)
{
_score += increment;
UpdateScoreText();
}
void UpdateScoreText()
{
_scoreText.text = $"Score: {_score}";
}
void Victory()
{
victory.SetActive(true);
_gameOver.ShowUI();
}
}
Score
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Lean.Pool;
public class Enemy : MonoBehaviour
{
private float moveSpeed;
void Start()
{
moveSpeed = Random.Range(1f, 3f);
}
void Update()
{
Move();
Flip();
}
void Move()
{
if(transform.position.x < 0)transform.position += transform.right * moveSpeed*Time.deltaTime;
else if (transform.position.x > 0) transform.position -= transform.right * moveSpeed * Time.deltaTime;
}
void Flip()
{
Vector3 localScale = transform.localScale;
if (transform.position.x > 0)
{
Mathf.Abs(localScale.x);
transform.localScale = localScale;
}
else if (transform.position.x < 0 && localScale.x>0)
{
Mathf.Abs(localScale.x);
localScale.x = -1;
transform.localScale = localScale;
}
}
}
Enemy
bottom of page
