¡¡º÷ŨÈÏ°ÏÆâ¤Î¾ì¹ç¤Ë¤Ï¹¶·â¤òÁªÂò¤¹¤ë¤è¤¦¤Ë¤·¤Æ¤¢¤²¤¿¤¤¤Î¤Ç¤¹¤¬¡¢º÷ŨÈϰϤϷ빽¤ÊÈϰϤ¬¤¢¤ê¤Þ¤¹¡£
¤Ä¤Þ¤ê¡¢ÈϰϤΥ®¥ê¥®¥ê¤Ë¥×¥ì¥¤¥ä¡¼¤¬¤¤¤Æ¤â¤½¤ì¤Ïº÷ŨÈÏ°ÏÆâ¤Ç¤¹¤«¤éȯ¸«¤µ¤ì¤¿¤â¤Î¤È¤Ê¤ê¤Þ¤¹¡£¤½¤Î¤¿¤á
º÷ŨÈÏ°ÏÆâ¤À¤«¤é¤¹¤°¤Ë¹¶·â¡¢¤È¤¤¤¦¤è¤¦¤Ë¥×¥í¥°¥é¥à¤òÁȤó¤Ç¤·¤Þ¤¦¤È¡¢¤â¤Î¤¹¤´¤¯¥×¥ì¥¤¥ä¡¼¤¬±ó¤¤¤Ë¤â´Ø¤ï¤é¤º
¤½¤Î¾ì¤Ç¹¶·â¤ò¹Ô¤Ã¤Æ¤·¤Þ¤¦¤³¤È¤Ë¤Ê¤ê¡¢¤³¤ì¤âÈó¾ï¤Ë³ê·Î¤«¤Ä¡¢¶¼°Ò¤È´¶¤¸¤ë¤³¤È¤Ï¤Ê¤¤¤Ç¤·¤ç¤¦¡£
¡¡¤½¤Î¤¿¤á¡¢¡Öº÷ŨÈÏ°ÏÆâ¤Ë¥×¥ì¥¤¥ä¡¼¤¬¤¤¤ë¾ì¹ç¤Ë¤Ï¡¢¤Þ¤º¥×¥ì¥¤¥ä¡¼¤Î°ÌÃ֤ޤǰÜÆ°¤ò¹Ô¤Ã¤Æ¡¢¤½¤Î¸å¡¢¹¶·â¤ò¹Ô¤¦¡×¤È¤¤¤¦ÆâÍƤǤ¢¤ì¤Ð
¥×¥ì¥¤¥ä¡¼¤ËÂФ·¤Æ¸þ¤«¤Ã¤Æ¤¤Æ¤«¤é¹¶·â¤¹¤ëµóÆ°¤Ë¤Ê¤ê¤Þ¤¹¤«¤é¡¢Å¨¤È¤·¤Æ¿½¤·Ê¬¤Ê¤¤¹ÔÆ°¤È¤¤¤¨¤ë¤Ç¤·¤ç¤¦¡£
¡¡¤³¤ì¤é¤ÎÅÀ¤òƧ¤Þ¤¨¤Æ¡¢EnemyControler¥¯¥é¥¹¤ò½¤Àµ¤·¤Æ¤¤¤¤Þ¤¹¡£
¡¡<= ¡Ü¥Ü¥¿¥ó¤ò²¡¤¹¤È³«¤¤Þ¤¹¤Î¤Ç¡¢¼«Ê¬¤Ê¤ê¤Î¼ÂÁõ¤ò¹Ô¤Ã¤¿¾å¤Ç³Îǧ¤ò¤·¤Æ¤ß¤Þ¤·¤ç¤¦¡£¤Þ¤¿³Îǧ¸å¡¢ÉÔÍפÊDebug.Log¤Ï¥³¥á¥ó¥È¥¢¥¦¥È¤¹¤ë¤«ºï½ü¤·¤Þ¤·¤ç¤¦¡£
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyController : MonoBehaviour {
private string weapon = "Weapon"; // ¥¿¥°»ØÄêÍÑ
[Header("¸½ºß¤ÎHP")]
public int hp;
[Header("¥¯¥ê¥Æ¥£¥«¥ë¤ÎȯÀ¸³ÎΨ")]
public int criticalSuccessRate;
[Header("¥¯¥ê¥Æ¥£¥«¥ë»þ¤Î¹¶·âÎÏÇÜΨ")]
public float criticalMultiplier;
[Header("°Üư®Å٤κǾ®ÃÍ")]
public float minMoveSpeed;
[Header("°Üư®Å٤κÇÂçÃÍ")]
public float maxMoveSpeed;
[Header("¹¶·âÎϤκǾ®ÃÍ")]
public int minAttackPower;
[Header("¹¶·âÎϤκÇÂçÃÍ")]
public int maxAttackPower;
private float moveSpeed;¡¡¡¡// ŬÍѤ¹¤ë°Üư®ÅÙ
public int attackPower;¡¡¡¡// ŬÍѤ¹¤ë¹¶·âÎÏ
public enum ENEMY_STATE {
SET_UP,
WAIT,
MOVE,
ATTACK,
READY
}
public ENEMY_STATE enemyState;
private float actionTime;
private Rigidbody rb;
private Animator anim;
public SearchArea searchArea;
private Vector3 destinationPos; // º÷Ũ»þ¤Î°ÜÆ°¤ÎÌÜŪÃÏ(Player¤Î°ÌÃÖ¾ðÊó)
Vector3 direction; // °ÜÆ°¤¹¤ëºÝ¤ÎÊý¸þ
void Start() {
// °Üư®Å٤ʤɤÎÃͤòÀßÄꤹ¤ë
SetUpEnemyParameter();
}
/// <summary>
¡¡¡¡/// Ũ¤Î¥Ñ¥é¥á¡¼¥¿(°Üư®Å٤乶·âÎϤʤÉ)¤È¾õÂÖ¤òÀßÄê
¡¡¡¡/// </summary>
private void SetUpEnemyParameter() {
enemyState = ENEMY_STATE.SET_UP;
Debug.Log(enemyState);
moveSpeed = Random.Range(minMoveSpeed, maxMoveSpeed);
attackPower = Random.Range(minAttackPower, maxAttackPower);
actionTime = Random.Range(3, 6);
rb = GetComponent<Rigidbody>();
anim = GetComponent<Animator>();
enemyState = ENEMY_STATE.WAIT;
Debug.Log(enemyState);
Debug.Log("MoveSpeed : " + moveSpeed);
Debug.Log("AttakPower : "+ attackPower);
}
void Update() {
// ½àÈ÷¾õÂÖ¡£Å¨¤Î¥Ñ¥é¥á¡¼¥¿¤Î½àÈ÷¤¬½ªÎ»¤·¤Æ¤¤¤Ê¤¤¾ì¹ç¤Ë¤ÏUpdate¤ò½èÍý¤·¤Ê¤¤
if (enemyState == ENEMY_STATE.SET_UP) {
return;
}
actionTime -= Time.deltaTime;
// ÂÔµ¡¾õÂÖ
if (enemyState == ENEMY_STATE.WAIT) {
¡¡¡¡¡¡¡¡¡¡¡¡Debug.Log("ÂÔµ¡Ãæ ¤¢¤È : " + actionTime + " ÉÃ");
if (actionTime <= 0) {
CheckNextAction();
Debug.Log("ÂÔµ¡½ªÎ»");
//NextAction(ENEMY_STATE.MOVE);
}
}
// °ÜÆ°¾õÂÖ
if (enemyState == ENEMY_STATE.MOVE) {
Move();
Debug.Log("°ÜÆ°Ãæ ¤¢¤È : " + actionTime + " ÉÃ");
if (actionTime <= 0) {
NextWait();
}
}
// º÷ŨÈÏ°ÏÆâ¤ÇÌÜŪÃÏ(Player¤Î°ÌÃÖ)¤¬¤¢¤ë¾ì¹ç¡¢ÌÜŪÃϤޤǰÜÆ°¤¹¤ë
if (enemyState == ENEMY_STATE.ATTACK) {
Debug.Log(Vector3.Distance(transform.position, destinationPos));
if (Vector3.Distance(transform.position, destinationPos) > 0.8f) {
Move();
Debug.Log("°ÜÆ°¹¶·âÃæ");
} else {
// ÌÜŪÃϤËÃ夤¤¿¤é¹¶·â
enemyState = ENEMY_STATE.READY;
StartCoroutine(Attack());
Debug.Log("¹¶·â");
return;
}
}
}
/// <summary>
/// ¼¡¤Î¹ÔÆ°¤ò³Îǧ¤¹¤ë
/// </summary>
private void CheckNextAction() {
// º÷ŨÈÏ°ÏÆâ¤ËPlayer¤¬¤¤¤ë¾ì¹ç
if (searchArea.isSearching) {
if (searchArea.player != null) {
// Player¤Î°ÌÃ֤ޤǰÜÆ°¤·¤Æ¹¶·â¤ò¤¹¤ë
MoveAttack(searchArea.player);
Debug.Log("°ÜÆ°¤·¤Æ¹¶·â¤¹¤ë");
}
} else {
// º÷ŨÈÏ°ÏÆâ¤ËPlayer¤¬¤¤¤Ê¤¤¾ì¹ç¡¢°ÜÆ°¤Î½àÈ÷¤ò¤¹¤ë
PraparateMove();
Debug.Log("°ÜÆ°½àÈ÷");
}
}
/// <summary>
/// º÷ŨÈÏ°ÏÆâ¤ÎPlayer¤Î°ÌÃÖ¤òÌÜŪÃϤȤ·¡¢°ÜÆ°¤¹¤ë
/// </summary>
/// <param name="player"></param>
private void MoveAttack(GameObject player) {
// ÌÜŪÃϤòÀßÄꤷ¤Æ¤ª¤¯
destinationPos = player.transform.position;
// °ÜÆ°¤¹¤ëÊý¸þ¤òÀßÄꤹ¤ë
direction = (transform.position - destinationPos).normalized;
// °ÜÆ°¤¹¤ëÊý¸þ¤ò¸þ¤¯
transform.LookAt(destinationPos);
// Update¤ÎAttack¤ÎÉôʬ¤Ç°ÜÆ°¤µ¤»¤ë
enemyState = ENEMY_STATE.ATTACK;
anim.SetBool("Run", true);
}
/// <summary>
/// °ÜÆ°¤Î½àÈ÷
/// </summary>
private void PraparateMove() {
// ¥é¥ó¥À¥à¤ÊÊý¸þ¤ò¸þ¤¯
transform.rotation = Quaternion.AngleAxis(Random.Range(0, 360), new Vector3(0, 1, 0));
direction = transform.position.normalized;
enemyState = ENEMY_STATE.MOVE;
actionTime = Random.Range(3, 5);
anim.SetBool("Run", true);
}
/// <summary>
/// ¼¡²ó¤Î¹ÔÆ°¤Þ¤Ç¤ÎÂÔµ¡
/// </summary>
/// <param name="nextState">ENEMY_STATE</param>
private void NextWait() {
anim.SetBool("Run", false);
// ÂÔµ¡
actionTime = Random.Range(3, 6);
enemyState = ENEMY_STATE.WAIT;
}
/// <summary>
/// °ÜÆ°¤Î½èÍý
/// </summary>
private void Move() {
rb.AddForce(-direction * moveSpeed);
Debug.Log(rb.velocity);
}
/// <summary>
/// ¹¶·â¤Î½èÍý
/// </summary>
/// <returns></returns>
private IEnumerator Attack() {
anim.SetTrigger("Attack");
yield return new WaitForSeconds(2.0f);
NextWait();
}
private void OnTriggerEnter(Collider col) {
// Weapon¥¿¥°°Ê³°¤ÏÅö¤¿¤êȽÄê¤È¤·¤ÆȽÄꤷ¤Ê¤¤
if (col.gameObject.tag != weapon) {
return;
}
// ¹¶·âÎϤؤνé´üÇÜΨ
float attackPowerRate = 1.0f;
// ¥¯¥ê¥Æ¥£¥«¥ë¤ÎȽÄê¤òÌá¤êÃͤǹԤ¦¡£True¤¬Ìá¤Ã¤Æ¤¤¿¤é¥¯¥ê¥Æ¥£¥«¥ë¤·¤¿È½Äê
if (JudgeCritialHit()) {
// ¥¯¥ê¥Æ¥£¥«¥ë¤·¤¿È½Äê¤Î¾ì¹ç¡¢¹¶·âÇÜΨ¤òÊѹ¹
attackPowerRate = criticalMultiplier;
}
// AttackPower¤ò¼èÆÀ¤·¤¿¤¤¤¿¤á¡¢¤Þ¤º¤ÏWeapon¥²¡¼¥à¥ª¥Ö¥¸¥§¥¯¥È¤ÎWeaponController¤«¤é¡¢PlayerController¤ò¼èÆÀ
MoveSample playerController = col.gameObject.GetComponent<WeaponController>().playerController;
// PlayerController¥¯¥é¥¹¤¬ÊÑ¿ô¤ËÂåÆþ¤Ç¤¤Æ¤¤¤ë¤«³Îǧ
Debug.Log(playerController);
// ºÇ½ªÅª¤Ê¹¶·âÎϤò»»½Ð(¾®¿ôÅÀ¤ÏÀÚ¤ê¼Î¤Æ)¡¡±¦ÊÕ¤ÏFloat·¿Æ±»Î¤Ç¤Î·×»»¼°¤Î¤¿¤á·×»»¤Ç¤¤ë¤¬¡¢·×»»·ë²Ì¤¬Float¤Ë¤Ê¤ë¤¿¤á¡¢¤½¤Î¤Þ¤Þ¤À¤Èº¸ÊÕ¤ÎInt·¿¤È·¿¤¬¹ç¤ï¤Ê¤¤¡£
// º¸Êդȹç¤ï¤»¤ë¤¿¤á¤Ë¡¢·×»»¸å¤ËInt·¿¤ËÌÀ¼¨Åª¤Ë·¿¤ÎÊÑ´¹¤ò¤·¤Æ¤¤¤ë(¤³¤ì¤ò¥¥ã¥¹¥È¤È¤¤¤¤¤Þ¤¹)
int damage = (int)Mathf.Floor(playerController.attackPower * attackPowerRate);
Debug.Log(damage);
// HP¤«¤édamageʬ¤À¤±¸º»»
hp -= damage;
Debug.Log("»Ä¤êHP : " + hp);
// »Ä¤êHP¤Î³Îǧ
if (hp <= 0) {
Debug.Log("Ũ¤òÅݤ·¤¿");
Destroy(this.gameObject);
}
}
/// <summary>
/// ¥¯¥ê¥Æ¥£¥«¥ë¤ÎȽÄê
/// </summary>
/// <returns></returns>
private bool JudgeCritialHit() {
// ȽÄêÍѤÎÊÑ¿ô¡£True¤Ê¤é¥¯¥ê¥Æ¥£¥«¥ë¤·¤¿È½Äê¡¢False¤Ê¤é¥¯¥ê¥Æ¥£¥«¥ë¤·¤Æ¤¤¤Ê¤¤È½Äê
bool isCritical = false;
// Íð¿ô¤ò£±¤Ä¼èÆÀ
int randomValue = Random.Range(0, 100);
// Íð¿ô¤È¥¯¥ê¥Æ¥£¥«¥ë¤Î³ÎΨ¤òÈæ¤Ù¡¢¥¯¥ê¥Æ¥£¥«¥ëȯÀ¸³ÎΨ¤è¤ê¤âÍð¿ô¤ÎÊý¤¬Ä㤤¤Ê¤é¥¯¥ê¥Æ¥£¥«¥ë¤·¤¿¤³¤È¤Ë¤¹¤ë
if (randomValue <= criticalSuccessRate) {
Debug.Log("Critical!!");
return true;
}
return false;
}
}
¥³¥á¥ó¥È¤ò¤«¤¯