战斗系统和Unity的NavMesh无法一起正常运行(处于保持状态)

问题描述 投票:0回答:1

出于某种原因,我的攻击系统和NavMesh(可能)不希望协同工作。我使用navmesh系统进行移动。除非我开始增加战斗系统,否则一切都会很好。不幸的是,当您单击敌人进行攻击时,它会移动并发动攻击,但是您根本无法移动。结束的唯一方法是再次攻击敌人(这会导致奇怪的攻击动作)。然后,角色将能够再次移动。而Idk为什么但这种故障仅在所有情况下的30%-50%发生。

GIF(抱歉打错):IssueGif

MouseManager.cs:

 private void Update()
{
    RaycastHit hit; // Инициализируем Raycast
    if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 50, clickableLayer.value))
    {
        Cursor.SetCursor(targetMouse, new Vector2(16, 16), CursorMode.Auto);
        // bool door = false;
        if (hit.collider.gameObject.tag == "Chest" || hit.collider.gameObject.tag == "CraftingTable")
        {
            Cursor.SetCursor(doorwayMouse, new Vector2(16, 16), CursorMode.Auto);
           // door = true;
        }
        bool isAttackable = hit.collider.GetComponent(typeof(IAttackable)) != null;
        if (isAttackable)
        {
            Cursor.SetCursor(targetMouse, new Vector2(16, 16), CursorMode.Auto);
        }

        if (Input.GetMouseButtonDown(1))
        {
            OnClickEnviroment.Invoke(hit.point);
        }

        if (hit.collider.gameObject.tag == "Chest")
        {
            PickItemUpManager();
        }

        else if (hit.collider.gameObject.tag == "CraftingTable" && Input.GetMouseButton(0))
        {

            OpenInteractable();
        }

        else if (isAttackable && Input.GetMouseButtonDown(0))
        {
            GameObject attackable = hit.collider.gameObject;
            OnClickAttackable.Invoke(attackable);
        }

    }
    else
    {
        Cursor.SetCursor(pointerMouse, Vector2.zero, CursorMode.Auto);
    }
}

private void PickItemUpManager()
{
    clicked = FindObjectsOfType<ItemPickUp>();
    if (Input.GetMouseButton(0))
    {

        for (int i = 0; i < clicked.Length; i++)
        {
            clicked[i].PickItemUp();
        }
    }
}

private void OpenInteractable()
{
    if (Input.GetMouseButton(0))
    {

        for (int i = 0; i < craftingWindows.Length; i++)
        {
            craftingWindows[i].OpenCraftingTable();
        }
    }
}

CharacterController.cs:

public class CharacterController : MonoBehaviour {
public AttackDefinition attackWeapon;

Animator animator;
NavMeshAgent agent;
Character stats;

private GameObject attackTarget;

private void Awake()
{
    animator = GetComponent<Animator>();
    agent = GetComponent<NavMeshAgent>();
    agent.updateRotation = false;

    stats = GetComponent<Character>();
}

private void Update()
{
    animator.SetFloat("Speed", agent.velocity.magnitude);
}
private void LateUpdate()
{
    if (agent.velocity.sqrMagnitude > Mathf.Epsilon)
    {
        transform.rotation = Quaternion.LookRotation(agent.velocity.normalized);
    }

}
public void SetDestination(Vector3 destination)
{
    StopAllCoroutines();
    agent.isStopped = false;
    agent.destination = destination;
}


public void AutoAttack(GameObject target)
{
    var weapon = attackWeapon;

    if (weapon != null)
    {
        StopAllCoroutines();

        agent.isStopped = false;
        attackTarget = target;
        StartCoroutine(PursueAndAttack());

    }
    agent.isStopped = false; // Без этой строчки не начинает ходить соовсем


    /*
    animator.SetTrigger("Attack");
    var attack = attackWeapon.CreateAttack(stats, target.GetComponent<Character>());
    var attackables = target.GetComponentsInChildren(typeof(IAttackable));

    foreach(IAttackable atackable in attackables)
    {
        atackable.OnAttack(gameObject, attack);
    }
    */
}

private IEnumerator PursueAndAttack()
{
    agent.isStopped = false;
    var weapon = attackWeapon;
    while(Vector3.Distance(transform.position, attackTarget.transform.position) > weapon.Range)
    {
        agent.destination = attackTarget.transform.position;
        yield return null;
    }

    agent.isStopped = true;
    transform.LookAt(attackTarget.transform);
    animator.SetTrigger("Attack");
    Hit();

}

public void Hit()
{

    if (attackTarget != null)
    {
        attackWeapon.ExecuteAttack(gameObject, attackTarget);
    }
}
}

ExecuteAttack():

public void ExecuteAttack(GameObject attacker, GameObject defender)
    {
        if (defender == null)
            return;

        // Check if defender is in range of the attack
        if (Vector3.Distance(attacker.transform.position, defender.transform.position) > Range)
            return;

        // Check if defender is in front of the player
        if (!attacker.transform.IsFacingTarget(defender.transform))
            return;

        // Here Attack happens
        var attackerStats = attacker.GetComponent<Character>();
        var defenderStats = defender.GetComponent<Character>();

        var attack = CreateAttack(attackerStats, defenderStats);

        var attackables = defender.GetComponentsInChildren(typeof(IAttackable));
        foreach (IAttackable a in attackables)
        {
            a.OnAttack(attacker, attack);
        }
    }

谢谢您的帮助!

c# unity3d game-development
1个回答
0
投票

实际上,这很容易解决(经过xd几个小时的思考)。1)我删除了CharacterController.cs中的'agent.isStopped = false;'2)但是它开始冲破敌人,所以3)我决定使用.enabled = false,而不是使用'.isStopped'。现在效果很好

© www.soinside.com 2019 - 2024. All rights reserved.