繼續敵人AI的製作,接下做攻擊的部分
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyControl : MonoBehaviour
{
public enum EnemyBehavior {patrol,battle};
public EnemyBehavior EnemyNow;
public enum Battle { follow, attack, dodge };
public Battle BatChoose;
public enum checkface { Lelf,Right};
public checkface face;
//宣告一個射擊武器物件
public GameObject SpearAttack;
public static bool SpearSpr;
...
private void FixedUpdate() ...
}
這樣在Inspector的腳本就會出現可以放射擊武器物件的欄位
private void FixedUpdate()
{
//敵人狀態
if (!LookForP)
{
StartCoroutine(DoCheck());
}
switch (EnemyNow)
{
case EnemyBehavior.patrol:
if (playertransform)
{
//巡邏
Domove();
}
break;
case EnemyBehavior.battle:
switch (BatChoose)
{
case Battle.follow:
followPlayer();
break;
case Battle.attack:
DoAttack();
break;
}
break;
}
}
void DoAttack()
{
if (spr.flipX == true)
{
SpearSpr = false;
}
esle
{
SpearSpr = true
}
Instantiate(SpearAttack, this.transform.position, Quaternion.identity);
}
射擊武器腳本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpearControl : MonoBehaviour
{
public float timer=0;
public int a = 0;
public bool direction;
void Start()
{
direction = EnemyControl.SpearSpr;
}
void Update()
{
if (direction)
{
this.gameObject.transform.position += new Vector3(-1f * Time.deltaTime * 60, 0, 0);
}
else
{
this.gameObject.transform.position += new Vector3(1f * Time.deltaTime * 60, 0, 0);
}
timer -= Time.deltaTime;
if (timer <= 0)
{
Destroy(this.gameObject);
}
}
}
攻擊間隔改好之後再更新
待更
參考資料: