Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

ETeeski 1.36 -AI movement help!

Discussion in 'Community Learning & Teaching' started by SmileyFaceOfRom, Aug 29, 2012.

  1. SmileyFaceOfRom

    SmileyFaceOfRom

    Joined:
    Aug 10, 2012
    Posts:
    6
    Hello! I've been following along with ETeeski's tutorials here, and I've gotten stuck. My enemies can follow me perfectly inside of the requisite cells, but once I move or jump out of the cells, they stop following me. The current cell and goalDoor are always set except when I'm not in a cell, so I can't for the life of me figure out the problem!

    EnemyMovementScript.js
    Code (csharp):
    1.     var currentCell : GameObject;
    2.     var playerMovementScript : PlayerMovementScript;
    3.     var playerTransform : Transform;
    4.     var playerCell : GameObject;
    5.     var goalDoor : GameObject;
    6.     var shortestPathSoFar : float;
    7.     @HideInInspector
    8.     var waitToStart : int = 5;
    9.     var currentMoveSpeed : float = 5;
    10.  
    11.     var randomizedCourse : boolean = false;
    12.     var randomizeCourseVector : Vector3;
    13.     var calculatedNewRandomizeCourseVector : boolean;
    14.      
    15.     function Awake ()
    16.     {
    17.         shortestPathSoFar = Mathf.Infinity;
    18.         playerMovementScript = GameObject.FindWithTag("Player").GetComponent(PlayerMovementScript);
    19.         playerTransform = GameObject.FindWithTag("Player").transform;
    20.         waitToStart = 5;
    21.         randomizeCourseVector = transform.position;
    22.     }
    23.      
    24.     function Update ()
    25.     {
    26.         if (waitToStart <= 0)
    27.         {
    28.             playerCell = playerMovementScript.currentCell;
    29.             for (var doorCheckingNow : GameObject in currentCell.GetComponent(AIPathCellScript).doors)
    30.             {
    31.                 for (var i : int = 0; i <= doorCheckingNow.GetComponent(AIPathDoorScript).cells.length - 1; i++)
    32.                 {
    33.                     if (doorCheckingNow.GetComponent(AIPathDoorScript).cells[i] == playerCell)
    34.                         if (doorCheckingNow.GetComponent(AIPathDoorScript).doorsToCells[i] < shortestPathSoFar)
    35.                         {
    36.                             goalDoor = doorCheckingNow;
    37.                             shortestPathSoFar = doorCheckingNow.GetComponent(AIPathDoorScript).doorsToCells[i];
    38.                         }
    39.                 }
    40.             }
    41.             shortestPathSoFar = Mathf.Infinity;
    42.         }
    43.         waitToStart -= 1;
    44.        
    45.         if (!calculatedNewRandomizeCourseVector)
    46.         {
    47.             randomizeCourseVector = FindRandomSpotInCurrentCell();
    48.             calculatedNewRandomizeCourseVector = true;
    49.         }
    50.        
    51.         if (goalDoor)
    52.             if (!goalDoor.GetComponent(AIPathDoorScript).doorOpen)
    53.                 goalDoor = null;
    54.       //If not in same cell, move towards goalDoor
    55.         if (currentCell != playerCell || playerCell == null)
    56.         {
    57.             if (randomizedCourse)
    58.                 transform.position += (goalDoor.transform.position - transform.position).normalized * currentMoveSpeed * Time.deltaTime;
    59.            
    60.             if (!randomizedCourse)
    61.             {
    62.                 transform.position += (randomizeCourseVector - transform.position).normalized * currentMoveSpeed * Time.deltaTime;
    63.                 if (Vector3.Distance(transform.position, randomizeCourseVector) < transform.localScale.x)
    64.                 {
    65.                     if (goalDoor)
    66.                         randomizedCourse = true;
    67.                     if (goalDoor == null)
    68.                         calculatedNewRandomizeCourseVector = false;
    69.                 }
    70.             }
    71.         }
    72.          //If in same cell, move towards player
    73.         if (playerCell == currentCell)
    74.             transform.position += (playerTransform.position - transform.position).normalized * currentMoveSpeed * Time.deltaTime;
    75.     }
    76.      
    77.     function OnTriggerEnter (hitTrigger : Collider)
    78.     {
    79.         if (hitTrigger.tag == "AIPathCell")
    80.         {
    81.             currentCell = hitTrigger.gameObject;
    82.             randomizedCourse = false;
    83.             calculatedNewRandomizeCourseVector = false;
    84.         }
    85.     }
    86. //
    87.     function FindRandomSpotInCurrentCell ()
    88.     {
    89.         return currentCell.transform.position + (currentCell.transform.rotation * Vector3(Random.Range(currentCell.transform.localScale.x * -0.5,currentCell.transform.localScale.x * 0.5),0,Random.Range(currentCell.transform.localScale.z * -0.5,currentCell.transform.localScale.z * 0.5)));
    90.     }
     
  2. eteeski

    eteeski

    Joined:
    Feb 2, 2010
    Posts:
    476
    in the player movement script, try getting rid of the function OnTriggerExit(){ currentCell = null; } part. or you can mod the code to use a raycast to check for the player's currentCell.
     
  3. SmileyFaceOfRom

    SmileyFaceOfRom

    Joined:
    Aug 10, 2012
    Posts:
    6
    EDIT
    I've just discovered what the issue was: my enemies consisted of several 'cube' parts inside a gameObject that had a box collider, rigidbody and the script. For WHATEVER reason, changing my enemies to be the simple 'blobs' you have them as solved my issue.
    Thanks for your help ETeeski, I really appreciate it! Now to get started on 1.38 >:)
     
    Last edited: Aug 30, 2012