Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

AI Scripting Issue

Discussion in 'Scripting' started by SomeGuy3, Jan 6, 2016.

  1. SomeGuy3

    SomeGuy3

    Joined:
    Dec 19, 2015
    Posts:
    65
    hello, I am trying to make an enemy ai approach a friendly ai, I have done this by using the script:
    Code (CSharp):
    1.  using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class AIZ_Sight : MonoBehaviour
    5. {
    6.  
    7.     private NavMeshAgent agent;
    8.     private AIZ_Movement myAIZ_Movement;
    9.     private float height = 0.5f;
    10.     public float sightDist = 10f;
    11.     public float awareDist = 3f;
    12.     private GameObject survivourJD;
    13.     private GameObject survivourBO;
    14.     public float approachSpeed = 0.75f;
    15.     RaycastHit hit;
    16.     private float timer;
    17.  
    18.  
    19.     void Start()
    20.     {
    21.         survivourJD = GameObject.FindGameObjectWithTag("SurvivourJD");
    22.         survivourBO = GameObject.FindGameObjectWithTag("SurvivourBO");
    23.         agent = GetComponent<NavMeshAgent>();
    24.         myAIZ_Movement = GetComponent<AIZ_Movement>();
    25.     }
    26.  
    27.     void Update()
    28.     {
    29.         timer += Time.deltaTime;
    30.         if (Physics.Raycast(transform.position + Vector3.up * height, transform.forward, out hit, sightDist))
    31.         {
    32.             Approach();
    33.         }
    34.         if (Physics.Raycast(transform.position + Vector3.up * height, Quaternion.AngleAxis(5, transform.up) * transform.forward, out hit, sightDist))
    35.         {
    36.             Approach();
    37.         }
    38.        
    39.         if (Physics.Raycast(transform.position + Vector3.up * height, Quaternion.AngleAxis(-10, transform.up) * transform.forward, out hit, sightDist))
    40. [B].... [script edited to make it bearable to read]....[/B]
    41.         if (Physics.Raycast(transform.position + Vector3.up * height, Quaternion.AngleAxis(-90, transform.up) * transform.forward, out hit, awareDist))
    42.         {
    43.             Approach();
    44.         }
    45.     }
    46.     void Approach()
    47.     {
    48.         if (hit.collider.gameObject.tag == "SurvivourJD")
    49.         {
    50.             agent.SetDestination(survivourJD.transform.position);
    51.             agent.speed = approachSpeed;
    52.             myAIZ_Movement.enabled = false;
    53.         }
    54.             if (hit.collider.gameObject.tag == "SurvivourBO")
    55.         {
    56.             agent.SetDestination(survivourBO.transform.position);
    57.             agent.speed = approachSpeed;
    58.             myAIZ_Movement.enabled = false;
    59.         }
    60.  
    61.     }
    62. }
    63.  
    This all works fine, however when the friendly ai is killed (which is achieved in a seperate script), I want my enemy ai to go back to myAIZ_Movement script. I have tried this by doing:
    Code (CSharp):
    1.  if (hit.collider.gameObject.tag == "SurvivourJD")
    2.         {
    3.             if (survivourJD.activeSelf == true)
    4.             {
    5.                 agent.SetDestination(survivourJD.transform.position);
    6.                 agent.speed = approachSpeed;
    7.                 myAIZ_Movement.enabled = false;
    8.             }
    9.             if (survivourJD.activeSelf == false)
    10.             {
    11.                 myAIZ_Movement.enabled = true;
    12.             }
    13.  
    However when I play this out, my enemy ai just ends up standing where the friendly ai is even when he is dead, if anyone has any tips, please tell, I have also tried several different techniques all to no avail.
     
  2. Svarr

    Svarr

    Joined:
    Dec 20, 2015
    Posts:
    22
    Shouldn't you do the GetComponent<>() calls on a gameObject?
    I think it would help if you attached the AIZ_Movement Script as well.
     
  3. SomeGuy3

    SomeGuy3

    Joined:
    Dec 19, 2015
    Posts:
    65
    I've never used GetComponent<>() on another gameObject before, could you explain why this would be better than just using tags and if so, how to do it?
    the AIZ_Movement script is just a simple go to a location script, its nothing to fancy, I believe the issue with my script is that I need to make it so that if there is no gameObjects in the enemies field of view, it will automatically go back to the AIZ_Movement script, however I am unaware how to do this.
     
  4. Svarr

    Svarr

    Joined:
    Dec 20, 2015
    Posts:
    22
    The documentation shows that you actually can call GetComponent on a component. So the way you did it is fine.
    I realised that leaving the <> brackets empty in my last post can lead to misunderstandings. You can't call GetComponent without specifying the type of component!

    It's important which methods the AIZ_Movement script uses. It may be that you'd have to call them manually after activating the component.
    Also, what game object is the AIZ_Sight script attached to?
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    if the survivor has been removed it's not going to return a hit with their tag.

    Code (csharp):
    1.  
    2. void Approach()
    3.     {
    4.         if (hit.collider.gameObject.tag == "SurvivourJD")
    5.         {
    6.             agent.SetDestination(survivourJD.transform.position);
    7.             agent.speed = approachSpeed;
    8.             myAIZ_Movement.enabled = false;
    9.         }
    10.         else if (hit.collider.gameObject.tag == "SurvivourBO")
    11.         {
    12.             agent.SetDestination(survivourBO.transform.position);
    13.             agent.speed = approachSpeed;
    14.             myAIZ_Movement.enabled = false;
    15.         }
    16.         else
    17.         {
    18.             myAIZ_Movement.enabled = true;
    19.         }
    20.  
    21.     }
    22.  
     
  6. SomeGuy3

    SomeGuy3

    Joined:
    Dec 19, 2015
    Posts:
    65
    quick update:
    I have just solved this issue by making another gameObject teleport to the area in which an friendly AI has been destroyed. This gameObject then enables the AIZ_Movement script, although this may not be the cleanest way of achieving my goal, it seems to work very well, thank you for your help though!