Search Unity

Strange Pathfinding with Navmesh[Video Link Included]

Discussion in 'Navigation' started by warrencwwong, Apr 21, 2020.

?

Should I make a new AI system or continue using this old one?

  1. New One

    0 vote(s)
    0.0%
  2. Old One

    0 vote(s)
    0.0%
  1. warrencwwong

    warrencwwong

    Joined:
    Aug 17, 2018
    Posts:
    25

    Hello! I have made this simple robot game with Navmesh agents, the agents try to reach the player's waypoints. However, the robots, as you see in the video, zoom back and forth for no reason. Also, the AI logic sucks as the agents go to their waypoints, which is decided at the start; but sometimes the waypoint is furthest away from the agent and the agent has to go a long way to that waypoint. It's extremely annoying.
    - Can you please help me write an AI script(as the behavior of the robots just isn't satisfactory) and explain how to make a good enemy attack logic?
    - Or can you recommend me some free, easy to use AI assets?
    Thank you very much
    Enemy script:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.AI;
    public class Enemy : MonoBehaviour {
    public Transform player;
    public float EnemyHeight = 2f;
    public float speed;

    public float rotSpeed = 5;
    public float distancebeforeshoot = 5;
    public bool shoot;
    public float Health = 100;
    public GameObject explosion;
    public int BotNumber;
    public WaveManager waveManager;
    public NavMeshAgent agent;

    void Update()
    {
    player = GameObject.Find("Player").transform;

    transform.localEulerAngles = new Vector3(0, transform.localEulerAngles.y, 0);
    float distance = Vector3.Distance(transform.position, player.transform.position);
    ApproachWaypoint();
    if (distance > distancebeforeshoot)
    {

    shoot = false;
    agent.speed = 5f;

    }
    else
    {

    shoot = true;
    agent.speed = 0;

    }
    transform.LookAt(player);
    if(Health <= 0)
    {
    Die();
    }
    }
    void Die()
    {
    Instantiate(explosion, transform.position, Quaternion.identity);
    waveManager.enemies.Remove(transform);
    Destroy(gameObject);


    }
    void ApproachWaypoint()
    {
    PlayerHealth playerHealth = player.GetComponent<PlayerHealth>();

    if (!playerHealth.WayPoints.Contains(playerHealth.WayPoints[BotNumber]))
    {
    return;
    }

    agent.SetDestination(playerHealth.WayPoints[BotNumber].position);
    }

    }
    PlayerHealth:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using TMPro;
    public class PlayerHealth : MonoBehaviour {
    public float Health;
    public TextMeshProUGUI healthText;
    public Image image;
    public List<Transform> WayPoints;
    public List<Transform> OccupiedWaypoints;

    public GameObject panel;
    public List<Image> blood;
    public RetrySystem retrysystem;

    public List<Renderer> graphics;
    public GameObject soldier;
    public Image image2;
    public float B = 0;
    public bool Dead;
    public Animator animator;
    void Start () {
    //Health = 100;
    Dead = false;
    }

    void Update () {

    for(int i = 0; i < WayPoints.Count; i++)
    {
    if(WayPoints.GetComponent<WayPoint>().colliding)
    {
    if(WayPoints == null)
    {
    return;
    }
    WayPoints.Remove(WayPoints);
    }
    }

    if (Health <= 0)
    {
    if (B >= 1)
    {
    panel.SetActive(true);

    return;
    }
    if (B < 1)
    {
    B = B + 0.003f;
    }
    image2.color = new Color(image2.color.r, image2.color.g, image2.color.b, B);
    }
    healthText.text = Health.ToString() + " HP";
    image.fillAmount = Health / 100;
    float A = 1 - Health + 50;
    for (int i = 0; i < blood.Count; i++)
    {

    blood.color = new Color(blood.color.r, blood.color.g, blood.color.b, A / 100);
    if(Health <= 0)
    {
    blood.color = new Color(blood.color.r, blood.color.g, blood.color.b, 0);
    }
    }
    if (Health <= 0)
    {
    animator.SetBool("Dead", true);
    retrysystem.Die();

    if (Dead)
    {
    return;
    }
    for (int i = 0; i < graphics.Count; i++)
    {

    graphics.enabled = false;
    soldier.SetActive(true);
    Cursor.lockState = CursorLockMode.None;
    Dead = true;
    }
    }
    else
    {
    if (animator.gameObject.activeSelf)
    {
    animator.SetBool("Dead", false);
    }
    Dead = false;
    Cursor.lockState = CursorLockMode.Locked;
    }


    }

    }
    Waypoint script(Put on WayPoints):
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class WayPoint : MonoBehaviour {
    public bool colliding;
    public PlayerHealth playerHealth;
    public Vector3 offset;
    private void Start()
    {
    offset = transform.position - playerHealth.transform.position;
    }
    private void Update()
    {
    transform.position = playerHealth.transform.position + offset;
    }
    private void OnTriggerEnter(Collider other)
    {
    colliding = true;
    }
    private void OnTriggerExit(Collider other)
    {
    colliding = false;
    if (!playerHealth.WayPoints.Contains(transform))
    {
    playerHealth.WayPoints.Add(transform);
    }
    }
    }
     
  2. warrencwwong

    warrencwwong

    Joined:
    Aug 17, 2018
    Posts:
    25
    EDIT: also, the pushing around behavior is very annoying. Here is the baked map:
     
  3. warrencwwong

    warrencwwong

    Joined:
    Aug 17, 2018
    Posts:
    25