Skip to content

Instantly share code, notes, and snippets.

@IshidaGames
Created November 23, 2019 12:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IshidaGames/e169956550a033e79642bca27e6f1a5e to your computer and use it in GitHub Desktop.
Save IshidaGames/e169956550a033e79642bca27e6f1a5e to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
[RequireComponent(typeof(CapsuleCollider))]
[RequireComponent(typeof(Rigidbody))]
[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;
//SEオブジェクトを入れる
GameObject seObj;
//SEスクリプトを入れる
SE seScr;
void Start()
{
animator = GetComponent<Animator>();
rb = GetComponent<Rigidbody>();
rb.constraints = RigidbodyConstraints.FreezeRotation;
caps = GetComponent<CapsuleCollider>();
caps.center = new Vector3(0, 0.8f, 0);
caps.radius = 0.23f;
caps.height = 1.6f;
agent = GetComponent<NavMeshAgent>();
agent.autoBraking = false;
startPoint = transform.position;
player = GameObject.FindGameObjectWithTag("Player");
attackJudgment.SetActive(false);
//SEオブジェクトを取得
seObj = GameObject.Find("SE");
//SEスクリプトを取得
seScr = seObj.GetComponent<SE>();
}
void Update()
{
if (action == Action.Wait)
{
agent.isStopped = true;
animator.SetFloat("Y", agent.velocity.sqrMagnitude);
}
else if (action == Action.Chase)
{
agent.isStopped = false;
agent.destination = player.transform.position;
agent.speed = chaseSpeed;
animator.SetFloat("Y", agent.velocity.sqrMagnitude);
time += Time.deltaTime;
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;
if (!agent.pathPending && agent.remainingDistance < 0.1f)
{
action = Action.Wait;
}
}
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "PlayerAttack")
{
if (action == Action.Damage)
{
return;
}
else
{
action = Action.Damage;
//SEスクリプトのDamageSound()を呼ぶ
seScr.DamageSound();
}
}
}
private void OnCollisionEnter(UnityEngine.Collision collision)
{
if (collision.gameObject.tag == "Player" && action == Action.Chase)
{
action = Action.Attack;
//SEスクリプトのAttackSound()を呼ぶ
seScr.AttackSound();
}
}
void DamageEnd()
{
animator.SetBool("Damage", false);
action = Action.Chase;
}
void AttackEnd()
{
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