Created
November 20, 2019 09:57
-
-
Save IshidaGames/cb59c3babebd324ba089c26c2e3d6c5a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
//NavMeshAgent使うとき必要 | |
using UnityEngine.AI; | |
[RequireComponent(typeof(CapsuleCollider))] | |
[RequireComponent(typeof(Rigidbody))] | |
//オブジェクトにNavMeshAgentコンポーネントを設置 | |
[RequireComponent(typeof(NavMeshAgent))] | |
public class Opponent : MonoBehaviour | |
{ | |
private NavMeshAgent agent; | |
private Vector3 startPoint; | |
Rigidbody rb; | |
CapsuleCollider caps; | |
private Animator animator; | |
GameObject player; | |
//追跡モードになってからの時間 | |
[SerializeField] float time; | |
//追跡モードを続ける時間 | |
[SerializeField] float chaseTime = 5.0f; | |
//攻撃にかける時間 | |
[SerializeField] float attackTime = 0.5f; | |
//追跡のスピード | |
[SerializeField] float chaseSpeed = 3.5f; | |
//スタート地点に戻るスピード | |
[SerializeField] float returnSpeed = 1.0f; | |
enum Action | |
{ | |
Wait, | |
Attack, | |
Chase, | |
Damage, | |
Return | |
} | |
[SerializeField] Action action = Action.Wait; | |
//攻撃判定のオブジェクトを入れる | |
public GameObject attackJudgment; | |
void Start() | |
{ | |
//Animatorコンポーネントを取得 | |
animator = GetComponent<Animator>(); | |
//Rigidbodyコンポーネントを取得 | |
rb = GetComponent<Rigidbody>(); | |
//RigidbodyのConstraintsを3つともチェック入れて | |
//勝手に回転しないようにする | |
rb.constraints = RigidbodyConstraints.FreezeRotation; | |
//CapsuleColliderコンポーネントを取得 | |
caps = GetComponent<CapsuleCollider>(); | |
//CapsuleColliderの中心の位置を決める | |
caps.center = new Vector3(0, 0.8f, 0); | |
//CapsuleColliderの半径を決める | |
caps.radius = 0.23f; | |
//CapsuleColliderの高さを決める | |
caps.height = 1.6f; | |
//NavMeshAgentコンポーネントを取得 | |
agent = GetComponent<NavMeshAgent>(); | |
// autoBraking を無効にすると、目標地点の間を継続的に移動します | |
//(つまり、エージェントは目標地点に近づいても | |
// 速度をおとしません) | |
agent.autoBraking = false; | |
//スタート地点を取得 | |
startPoint = transform.position; | |
//Playerタグのオブジェクトを取得 | |
player = GameObject.FindGameObjectWithTag("Player"); | |
//攻撃判定のオブジェクトを隠す | |
attackJudgment.SetActive(false); | |
} | |
void Update() | |
{ | |
//殴られていない時は動かす待機モーション出す | |
if (action == Action.Wait) | |
{ | |
//Agentを止める | |
agent.isStopped = true; | |
//スピードによりモーションが変わる | |
animator.SetFloat("Y", agent.velocity.sqrMagnitude); | |
} | |
//殴られるとPlayerを追跡し出す | |
else if (action == Action.Chase) | |
{ | |
//NavMeshAgentが動き出す | |
agent.isStopped = false; | |
//目的地をPlayerにする | |
agent.destination = player.transform.position; | |
agent.speed = chaseSpeed; | |
animator.SetFloat("Y", agent.velocity.sqrMagnitude); | |
time += Time.deltaTime; | |
//chaseTimeの時間が過ぎるとReturnモードになる | |
if (time > chaseTime) | |
{ | |
action = Action.Return; | |
time = 0; | |
} | |
} | |
//攻撃モード | |
else if (action == Action.Attack) | |
{ | |
animator.SetBool("Attack", true); | |
agent.speed = 0; | |
attackJudgment.SetActive(true); | |
} | |
//ダメージモード | |
else if (action == Action.Damage) | |
{ | |
animator.SetBool("Damage", true); | |
agent.speed = 0; | |
} | |
//スタート地点に帰る | |
else if (action == Action.Return) | |
{ | |
animator.SetFloat("Y", agent.velocity.sqrMagnitude); | |
agent.speed = returnSpeed; | |
//目的地をスタート地点に | |
agent.destination = startPoint; | |
//経路探索の準備ができておらず、 | |
//目標地点との距離から0.1m未満になったら起動 | |
if (!agent.pathPending && agent.remainingDistance < 0.1f) | |
{ | |
//待機モードに戻る | |
action = Action.Wait; | |
} | |
} | |
} | |
//攻撃受けた時の処理 | |
//IsTriggerにチェックしたオブジェクトが | |
//別オブジェクトに触れると起動 | |
private void OnTriggerEnter(Collider other) | |
{ | |
//PlayerAttackタグに触れると、ダメージモードなら何も起きない | |
//それ以外のモードならダメージモードになる | |
if (other.gameObject.tag == "PlayerAttack") | |
{ | |
if (action == Action.Damage) | |
{ | |
return; | |
} | |
else | |
{ | |
action = Action.Damage; | |
} | |
} | |
} | |
//攻撃する条件 | |
//本体のColliderが別オブジェクトに触れたら起動 | |
private void OnCollisionEnter(UnityEngine.Collision collision) | |
{ | |
//相手がPlayerタグでこちらが追跡モードの時に起動 | |
//攻撃モードになる | |
if (collision.gameObject.tag == "Player" && action == Action.Chase) | |
{ | |
action = Action.Attack; | |
} | |
} | |
//DAMAGED00のアニメーションイベントで呼ばれる | |
//Action.Moveに戻す | |
//ダメージアニメーションから移動アニメーションへ | |
void DamageEnd() | |
{ | |
animator.SetBool("Damage", false); | |
action = Action.Chase; | |
} | |
//POSE30のアニメーションイベントで呼ばれる | |
//Invokeで指定した秒数の後にAttackEndAnimが呼ばれる | |
void AttackEnd() | |
{ | |
//attackTime秒後にAttackEndAnimを呼ぶ | |
Invoke("AttackEndAnim", attackTime); | |
} | |
//追跡モードに戻す | |
//パンチアニメーションから移動アニメーションへ | |
//攻撃判定のオブジェクトをしまう | |
void AttackEndAnim() | |
{ | |
animator.SetBool("Attack", false); | |
attackJudgment.SetActive(false); | |
action = Action.Chase; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment