Search Unity

How to add distance to a script?

Discussion in 'Scripting' started by undyingwill, Mar 14, 2017.

  1. undyingwill

    undyingwill

    Joined:
    Feb 3, 2016
    Posts:
    92
    Hello! I have that simple navmesh script, which allows ai to follow the playerr, but ai follows from unlimited distance. What lines of code I have to add into the script to make AI follow player only when he see him, or on some meters away or on collider... Thanks
     
  2. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    816
    Trigger Collider acting as visual range(doesn't ncessarily have to be a sphere)
    check if object within trigger is valid target
    raycast to target to make sure there are no obstacles preventing sight of valid target
    move to target

    Simplest logic steps I can think of on short notice. Any other move complex behaviours would probably require trigger colliders at doors to trigger alternate behaviours and/or timers to determine when to stop pursuit.
     
  3. undyingwill

    undyingwill

    Joined:
    Feb 3, 2016
    Posts:
    92
    I have to put the script not on the AI, but on child of AI with trigger collider, so script would work only when player is inside collider?
     
  4. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    816
    I have no idea how you have written the script so I can't comment. All I gave you was the logic, how you executed it is beyond my knowledge. You will have to post the script up for analysis.
     
  5. undyingwill

    undyingwill

    Joined:
    Feb 3, 2016
    Posts:
    92
    That is the script. Sorry, but I dont know how to format is as script in this forum.

    using UnityEngine;
    using UnityEngine.AI;


    public class AIMove : MonoBehaviour {

    NavMeshAgent agent;
    public Transform target;
    void Start ()
    {
    agent = GetComponent<NavMeshAgent> ();
    }


    void Update ()
    {
    agent.SetDestination(target.position);
    }

    }
     
  6. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    816
  7. undyingwill

    undyingwill

    Joined:
    Feb 3, 2016
    Posts:
    92
    Is not in child right now, that is just my idea to detect player from a limited distance. If I make AI collider bigger, something for sure will not work correctly
     
  8. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    816
    yes you are right your movement simply wont happen. infact unity will simply throw an error at you upon starting since your child object most likely does not have a navMeshAgent component.

    what you have to do is make the child with the detectPlayer script inform the parent it has detected the player. the following is only one way to do it, but for now it's most likely the simplest to understand.
    Code (CSharp):
    1. //in parent script
    2. public void TargetAquired(Transform t){
    3.     target = t;
    4. }
    Code (CSharp):
    1. //in child
    2. AIMove parentScript;//this is the script you posted up earlier
    3. void Awake(){
    4.     parentScript = transform.parent.GetComponent<AIMove>();
    5. }
    6. void OnTriggerEnter(Collider col){
    7.     //this is just an example of detecting the player through tags, change the condition as you see fit
    8.     if(col.tag == "player")
    9.     {
    10.         parentScript.TargetAquired(col.transform);//calling the public method created in your parent script.
    11.     }
    12. }
     
  9. undyingwill

    undyingwill

    Joined:
    Feb 3, 2016
    Posts:
    92
    i've written this part of script:

    Code (JavaScript):
    1. function Start ()
    2. {
    3.     agent = GetComponent(UnityEngine.AI.NavMeshAgent);
    4. }
    5.  
    6. function OnTriggerExit (Col : Collider)
    7. {
    8.     if(Col.tag =="Player")
    9.     {
    10.         activateTrigger = false;    //Disables the Trigger
    11.  
    12.     }
    13. }
    14.  
    15. function OnTriggerEnter (Col : Collider)
    16. {
    17.     if(Col.tag =="Player")
    18.     {
    19.         activateTrigger = true;
    20.  
    21.  
    22.         if(Input.GetKeyDown(KeyCode.E))
    23.         {
    24.             activateTrigger = true;
    25.  
    26.         }
    27.     }
    28. }
    The thing is, what to add to - if(Col.tag =="Player"), so ai detects first trigger (or better distance) and if is true, then go to tag player. It should be something like - if distance between ai and player is <= then = go to tag "player"