Jump to content
Unity Insider Forum

AI funktioniert nicht richtig


Recommended Posts

Hallo,

ich habe ein Problem mit meiner AI. Ich habe das Navmesh und ein Script, dass die Bewegung des Gegners steuert. Außerdem habe ich dem Gegner einen Cube als Trigger zum angreifen gegeben und eine Zone als Sichtfeld des Gegners. Außerdem habe ich dem Gegner eine Angriffsmusik gegeben und einen Bloodscreen der aktiviert wird wenn der Gegner zuschlägt. Mein Problem ist nun, dass der Gegner nicht anfängt zu patrouillieren, sondern auf einer Stelle läuft bis er den Spieler entdeckt hat. Erst danach fängt der Gegner seine Routine an.

Vielen Dank.

sternenhimmelglanz

 

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

public class EnemyAttack : MonoBehaviour
{
    private NavMeshAgent Nav;
    private NavMeshHit hit;
    private bool blocked = false;
    private bool RunToPlayer = false;
    private float DistanceToPlayer;
    private bool IsChecking = true;
    private int FailedChecks = 0;

    public Transform Player;
    public Animator Anim;
    public GameObject Enemy;
    public float MaxRange = 35.0f;
  
    public int MaxChecks = 3;
    public float ChaseSpeed = 8.5f;
    public float WalkSpeed = 1.6f;
    public float AttackDistance = 2.3f;
    public float AttackRotateSpeed = 2.0f;
    public float CheckTime = 3.0f;
    public GameObject ChaseMusic;
    public GameObject HurtUI;

    // Start is called before the first frame update
    void Start()
    {
        Nav = GetComponentInParent<NavMeshAgent>();
        ChaseMusic.gameObject.SetActive(false);
    }

    // Update is called once per frame
    void Update()
    {
            DistanceToPlayer = Vector3.Distance(Player.position, Enemy.transform.position);
            if (DistanceToPlayer < MaxRange)
            {
                if (IsChecking == true)
                {
                    IsChecking = false;

                    blocked = NavMesh.Raycast(transform.position, Player.position, out hit, NavMesh.AllAreas);

                    if (blocked == false)
                    {
                        Debug.Log("I can see the Player");
                        RunToPlayer = true;
                        FailedChecks = 0;
                    }
                    if (blocked == true)
                    {
                        Debug.Log("Where did the Player go?");
                        RunToPlayer = false;
                        Anim.SetInteger("State", 1);
                        FailedChecks++;
                    }

                    StartCoroutine(TimedCheck());
                }
            }

            if (RunToPlayer == true)
            {
                Enemy.GetComponent<EnemyMove>().enabled = false;
                ChaseMusic.gameObject.SetActive(true);
               
                if (DistanceToPlayer > AttackDistance)
                {
                    Nav.isStopped = false;
                    Anim.SetInteger("State", 2);
                    Nav.acceleration = 24;
                    Nav.SetDestination(Player.position);
                    Nav.speed = ChaseSpeed;
                    HurtUI.gameObject.SetActive(false);
                }
                if (DistanceToPlayer < AttackDistance - 0.5f)
                {
                    Nav.isStopped = true;
                    Debug.Log("I am attacking");
                    Anim.SetInteger("State", 3);
                    Nav.acceleration = 180; 
                    HurtUI.gameObject.SetActive(true);

                    Vector3 Pos = (Player.position - Enemy.transform.position).normalized;
                    Quaternion PosRotation = Quaternion.LookRotation(new Vector3(Pos.x, 0, Pos.z)); 
                    Enemy.transform.rotation = Quaternion.Slerp(Enemy.transform.rotation, PosRotation, Time.deltaTime * AttackRotateSpeed); 
                }
            }
            else if (RunToPlayer == false)
            {
                Nav.isStopped = true;
            }
        }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("Player"))
        {
            RunToPlayer = true;
        }

    }

    IEnumerator TimedCheck()
    {
        yield return new WaitForSeconds(CheckTime);
        IsChecking = true;

        if (FailedChecks > MaxChecks)
        {
            Enemy.GetComponent<EnemyMove>().enabled = true;
            Nav.isStopped = false;
            Nav.speed = WalkSpeed;
            FailedChecks = 0;
            ChaseMusic.gameObject.SetActive(false);
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class EnemyMove : MonoBehaviour
{
    private NavMeshAgent Nav;
    private Animator Anim;
    private Transform TheTarget;
    private float DistanceToTarget;
    private int TargetNumber = 1;
    private bool HasStopped = false;
    private bool Randomizer = true;
    private int NextTargetNumber;

    public float StopDistance = 2.0f;
    public int MaxTargets = 5;
    public float WaitTime = 1.5f;
    public Transform Target1;
    public Transform Target2;
    public Transform Target3;
    public Transform Target4;
    public Transform Target5;

    // Start is called before the first frame update
    void Start()
    {
        Nav = GetComponent<NavMeshAgent>();
        Anim = GetComponent<Animator>();
        TheTarget = Target1;
    }

    // Update is called once per frame
    void Update()
    {
        DistanceToTarget = Vector3.Distance(TheTarget.position, transform.position);
        if (DistanceToTarget > StopDistance)
        {
            Nav.SetDestination(TheTarget.position);
            Anim.SetInteger("State", 0);
            Nav.isStopped = false;
            NextTargetNumber = TargetNumber;
            Nav.speed = 1f;
        }
        if (DistanceToTarget < StopDistance)
        {
            Nav.isStopped = true;
            Anim.SetInteger("State", 1);
            StartCoroutine(LookAround());
        }
    }

    void SetTarget()
    {
        if (TargetNumber == 1)
        {
            TheTarget = Target1;
        }
        if (TargetNumber == 2)
        {
            TheTarget = Target2;
        }
        if (TargetNumber == 3)
        {
            TheTarget = Target3;
        }
        if (TargetNumber == 4)
        {
            TheTarget = Target4;
        }
        if (TargetNumber == 5)
        {
            TheTarget = Target5;
        }

    }

    IEnumerator LookAround()
    {
        yield return new WaitForSeconds(WaitTime);

        if (HasStopped == false)
        {
            HasStopped = true;
            if (Randomizer == true)
            {
                Randomizer = false;
                TargetNumber = Random.Range(1, MaxTargets);

                if (TargetNumber == NextTargetNumber)
                {
                    TargetNumber++;

                    if (TargetNumber >= MaxTargets)
                    {
                        TargetNumber = 1;
                    }
                }
            }
            SetTarget();

            yield return new WaitForSeconds(WaitTime);
            HasStopped = false;
            Randomizer = true;
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyWeaponDamage : MonoBehaviour
{
    public int WeaponDamage = 1;
    public Animator HurtAnim;
    private bool HitActive = false;
    public AudioSource MyPlayer;
    public GameObject Player;

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("Player"))
        {
            if (HitActive == false)
            {
                HitActive = true;
                HurtAnim.SetTrigger("Hurt");
                SaveScript.PlayerHealth -= WeaponDamage;
                SaveScript.HealthChanged = true;
                MyPlayer.Play();
            }
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.gameObject.CompareTag("Player"))
        {
            if (HitActive == true)
            {
                HitActive = false;
            }
        }
    }
}

 

Edited by sternenhimmelglanz
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...