首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使anim.SetBool与NavMeshAgent协同工作的问题

使anim.SetBool与NavMeshAgent协同工作的问题
EN

Stack Overflow用户
提问于 2018-07-06 23:57:25
回答 1查看 761关注 0票数 0

我正在为我的游戏中的一个角色设置一个简单的小人工智能。由于某些原因,在使用NavMeshAgent时,我在获得动画播放时遇到了很大的问题,我不明白为什么。这是我从Unity中汇集而来的一个方法点系统,而且我似乎无法完成这个任务。我希望,如果有人能给我一些投入,如果可以澄清一些其他的事情,以及。我真的是迷失在这里,并将感谢任何意见。在底部的代码全部工作,直到它击中巡逻队,然后玩家移动,没有动画。我觉得有更多的东西我需要了解的肚脐的可能。或者更多我需要了解的编程在一般情况下。

代码语言:javascript
复制
    e// Patrol.cs
using UnityEngine;
using UnityEngine.AI;
using System.Collections;


public class Enemy_Patrol : MonoBehaviour
{
    public Transform[] points;
    public Animator anim; 

    private int destPoint = 0;
    private NavMeshAgent agent;

    void Start()
    {
        agent = GetComponent<NavMeshAgent>();
        anim = GetComponent<Animator>(); 

        // Disabling auto-braking allows for continuous movement
        // between points (ie, the agent doesn't slow down as it
        // approaches a destination point).
        agent.autoBraking = false;

        GotoNextPoint();
    }


    void GotoNextPoint()
    {
        // Returns if no points have been set up
        if (points.Length == 0)
            return;

        // Set the agent to go to the currently selected destination.
        agent.destination = points[destPoint].position;
        anim.SetBool("WalkForwards", true);
        anim.SetBool("IsIdle", false);
        // Choose the next point in the array as the destination,
        // cycling to the start if necessary.
        destPoint = (destPoint + 1) % points.Length;
    }


    void Update()
    {
        // Choose the next destination point when the agent gets
        // close to the current one.
        if (!agent.pathPending && agent.remainingDistance < 0.5f)

        GotoNextPoint();
    }
}


// Code i wrote to handle following chasing etc.

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

public class AI_Tester : MonoBehaviour
{
    private float patrolSpeed = .2f;
    public NavMeshAgent agent;
    public GameObject[] waypoints;
    private int waypointInd = 0;
    public Transform player;
    static Animator anim;
    public SealForce_DestructableObjects destructableObjects;
    public Transform enemy;


    // Use this for initialization
    void Start()
    {
        anim = GetComponentInChildren<Animator>();
        agent = GetComponent<NavMeshAgent>();
        destructableObjects = GetComponent<SealForce_DestructableObjects>();
        waypoints = GameObject.FindGameObjectsWithTag("waypoints");
        waypointInd = Random.Range(0, waypoints.Length); 
    }


    void AIMovements()
    {
        if (Vector3.Distance(player.position, this.transform.position) <= 30)
        {
            Vector3 direction = player.position - this.transform.position;
            direction.y = 0;

            this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
                                        Quaternion.LookRotation(direction), 0.1f);

            if (direction.magnitude > 15)
            {
                MoveToFiringRange();
            }


            if (direction.magnitude <= 15)
            {
                AttackPlayer();
            }
        }
        else if (Vector3.Distance(player.position, this.transform.position) > 30)
        {
            WaitingOnAction();
        }
    }
    public void Update()
    {
        AIMovements();
    }
    public void AttackPlayer()
    {
        anim.SetTrigger("IsAttacking");
        anim.SetBool("IsIdle", false);
        anim.SetBool("RunForwards", false);
    }
    public void MoveToFiringRange()
    {
        this.transform.Translate(0, 0, 0.04f);
        anim.SetBool("RunForwards", true);
        anim.SetBool("IsIdle", false);
        anim.ResetTrigger("IsAttacking");
    }
    public void WaitingOnAction()
    {
        anim.SetBool("IsIdle", true);
        anim.SetBool("RunForwards", false); 

        StartCoroutine("BackToPatrol"); 
    }

//program works fine all the up to here. The only thing wrong with patrol is no animation. 

    IEnumerator BackToPatrol()
    {
        yield return new WaitForSeconds(5);
        anim.SetBool("IsIdle", false);

        Patrol(); 
    }
    public void Patrol()
    {

        Debug.Log("In Patrol"); 
        agent.speed = patrolSpeed;
        if (Vector3.Distance(this.transform.position, waypoints[waypointInd].transform.position) >= 2)
        {
            agent.SetDestination(waypoints[waypointInd].transform.position);
            anim.SetBool("WalkForwards", true); 
        }
        if (Vector3.Distance(this.transform.position, waypoints[waypointInd].transform.position) <= 2)
        {
            waypointInd += 1;
            if (waypointInd > waypoints.Length)
            {
                waypointInd = 0;

            }

        }
    }
}

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

public class AI_Tester : MonoBehaviour
{
    public bool isPatrolling; 
    private float patrolSpeed = 1.5f;
    public NavMeshAgent agent;
    public GameObject[] waypoints;
    private int waypointInd = 0;
    public Transform player;
    static Animator anim;
    //public SealForce_DestructableObjects destructableObjects;
    //public Transform enemy;


    // Use this for initialization
    void Start()
    {
        anim = GetComponentInChildren<Animator>();
        agent = GetComponent<NavMeshAgent>();
        //destructableObjects = GetComponent<SealForce_DestructableObjects>();
        waypoints = GameObject.FindGameObjectsWithTag("waypoints");
        waypointInd = Random.Range(0, waypoints.Length);

    }


    void AIMovements()
    {
        if (Vector3.Distance(player.position, this.transform.position) <= 30)
        { 
            Vector3 direction = player.position - this.transform.position;
            direction.y = 0;

            this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
                                        Quaternion.LookRotation(direction), 0.1f);

            if (direction.magnitude > 15)
            {
                //StopCoroutine("BackToPatrol");
                StopCoroutine("Patrol");
                isPatrolling = false; 
                MoveToFiringRange();
            }


            if (direction.magnitude <= 15)
            {
                //StopCoroutine("BackToPatrol");
                StopCoroutine("Patrol");
                isPatrolling = false;  
                AttackPlayer();
            }
        }
        else if (Vector3.Distance(player.position, this.transform.position) > 30 && !isPatrolling)
        {
            //StopCoroutine("BackToPatrol");
            StopCoroutine("Patrol");
            WaitingOnAction();
        }
    }
    public void Update()
    {  
        AIMovements();  
    }
    public void AttackPlayer()
    {
        anim.SetTrigger("IsAttacking");
        anim.SetBool("IsIdle", false);
        anim.SetBool("RunForwards", false);
    }
    public void MoveToFiringRange()
    { 
        this.transform.Translate(0, 0, 0.04f);
        anim.SetBool("RunForwards", true);
        anim.SetBool("IsIdle", false);
        anim.ResetTrigger("IsAttacking");
    }
    public void WaitingOnAction()
    {
        anim.SetBool("IsIdle", true);
        anim.SetBool("RunForwards", false); 

        StartCoroutine("BackToPatrol"); 

    }
    IEnumerator BackToPatrol()
    {
        isPatrolling = true;
        yield return new WaitForSeconds(5);
        anim.SetBool("IsIdle", false); 

        yield return StartCoroutine ("Patrol");
        isPatrolling = false;
    }
    IEnumerator Patrol()
    {
        Debug.Log("In Patrol");
        agent.speed = patrolSpeed;
        agent.SetDestination(waypoints[waypointInd].transform.position);
        anim.SetBool("WalkForwards", true);

        while (Vector3.Distance(this.transform.position, waypoints[waypointInd].transform.position) >= 2 && !isPatrolling)
        {
            yield return null;
        }

        waypointInd++;

        if (waypointInd >= waypoints.Length) waypointInd = 0;



    }


   /* public void  Patrol()
    {
        Debug.Log("In Patrol");
        agent.speed = patrolSpeed;

        if (Vector3.Distance(this.transform.position, waypoints[waypointInd].transform.position) >= 2)
        {
            agent.SetDestination(waypoints[waypointInd].transform.position);
            anim.SetBool("IsIdle", false); 
            anim.SetBool("WalkForwards", false);
        }
        if (Vector3.Distance(this.transform.position, waypoints[waypointInd].transform.position) <= 2)
        {
            waypointInd += 1;
            if (waypointInd > waypoints.Length)
            {
                waypointInd = 0;*/



}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-07 06:32:28

您正在更新每个帧上调用WaitingOnAction,这将将动画器的IsIdle设置为true,并启动一个新的BackToPatrol协同线。这种情况不应该发生。在再次调用WaitingOnAction之前,尝试检查该字符是否已到达其目的地。类似于:

代码语言:javascript
复制
else if (Vector3.Distance(player.position, this.transform.position) > 30 && !isPatrolling)
{
    WaitingOnAction();
}

在你们的共同阵线里:

代码语言:javascript
复制
IEnumerator BackToPatrol()
{
    isPatrolling = true;
    yield return new WaitForSeconds(5);
    anim.SetBool("IsIdle", false);

    yield return StartCoroutine("Patrol"); 
    isPatrolling = false;
}

IEnumerator Patrol()
{
    Debug.Log("In Patrol"); 
    agent.speed = patrolSpeed;
    agent.SetDestination(waypoints[waypointInd].transform.position);
    anim.SetBool("WalkForwards", true);
    agent.isStopped = false;

    while (Vector3.Distance(this.transform.position, waypoints[waypointInd].transform.position) >= 2 && isPatrolling)
    {
        yield return null;
    }

    agent.isStopped = true;
    anim.SetBool("WalkForwards", false);
    waypointInd++;

    if(waypointInd >= waypoints.Length) waypointInd = 0;
}

我没有测试过,但是像这样的东西应该能用。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51218872

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档