Search Unity

FPS 1.36 AI randomizing course

Discussion in 'Scripting' started by FirePlantGames, Feb 12, 2013.

  1. FirePlantGames

    FirePlantGames

    Joined:
    Dec 11, 2012
    Posts:
    49
    so i'm here AGAIN and with a new problem(obviously)so every thing works BUT when i'm in a different cell as the enemy He doesn't move toward me He just moves randomly in his current cell(please note that he DOES come toward me when i'm in his cell) so what am I doing wrong this time. code:

    enemy movement script:
    Code (csharp):
    1.  
    2. var currentCell : GameObject;
    3. var playerMovementScript : PlayerMovementScript;
    4. var playerTransform : Transform;
    5. var playerCell : GameObject;
    6. var goalDoor : GameObject;
    7. var shortestPathSoFar : float;
    8. @HideInInspector
    9. var waitToStart : int = 5;
    10. var currentMoveSpeed : float = 5;
    11. var randomizedCourse : boolean = false;
    12. var randomizeCourseVector : Vector3;
    13. var calculatedNewRandomizeCourseVector : boolean;
    14.  
    15.  
    16.  
    17. function Awake ()
    18. {
    19. shortestPathSoFar = Mathf.Infinity;
    20. playerMovementScript = GameObject.FindWithTag("Player").GetComponent(PlayerMovementScript);
    21. playerTransform = GameObject.FindWithTag("Player").transform;
    22. waitToStart = 5;
    23. randomizeCourseVector = transform.position;
    24. }
    25.  
    26.  
    27.  
    28. function Update ()
    29. {
    30. if (waitToStart <= 0)
    31. {
    32. playerCell = playerMovementScript.CurrentCell;
    33. for (var doorCheckingNow : GameObject in currentCell.GetComponent(PathCell).doors)
    34. {
    35. for (var i : int = 0; i <= doorCheckingNow.GetComponent(AIdoorScript).cells.length - 1; i++)
    36. {
    37. if (doorCheckingNow.GetComponent(AIdoorScript).cells[i] == playerCell)
    38. if (doorCheckingNow.GetComponent(AIdoorScript).doorsToCells[i] < shortestPathSoFar)
    39. {
    40. goalDoor = doorCheckingNow;
    41. shortestPathSoFar = doorCheckingNow.GetComponent(AIdoorScript).doorsToCells[i];
    42. }
    43. }
    44. }
    45. shortestPathSoFar = Mathf.Infinity;
    46. }
    47. waitToStart -= 1;
    48. if (!calculatedNewRandomizeCourseVector)
    49. {
    50. randomizeCourseVector = FindRandomSpotInCurrentCell();
    51. calculatedNewRandomizeCourseVector = true;
    52. }
    53. if (currentCell != playerCell || playerCell == null)
    54. {
    55. if (randomizedCourse)
    56. transform.position += (goalDoor.transform.position - transform.position).normalized * currentMoveSpeed * Time.deltaTime;
    57. if (!randomizedCourse)
    58. {
    59. transform.position += (randomizeCourseVector - transform.position).normalized * currentMoveSpeed * Time.deltaTime;
    60. if (Vector3.Distance(transform.position, randomizeCourseVector) < transform.localScale.x)
    61. {
    62. if (goalDoor)
    63. randomizedCourse = true;
    64. if (goalDoor == null)
    65. calculatedNewRandomizeCourseVector = false;
    66. }
    67. }
    68. }
    69. if (playerCell == currentCell)
    70. transform.position += (playerTransform.position - transform.position).normalized * currentMoveSpeed * Time.deltaTime;
    71. }
    72.  
    73.  
    74.  
    75. function OnTriggerEnter (hitTrigger : Collider)
    76. {
    77. if (hitTrigger.tag == "AIPathCell")
    78. {
    79. currentCell = hitTrigger.gameObject;
    80. randomizedCourse = false;
    81. calculatedNewRandomizeCourseVector = false;
    82. }
    83. }
    84.  
    85. function FindRandomSpotInCurrentCell ()
    86. {
    87. 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)));
    88. }
    89.  
    door script:
    Code (csharp):
    1.  
    2. var cells = new Array();
    3. var doorsToCells = new Array();
    4. var immediateCells = new Array();
    5. var testForCells : boolean = false;
    6. var waitToTestCells : float = 2;
    7. var stage : int = 1;
    8. @HideInInspector
    9. var doorOpen : boolean = true;
    10.  
    11.  
    12. function Awake()
    13. {
    14. doorOpen = true;
    15. cells = GameObject.FindGameObjectsWithTag("AIPathCell");
    16. doorsToCells.length = cells.length;
    17. testForCells = true;
    18. waitToTestCells = 2;
    19. stage = 1;
    20. }
    21.  
    22. function Update()
    23. {
    24.  
    25.  
    26. if (testForCells  waitToTestCells <= 0)
    27. {
    28. for (var immediateCells : GameObject in immediateCells)
    29. {
    30. for (var i : int = 0;i <= cells.length - 1; i++)
    31. {
    32. if (cells[i] == immediateCells)
    33. doorsToCells[i] = 1;
    34. }
    35. }
    36. for (stage = 2; stage <= cells.length - 1; stage++)
    37. {
    38. for (i = 0; i <= cells.length - 1; i++)
    39. {
    40. if (doorsToCells[i] == stage - 1)
    41. for (var checkDoor : GameObject in cells[i].GetComponent(PathCell).doors)
    42. {
    43. if (checkDoor != gameObject)
    44. {
    45. for (var checkCell : GameObject in checkDoor.GetComponent(AIdoorScript).immediateCells)
    46. {
    47. for (var j : int = 0; j <= cells.length - 1; j++)
    48. {
    49. if (cells[j] == checkCell  doorsToCells[j] == null)
    50. doorsToCells[j] = stage;
    51. }
    52. }
    53. }
    54. }
    55. }
    56. }
    57. testForCells = false;
    58. }
    59. waitToTestCells -= 1;
    60. }
    61.  
    62. function OnTriggerEnter (other : Collider)
    63. {
    64. if (other.tag == "AIPathCell")
    65. immediateCells.Add(other.gameObject);
    66. }
    67.