.
This commit is contained in:
65
Assets/Scripts/BluePlayerController.cs
Normal file
65
Assets/Scripts/BluePlayerController.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
public class BluePlayerController : MonoBehaviour
|
||||
{
|
||||
public GameObject shield;
|
||||
|
||||
public GameObject bulletPrefab;
|
||||
public Transform firePoint;
|
||||
|
||||
public float shootCooldown = 0.5f;
|
||||
|
||||
private float cooldownTimer;
|
||||
|
||||
private bool isDead = false;
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (Keyboard.current == null)
|
||||
return;
|
||||
|
||||
if (isDead)
|
||||
return;
|
||||
|
||||
cooldownTimer -= Time.deltaTime;
|
||||
|
||||
HandleShield();
|
||||
HandleShoot();
|
||||
}
|
||||
|
||||
void HandleShield()
|
||||
{
|
||||
// Giữ F => bỏ khiên
|
||||
bool holdingF =
|
||||
Keyboard.current.fKey.isPressed;
|
||||
|
||||
shield.SetActive(!holdingF);
|
||||
}
|
||||
|
||||
void HandleShoot()
|
||||
{
|
||||
|
||||
|
||||
// Chỉ bắn khi đang bỏ khiên
|
||||
if (!shield.activeSelf &&
|
||||
Keyboard.current.gKey.wasPressedThisFrame &&
|
||||
cooldownTimer <= 0f)
|
||||
{
|
||||
Instantiate(
|
||||
bulletPrefab,
|
||||
firePoint.position,
|
||||
firePoint.rotation
|
||||
);
|
||||
|
||||
cooldownTimer = shootCooldown;
|
||||
}
|
||||
}
|
||||
|
||||
public void Die()
|
||||
{
|
||||
isDead = true;
|
||||
|
||||
GameManager.Instance.ShowWinner("RED WIN!");
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/BluePlayerController.cs.meta
Normal file
2
Assets/Scripts/BluePlayerController.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 16ca7876af4ace2468e2289a8513ae82
|
||||
45
Assets/Scripts/Bullet.cs
Normal file
45
Assets/Scripts/Bullet.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class Bullet : MonoBehaviour
|
||||
{
|
||||
public float speed = 10f;
|
||||
|
||||
void Update()
|
||||
{
|
||||
transform.position += transform.forward * speed * Time.deltaTime;
|
||||
}
|
||||
|
||||
private void OnTriggerEnter(Collider other)
|
||||
{
|
||||
// Trúng khiên
|
||||
if (other.CompareTag("Shield"))
|
||||
{
|
||||
Destroy(gameObject);
|
||||
return;
|
||||
}
|
||||
|
||||
// Trúng đầu
|
||||
if (other.CompareTag("BluePlayer"))
|
||||
{
|
||||
HitBox hit = other.GetComponent<HitBox>();
|
||||
|
||||
if (hit != null)
|
||||
{
|
||||
hit.blueplayer.Die();
|
||||
}
|
||||
|
||||
Destroy(gameObject);
|
||||
}
|
||||
if (other.CompareTag("RedPlayer"))
|
||||
{
|
||||
HitBox hit = other.GetComponent<HitBox>();
|
||||
|
||||
if (hit != null)
|
||||
{
|
||||
hit.redplayer.Die();
|
||||
}
|
||||
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Bullet.cs.meta
Normal file
2
Assets/Scripts/Bullet.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b3d0c723e563624469ed05366a4184d3
|
||||
35
Assets/Scripts/GameManager.cs
Normal file
35
Assets/Scripts/GameManager.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
public class GameManager : MonoBehaviour
|
||||
{
|
||||
public static GameManager Instance;
|
||||
|
||||
public GameObject resultPanel;
|
||||
public TextMeshProUGUI resultText;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
Instance = this;
|
||||
}
|
||||
|
||||
public void ShowWinner(string loserName)
|
||||
{
|
||||
resultPanel.SetActive(true);
|
||||
|
||||
if (loserName.Contains("Blue"))
|
||||
resultText.text = "RED WIN!";
|
||||
else
|
||||
resultText.text = "BLUE WIN!";
|
||||
|
||||
Time.timeScale = 0f;
|
||||
}
|
||||
|
||||
public void Again()
|
||||
{
|
||||
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
|
||||
Time.timeScale = 1f;
|
||||
}
|
||||
|
||||
}
|
||||
2
Assets/Scripts/GameManager.cs.meta
Normal file
2
Assets/Scripts/GameManager.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 61e65f88b41b87a4cb5404645938e821
|
||||
7
Assets/Scripts/HitBox.cs
Normal file
7
Assets/Scripts/HitBox.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class HitBox : MonoBehaviour
|
||||
{
|
||||
public BluePlayerController blueplayer;
|
||||
public RedPlayerController redplayer;
|
||||
}
|
||||
2
Assets/Scripts/HitBox.cs.meta
Normal file
2
Assets/Scripts/HitBox.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d3c9212d3857e5b4893a36729d359906
|
||||
65
Assets/Scripts/RedPlayerController.cs
Normal file
65
Assets/Scripts/RedPlayerController.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
public class RedPlayerController : MonoBehaviour
|
||||
{
|
||||
public GameObject shield;
|
||||
|
||||
public GameObject bulletPrefab;
|
||||
public Transform firePoint;
|
||||
|
||||
public float shootCooldown = 0.5f;
|
||||
|
||||
private float cooldownTimer;
|
||||
|
||||
private bool isDead = false;
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (Keyboard.current == null)
|
||||
return;
|
||||
|
||||
if (isDead)
|
||||
return;
|
||||
|
||||
cooldownTimer -= Time.deltaTime;
|
||||
|
||||
HandleShield();
|
||||
HandleShoot();
|
||||
}
|
||||
|
||||
void HandleShield()
|
||||
{
|
||||
// Giữ F => bỏ khiên
|
||||
bool holdingF =
|
||||
Keyboard.current.kKey.isPressed;
|
||||
|
||||
shield.SetActive(!holdingF);
|
||||
}
|
||||
|
||||
void HandleShoot()
|
||||
{
|
||||
|
||||
|
||||
// Chỉ bắn khi đang bỏ khiên
|
||||
if (!shield.activeSelf &&
|
||||
Keyboard.current.jKey.wasPressedThisFrame &&
|
||||
cooldownTimer <= 0f)
|
||||
{
|
||||
Instantiate(
|
||||
bulletPrefab,
|
||||
firePoint.position,
|
||||
firePoint.rotation
|
||||
);
|
||||
|
||||
cooldownTimer = shootCooldown;
|
||||
}
|
||||
}
|
||||
|
||||
public void Die()
|
||||
{
|
||||
isDead = true;
|
||||
|
||||
GameManager.Instance.ShowWinner("BLUE WIN!");
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/RedPlayerController.cs.meta
Normal file
2
Assets/Scripts/RedPlayerController.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e10bf968efdc6624a85b1b79a5806946
|
||||
Reference in New Issue
Block a user