【Unity】攻撃中に移動しないようにする

投稿者: | 2020-07-02

攻撃モーション中に敵が滑るように動くと不自然なので、攻撃中はナビメッシュエージェントが移動しないようにしてみます。

前の記事の敵のスクリプトを少し修正します。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.AI;

public class AgentAttackTest : MonoBehaviour
{
    [SerializeField] Transform player;
    NavMeshAgent agent;
    Animator animator;
    bool isAttacking = false;
    bool judged = false;

    [SerializeField] AudioClip[] audios;
    AudioSource audioSource;
    
    [SerializeField] Text text;

    UnityStandardAssets.Characters.FirstPerson1.FirstPersonController1 sc_fps;

    // Start is called before the first frame update
    void Start()
    {
        agent = GetComponent<NavMeshAgent>();       
        animator = GetComponent<Animator>();
        audioSource = GetComponent<AudioSource>();
        sc_fps = player.GetComponent<UnityStandardAssets.Characters.FirstPerson1.FirstPersonController1>();

    }

    // Update is called once per frame
    void Update()
    {
        // text.text = isAttacking + "";

        agent.destination = player.position;

        // 攻撃しているとき
        if (animator.GetCurrentAnimatorStateInfo(0).IsName("Armature|Attack"))
        {
            // 攻撃アニメーション60%
            if (!judged && animator.GetCurrentAnimatorStateInfo(0).normalizedTime >= 0.4f)
            {
      
                judged = true;
                // 攻撃があたった時
                if (Vector3.Distance(player.position, transform.position) <= 3f && Vector3.Dot(player.position - transform.position, transform.forward) >= 0.7f)
                {
                    sc_fps.SetHP(-0.1f);
                    audioSource.clip = audios[1];
                    audioSource.Play();

                }
                // 攻撃があたらなかった時
                else
                {
                    audioSource.clip = audios[0];
                    audioSource.Play();
                }
            }

            // 攻撃アニメーションのおわり
            if (isAttacking && animator.GetCurrentAnimatorStateInfo(0).normalizedTime >= 0.95f)
            {
                isAttacking = false;
                agent.isStopped = false; // 動きを再開

                StartCoroutine("Groan");
            }
        }

        // 歩いているとき
        if (animator.GetCurrentAnimatorStateInfo(0).IsName("Armature|Walk"))
        {

            // プレイヤーが目の前にいると
            if (!isAttacking && Vector3.Distance(player.position, transform.position) <= 2f && Vector3.Dot(player.position - transform.position, transform.forward) >= 0.9f) //!agent.pathPending && agent.remainingDistance < 2f && 
            {

                agent.isStopped = true; // 動きを止める

                isAttacking = true;
                judged = false;

                animator.SetTrigger("Attack");

                StopCoroutine("Groan");

            }

        }

    }

    IEnumerator Groan()
    {
        yield return new WaitForSeconds(Random.Range(1f,5f));

        audioSource.clip = audios[2];
        audioSource.Play();

    }
}

攻撃を始めるときに、NavMeshAgent.isStoppedにtrueを代入して移動を停止します。攻撃モーションの終わりにはfalseに戻します。

これで敵が攻撃中に移動しなくなりました。

コメントを残す

メールアドレスが公開されることはありません。