top of page
Akaranan Weerayanphan


Sleepless Frontline
Genre : 3D , Top-Down , Shooting
Platform : PC
Mode : Single Player
"Sleepless Frontline" เป็นเกมแนว 3D, Top-Down, Shooting ที่ผู้เล่นจะได้รับบทเป็นทหารที่ปกป้องยานอวกาศจากการโจมตีของเอเลียนแมลง โดยต้องพยายามรอดชีวิตและปกป้องยานจนกว่าความช่วยเหลือจะมาถึง
Programing(AI)
ได้รับหน้าที่สร้างAIศัตรูภายในเกม
โดยที่นำมาใช้จะเป็น State Machine , Path Finding

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class BossSpider : Spider
{
private int attackAnim = 0;
public override void Attack()
{
if( hasAttacked == false )
{
randomAttack();
hasAttacked = true;
}
if( hasAttacked == true )
{
currentCooldown -= Time.deltaTime;
}
if( currentCooldown < 0 )
{
hasAttacked = false;
currentState = SpiderState.Chase;
}
}
private void randomAttack()
{
animator.SetBool("walk" , false);
if( attackAnim%2 == 0) {
animator.SetTrigger("attack1");
currentCooldown = 3;
}
else {
animator.SetTrigger("attack2");
currentCooldown = 5;
}
attackAnim++;
}
}
BossSpider
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.AI;
public class Spider : MonoBehaviour
{
protected float patrolSpeed = 5f;
private float chaseSpeed = 5f;
[SerializeField] private float chaseDistance = 10f;
[SerializeField] private float attackDistance = 3f;
[SerializeField] private Transform[] waypoints;
[SerializeField] private float waypointWaitTime = 1f;
private NavMeshAgent navMeshAgent;
private Transform playerTransform;
private float distanceToPlayer;
private int currentWaypointIndex = 0;
private bool waitingAtWaypoint = false;
private float waitTimeRemaining = 0f;
[SerializeField] protected float attackCooldown = 2f;
protected float currentCooldown = 0f;
protected bool hasAttacked = false;
protected Animator animator;
protected enum SpiderState { Patrol, Chase, Attack ,Die};
protected SpiderState currentState;
[SerializeField] private int SpiderHp;
private EnemyDetail enemyDetail;
[SerializeField] public int SpiderDamage;
private void Start()
{
animator = GetComponent<Animator>();
navMeshAgent = GetComponent<NavMeshAgent>();
enemyDetail = GetComponent<EnemyDetail>();
currentState = SpiderState.Patrol;
waypoints = wpManager.instance.setwaypoint(Random.Range(0,6));
SetNextWaypoint();
}
private void Update()
{
if(playerTransform == null)
{
playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
}
distanceToPlayer = Vector3.Distance(transform.position , playerTransform.position);
switch( currentState )
{
case SpiderState.Patrol:
navMeshAgent.speed = patrolSpeed;
break;
case SpiderState.Chase:
navMeshAgent.speed = chaseSpeed;
break;
case SpiderState.Attack:
navMeshAgent.speed = 0;
break;
case SpiderState.Die: navMeshAgent.speed = 0; break;
}
switch( currentState )
{
case SpiderState.Patrol:
if( distanceToPlayer <= chaseDistance )
{
currentState = SpiderState.Chase;
}
if( waitingAtWaypoint == false && navMeshAgent.remainingDistance < 0.1f )
{
waitingAtWaypoint = true;
waitTimeRemaining = waypointWaitTime;
}
if( waitingAtWaypoint == true)
{
animator.SetBool("idle" , true);
animator.SetBool("walk" , false);
waitTimeRemaining -= Time.deltaTime;
if( waitTimeRemaining <= 0f )
{
waitingAtWaypoint = false;
SetNextWaypoint();
}
}
else
{
animator.SetBool("idle" , false);
animator.SetBool("walk" , true);
navMeshAgent.SetDestination(waypoints[currentWaypointIndex].position);
}
break;
case SpiderState.Chase:
if( distanceToPlayer <= attackDistance )
{
currentState = SpiderState.Attack;
}
else if( distanceToPlayer > chaseDistance )
{
currentState = SpiderState.Patrol;
}
else
{
animator.SetBool("idle" , false);
animator.SetBool("walk",true);
navMeshAgent.SetDestination(playerTransform.position);
}
break;
case SpiderState.Attack:
Attack();
break;
case SpiderState.Die:
animator.SetBool("die",true );
Destroy(gameObject , 2);
break;
}
if( enemyDetail.BossCurHP <= 0)
{
currentState = SpiderState.Die;
}
}
private void SetNextWaypoint()
{
currentWaypointIndex = (currentWaypointIndex + 1) % waypoints.Length;
navMeshAgent.SetDestination(waypoints[currentWaypointIndex].position);
}
public virtual void Attack()
{
if(hasAttacked == false )
{
hasAttacked = true;
animator.SetBool("walk" , false);
animator.SetTrigger("attack");
currentCooldown = attackCooldown;
}
if(hasAttacked == true)
{
currentCooldown -= Time.deltaTime;
}
if(currentCooldown < 0) { hasAttacked = false; currentState = SpiderState.Chase; }
}
[SerializeField] GameObject damageBox;
public void DamageBox()
{
damageBox.SetActive(!damageBox.activeSelf);
}
}
Spider
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class spiderSpawn : MonoBehaviour
{
[SerializeField] private Transform[] spawnpoint;
[SerializeField] private GameObject BossPrefab;
[SerializeField] private GameObject[] SpawnPrefab;
[SerializeField] private TextMeshProUGUI _tmPro;
[SerializeField] private float spawnwaveDelay = 5;
private float curspawnwaveDelay;
private int wave,
bosswave = 6;
void Start()
{
curspawnwaveDelay = 0;
wave = 0;
_tmPro.text = "Wave : " + wave.ToString();
}
void Update()
{
if( curspawnwaveDelay <= 0)
{
for(int i = 0; i < spawnpoint.Length; i++)
{
if( wave % bosswave != 0 ) Instantiate(SpawnPrefab[wave % 2] , spawnpoint[i]);
else Instantiate(BossPrefab , spawnpoint[i]);
}
wave++;
curspawnwaveDelay = spawnwaveDelay;
}
curspawnwaveDelay -= Time.deltaTime;
_tmPro.text = "Wave : " + wave.ToString();
}
}
Spider Spawner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class wpManager : MonoBehaviour
{
public static wpManager instance { get; private set; }
public Transform[] waypointSet1;
public Transform[] waypointSet2;
public Transform[] waypointSet3;
public Transform[] waypointSet4;
public Transform[] waypointSet5;
public Transform[] waypointSet6;
private void Awake()
{
if( instance != null )
{
Destroy(gameObject);
}
instance = this;
}
public Transform[] setwaypoint(int number)
{
switch(number)
{
case 0: return waypointSet1;
case 1: return waypointSet2;
case 2: return waypointSet3;
case 3: return waypointSet4;
case 4: return waypointSet5;
case 5: return waypointSet6;
}
return waypointSet1;
}
}
Waypoint
Manager
Unity Developing
ได้รับหน้าที่สร้างAIศัตรูภายในเกม
ค้นหา ปรับแต่งโมเดล แอนิเมชั่น และ สร้างส่วนต่างๆ
bottom of page
