Search Unity

Help With pacman game Enemy AI

Discussion in 'Scripting' started by gdossantos87, May 16, 2015.

  1. gdossantos87

    gdossantos87

    Joined:
    Mar 18, 2014
    Posts:
    43
    Helo guys this time im trying to create the panic state of the ghost
    Code (csharp):
    1.  
    2. [RequireComponent(typeof(NavMeshAgent))]
    3. public class EnemyAI : MonoBehaviour {
    4.  
    5.    // Use this for initialization
    6.  
    7.   private NavMeshAgent nav;
    8.  
    9.   public float patrolSpeed = 2.0f;
    10.   public float chaseSpeed = 4.5f;
    11.   public float runawaySpeed = 3.5f;
    12.  
    13.  
    14.   public Transform[] waypoints;
    15.  
    16.   private int curWaypoint = 0;
    17.   private int maxWaypoint;
    18.   private float timeSinceLastCall;
    19.   private Transform player;
    20.  
    21.  
    22.   private SphereCollider spherecollider;
    23.   private PlayerAttack playerAttack;
    24.   private GameObject bigPill;
    25.   private PowerUp powerUp;
    26.   private Renderer rend;
    27.  
    28.  
    29.   private EnemyAttack enemyAttack;
    30.  
    31.   public float minWaypointDistance = 0.0f;
    32.  
    33.    void Awake () {
    34.  
    35.   nav = GetComponent<NavMeshAgent>();
    36.     rend = GetComponent<Renderer>();
    37.    
    38.    
    39.     player = GameObject.FindGameObjectWithTag("Player").transform;
    40.     bigPill = GameObject.FindGameObjectWithTag("BigPill").gameObject;
    41.     powerUp = bigPill.GetComponent<PowerUp>();
    42.     playerAttack = player.GetComponent<PlayerAttack>();
    43.  
    44.  
    45.    
    46.     enemyAttack = GetComponent<EnemyAttack>();
    47.     spherecollider = GetComponent<SphereCollider>();
    48.  
    49.   maxWaypoint = waypoints.Length - 1;
    50.  
    51.    }
    52.  
    53.    // Update is called once per frame
    54.     void Update () {
    55.  
    56.   Patrolling();
    57.   AttackThePlayer();
    58.  
    59.     AvoidThePlayer();
    60.   AvoidThePlayeTime();
    61.  
    62.  
    63.  
    64.  
    65.  
    66.     }
    67.  
    68.  
    69.  
    70.   public void Patrolling()
    71.   {
    72.   nav.speed = patrolSpeed;
    73.  
    74.  
    75.   Vector3 tempLocalPosition = transform.position;
    76.   tempLocalPosition.y = 0f;
    77.  
    78.   Vector3 tempWaypointPosition = waypoints[curWaypoint].position;
    79.   tempWaypointPosition.y = 0f;
    80.  
    81.   if (Vector3.Distance(tempLocalPosition, tempWaypointPosition) <= minWaypointDistance)
    82.   {
    83.   if (curWaypoint == maxWaypoint)
    84.   curWaypoint = 0;
    85.   else
    86.   curWaypoint++;
    87.   }
    88.  
    89.   nav.SetDestination(waypoints[curWaypoint].position);
    90.  
    91.  
    92.   }
    93.  
    94.   public void AttackThePlayer()
    95.   {
    96.  
    97.   if (enemyAttack.playerInRange == true)
    98.   {
    99.  
    100.  
    101.   nav.speed = chaseSpeed;
    102.   nav.SetDestination(player.position);
    103.   }
    104.  
    105.  
    106.  
    107.   }
    108.  
    109.   public void AvoidThePlayer()
    110.   {
    111.  
    112.  
    113.   if (PowerUp.powerUpIsActive == true)
    114.   {
    115.  
    116.  
    117.   nav.speed = runawaySpeed;
    118.   rend.material.color = Color.blue;
    119.   nav.SetDestination(transform.position - player.position);
    120.   spherecollider.radius = 1.0f;
    121.  
    122.  
    123.   }
    124.  
    125.   }
    126.  
    im trying to make the avoidThePlayerMethod to just last like 10 seconds

    Code (csharp):
    1.  
    2. public IEnumerator AvoidThePlayer(float maxTimeCall)
    3.   {
    4.   if (PowerUp.powerUpIsActive == true)
    5.   {
    6.   timeSinceLastCall += Time.deltaTime;
    7.   Debug.Log(timeSinceLastCall);
    8.   nav.speed = runawaySpeed;
    9.   rend.material.color = Color.blue;
    10.   nav.SetDestination(transform.position - player.position);
    11.   spherecollider.radius = 1.0f;
    12.   if (timeSinceLastCall >= maxTimeCall)
    13.   {
    14.   Debug.Log("Termino el counter!");
    15.   Patrolling();
    16.   timeSinceLastCall = 0;
    17.   }
    18.  
    19.   }
    20.  
    21.   yield return new WaitForEndOfFrame();
    22.   }
    23.  
    but it does not work, you guys hace any suggestions???? plz help
    sorry guys im really tired...big thank you for your time
     
  2. SnakeTheRipper

    SnakeTheRipper

    Joined:
    Dec 31, 2014
    Posts:
    136
    You are calling all the methods on Update(), that's not cool. There should be some conditional there.

    To make the panic mode last 10 seconds, you should set the enemy on panic mode and use :

    Code (CSharp):
    1. Invoke("MethodName", 10f);
    Where MethodName is the method that puts the enemies back to a normal state.
     
  3. gdossantos87

    gdossantos87

    Joined:
    Mar 18, 2014
    Posts:
    43
    Like this?

    Code (csharp):
    1.  
    2. public void AvoidThePlayer()
    3.   {
    4.  
    5.  
    6.   if (PowerUp.powerUpIsActive == true)
    7.   {
    8.    
    9.    
    10.   nav.speed = runawaySpeed;
    11.   rend.material.color = Color.blue;
    12.   nav.SetDestination(transform.position - player.position);
    13.   spherecollider.radius = 1.0f;
    14.    
    15.   Invoke("Patrolling",5f);
    16.   }
    17.  
    18.   }
    19.  
     
  4. SnakeTheRipper

    SnakeTheRipper

    Joined:
    Dec 31, 2014
    Posts:
    136
    Yeah, and in the Patrolling method you should put the enemy back to the normal state.
     
  5. gdossantos87

    gdossantos87

    Joined:
    Mar 18, 2014
    Posts:
    43
    actually, the patrolling method is the default state
     
  6. gdossantos87

    gdossantos87

    Joined:
    Mar 18, 2014
    Posts:
    43
    i have read online about scripting a State machine using the switch case method but i don't have much experience on it
     
  7. SnakeTheRipper

    SnakeTheRipper

    Joined:
    Dec 31, 2014
    Posts:
    136
    Yes, switch is really usefull for this cases.
     
  8. gdossantos87

    gdossantos87

    Joined:
    Mar 18, 2014
    Posts:
    43
    do you know how can i implement it???? by the way thank you for your time im very grateful
     
  9. SnakeTheRipper

    SnakeTheRipper

    Joined:
    Dec 31, 2014
    Posts:
    136
    Well, I've never done that before. First off, how many states will the enemy have ?
     
  10. gdossantos87

    gdossantos87

    Joined:
    Mar 18, 2014
    Posts:
    43
    Just 3, patrolling chasingplayer and awayfrom player, I have never used it before
     
  11. SnakeTheRipper

    SnakeTheRipper

    Joined:
    Dec 31, 2014
    Posts:
    136
    Okay then. You need to switch on an int variable.

    Code (CSharp):
    1. public int currentState;
    2.  
    3.     private void Update()
    4.     {
    5.         switch (currentState)
    6.         {
    7.             case 0:
    8.                 Patrol();
    9.                 break;
    10.  
    11.             case 1:
    12.                 Chase();
    13.                 break;
    14.  
    15.             case 2:
    16.                 Avoid();
    17.                 break;
    18.         }
    19.     }
    Normal state is patrolling as you said, so when currentState is 0 it will keep patrolling like normal. 1 will stand for chasing the player. When the player hits the thing that makes all enemies avoid him, you need to set currentState to 2.

    By the way, I thought there were only 2 possible states (apart from dead or moving back to the center), chasing and avoiding.

    What does patrolling do ?
     
  12. gdossantos87

    gdossantos87

    Joined:
    Mar 18, 2014
    Posts:
    43
    it fint the waypoints that are set in the scene, to simulate the "patroling" , oh yeah ! I'm missing those 2 states too, death and respawn in the center, for testing purouses the death state its just the destroy method, that's on another script
     
  13. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    If no-one has mentioned it yet, I highly suggest taking a look at the Stealth Project's AI tutorial on Unity's Learn Modules.
    https://unity3d.com/learn/tutorials/projects/stealth/enemy-ai

    For what you need, you would only need half of what they have used, plus it is good to get into the habit if checking the Unity tutorials, pieces of them can be incredibly useful.
     
  14. gdossantos87

    gdossantos87

    Joined:
    Mar 18, 2014
    Posts:
    43
    thank you very much guys, i will check out the tutorial
     
  15. SnakeTheRipper

    SnakeTheRipper

    Joined:
    Dec 31, 2014
    Posts:
    136
    Cool. Comment below if you find any trouble.
     
  16. gdossantos87

    gdossantos87

    Joined:
    Mar 18, 2014
    Posts:
    43
    i wont give up on this plz help me!, ok my timer starts but the AvoidThePlayer() functions does not trigger and the timer keeps countin until the powerUpStop variable


    this is my EnemyAi scrypt
    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. [RequireComponent(typeof(NavMeshAgent))]
    7. public class EnemyAI : MonoBehaviour {
    8.  
    9.    // Use this for initialization
    10.  
    11.   private NavMeshAgent nav;
    12.  
    13.   public float patrolSpeed = 2.0f;
    14.   public float chaseSpeed = 4.5f;
    15.   public float runawaySpeed = 3.5f;
    16.  
    17.  
    18.   public Transform[] waypoints;
    19.  
    20.   private int curWaypoint = 0;
    21.   private int maxWaypoint;
    22.   private float timeSinceLastCall;
    23.   private Transform player;
    24.  
    25.  
    26.   private SphereCollider spherecollider;
    27.  
    28.   private PlayerAttack playerAttack;
    29.   private PlayerHealth playerHealth;
    30.   private GameObject bigPill;
    31.   private PowerUp powerUp;
    32.   private Renderer rend;
    33.  
    34.  
    35.   private EnemyAttack enemyAttack;
    36.  
    37.   public float minWaypointDistance = 0.0f;
    38.  
    39.    void Awake () {
    40.  
    41.   nav = GetComponent<NavMeshAgent>();
    42.     rend = GetComponent<Renderer>();
    43.    
    44.    
    45.     player = GameObject.FindGameObjectWithTag("Player").transform;
    46.     playerHealth = player.GetComponent<PlayerHealth>();
    47.     bigPill = GameObject.FindGameObjectWithTag("BigPill").gameObject;
    48.     powerUp = bigPill.GetComponent<PowerUp>();
    49.     playerAttack = player.GetComponent<PlayerAttack>();
    50.  
    51.  
    52.    
    53.     enemyAttack = GetComponent<EnemyAttack>();
    54.     spherecollider = GetComponent<SphereCollider>();
    55.  
    56.   maxWaypoint = waypoints.Length - 1;
    57.  
    58.    }
    59.  
    60.    // Update is called once per frame
    61.     void Update () {
    62.  
    63.  
    64.   if (enemyAttack.playerInRange == true)
    65.   AttackThePlayer();
    66.   else if(PowerUp.powerUpIsActive == true)
    67.   AvoidThePlayer();
    68.   else
    69.   Patrolling();
    70.  
    71.  
    72.  
    73.     }
    74.  
    75.  
    76.   public void Patrolling()
    77.   {
    78.   nav.speed = patrolSpeed;
    79.  
    80.  
    81.   Vector3 tempLocalPosition = transform.position;
    82.   tempLocalPosition.y = 0f;
    83.  
    84.   Vector3 tempWaypointPosition = waypoints[curWaypoint].position;
    85.   tempWaypointPosition.y = 0f;
    86.  
    87.   if (Vector3.Distance(tempLocalPosition, tempWaypointPosition) <= minWaypointDistance)
    88.   {
    89.   if (curWaypoint == maxWaypoint)
    90.   curWaypoint = 0;
    91.   else
    92.   curWaypoint++;
    93.   }
    94.  
    95.   nav.SetDestination(waypoints[curWaypoint].position);
    96.  
    97.   if (playerHealth.currentHealth <= 0)
    98.   {
    99.   nav.Stop();
    100.   }
    101.  
    102.  
    103.   }
    104.  
    105.   public void AttackThePlayer()
    106.   {
    107.  
    108.   nav.speed = chaseSpeed;
    109.   nav.SetDestination(player.position);
    110.   }
    111.  
    112.   public void AvoidThePlayer()
    113.   {
    114.  
    115.   nav.speed = runawaySpeed;
    116.   rend.material.color = Color.blue;
    117.   nav.SetDestination(transform.position - player.position);
    118.   spherecollider.radius = 1.0f;
    119.  
    120.   }
    121.  
    122.  
    123.   //public IEnumerator AvoidThePlayer(float maxTimeCall)
    124.   //{
    125.   //  if (PowerUp.powerUpIsActive == true)
    126.   //  {
    127.   //  timeSinceLastCall += Time.deltaTime;
    128.   //  Debug.Log(timeSinceLastCall);
    129.   //  nav.speed = runawaySpeed;
    130.   //  rend.material.color = Color.blue;
    131.   //  nav.SetDestination(transform.position - player.position);
    132.   //  spherecollider.radius = 1.0f;
    133.   //  if (timeSinceLastCall >= maxTimeCall)
    134.   //  {
    135.   //  Debug.Log("Termino el counter!");
    136.   //  Patrolling();
    137.   //  timeSinceLastCall = 0;
    138.   //  }
    139.  
    140.   //  }
    141.  
    142.   //  yield return new WaitForEndOfFrame();
    143.   //}
    144.  
    145.  
    146.   }
    147.  
    148.  
    149.  
    my PlayerAttackScipt

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlayerAttack : MonoBehaviour
    6. {
    7.  
    8.   public int attackHit = 100;
    9.   public AudioClip eatAudio;
    10.   public AudioClip powerUpEatAudio;
    11.   public AudioClip enemyDeath;
    12.  
    13.   [HideInInspector]
    14.   public bool isAttackingMode;
    15.   private PowerUp powerUp;
    16.   private GameObject bigPill;
    17.  
    18.  
    19.   AudioSource playerAudio;
    20.  
    21.  
    22.    void Awake ()
    23.    {
    24.     isAttackingMode = false;
    25.     playerAudio = GetComponent<AudioSource>();
    26.  
    27.     bigPill = GameObject.Find("BigPIll");
    28.  
    29.  
    30.   powerUp = bigPill.GetComponent<PowerUp>();
    31.    }
    32.  
    33.  
    34.    void Update () {
    35.  
    36.    
    37.    }
    38.  
    39.   public void Eat()
    40.   {
    41.   playerAudio.clip = eatAudio;
    42.   playerAudio.Play();
    43.   playerAudio.loop = false;
    44.   }
    45.  
    46.   void OnTriggerEnter(Collider other)
    47.   {
    48.  
    49.   if (other.gameObject.tag == "Enemy" && PowerUp.powerUpIsActive == true)
    50.   {
    51.  
    52.   Destroy(other.gameObject);
    53.   }
    54.  
    55.   }
    56. }
    57.  
    58.  
    and my PowerUp Script

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PowerUp : MonoBehaviour
    6. {
    7.  
    8.  
    9.   public static bool powerUpIsActive;
    10.   private bool count;
    11.  
    12.   private float powerUpTimer;
    13.   private float powerUpStop = 5.0f;
    14.   AudioSource powerUpAudio;
    15.   private MeshRenderer meshrenderer;
    16.   private Collider collider;
    17.   private SphereCollider sphereCollider;
    18.  
    19.  
    20.    // Use this for initialization
    21.    void Awake ()
    22.    {
    23.  
    24.     powerUpAudio = GetComponent<AudioSource>();
    25.     meshrenderer = GetComponent<MeshRenderer>();
    26.     collider = GetComponent<Collider>();
    27.     sphereCollider = GetComponent<SphereCollider>();
    28.  
    29.  
    30.    }
    31.  
    32.    // Update is called once per frame
    33.    void Update ()
    34.    {
    35.     Debug.Log(powerUpIsActive +""+  powerUpTimer);
    36.  
    37.      PowerUpCountTimer();
    38.    }
    39.  
    40.   void OnTriggerEnter(Collider other)
    41.   {
    42.   if (other.gameObject.tag == "Player")
    43.   {
    44.  
    45.   count = true;
    46.  
    47.   meshrenderer.enabled = false;
    48.   collider.enabled = false;
    49.   sphereCollider.enabled = false;
    50.   powerUpAudio.Play();
    51.   powerUpAudio.loop = true;
    52.   powerUpIsActive = true;
    53.   //if (powerUpTimer >= powerUpStop)
    54.   //{
    55.   //  meshrenderer.enabled = true;
    56.   //  collider.enabled = true;
    57.   //  sphereCollider.enabled = true;
    58.   //  powerUpAudio.loop = true;
    59.   //  powerUpIsActive = false;
    60.   //  powerUpTimer = 0f;
    61.   //}
    62.   //else
    63.   //  powerUpTimer = 0f;
    64.  
    65.  
    66.   }
    67.   }
    68.  
    69.   public void PowerUpCountTimer()
    70.   {
    71.   if (count == true)
    72.   {
    73.   powerUpTimer += Time.deltaTime;
    74.  
    75.   if (powerUpTimer >= powerUpStop)
    76.   {
    77.  
    78.   powerUpIsActive = false;
    79.   powerUpTimer = 0f;
    80.   powerUpAudio.Stop();
    81.   }
    82.  
    83.   }
    84.   else
    85.   {
    86.   powerUpIsActive = false;
    87.   powerUpTimer = 0f;
    88.   }
    89.   }
    90.  
    91. }
    92.  
    93.  
     
  17. gdossantos87

    gdossantos87

    Joined:
    Mar 18, 2014
    Posts:
    43
    oh and my public static bool powerUpIsActive; does not changes to true, maybe because it is static?
     
  18. gdossantos87

    gdossantos87

    Joined:
    Mar 18, 2014
    Posts:
    43
    the weird thing is that it works with some of the ghost
     
  19. gdossantos87

    gdossantos87

    Joined:
    Mar 18, 2014
    Posts:
    43
    i have uploaded a video to youtube so you guys can have an idea
     
  20. gdossantos87

    gdossantos87

    Joined:
    Mar 18, 2014
    Posts:
    43
    is there a way to getcomponent from a prefab instead from the gameobjects????
     
  21. gdossantos87

    gdossantos87

    Joined:
    Mar 18, 2014
    Posts:
    43
    i have made a change in the powerUp script by changing the UpdateFunction to LateUpdate , and by creating a function called NormalState() where i set back their default values but now only the center bigPill
     
  22. gdossantos87

    gdossantos87

    Joined:
    Mar 18, 2014
    Posts:
    43
    ok new update!!!!!! i manage to set back all their default states and the game works just as i want it to, but it only works with one bigPill i does not work with the others

     
  23. gdossantos87

    gdossantos87

    Joined:
    Mar 18, 2014
    Posts:
    43
    Updated EnemiAI script
    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. [RequireComponent(typeof(NavMeshAgent))]
    7. public class EnemyAI : MonoBehaviour {
    8.  
    9.    // Use this for initialization
    10.  
    11.   private NavMeshAgent nav;
    12.  
    13.   public float patrolSpeed = 2.0f;
    14.   public float chaseSpeed = 4.5f;
    15.   public float runawaySpeed = 3.5f;
    16.    
    17.  
    18.   public Transform[] waypoints;
    19.  
    20.   private int curWaypoint = 0;
    21.   private int maxWaypoint;
    22.   private float timeSinceLastCall;
    23.   private Transform player;
    24.    
    25.    
    26.   private SphereCollider spherecollider;
    27.  
    28.   private PlayerAttack playerAttack;
    29.   private PlayerHealth playerHealth;
    30.   private GameObject bigPill;
    31. //  private PowerUp powerUp;
    32.   private Renderer rend;
    33.   private Color originalColor;
    34.    
    35.    
    36.   private EnemyAttack enemyAttack;
    37.  
    38.   public float minWaypointDistance = 0.0f;
    39.    
    40.    void Awake () {
    41.  
    42.   nav = GetComponent<NavMeshAgent>();
    43.     rend = GetComponent<Renderer>();
    44.  
    45.     originalColor = rend.sharedMaterial.color;
    46.     player = GameObject.FindGameObjectWithTag("Player").transform;
    47.     playerHealth = player.GetComponent<PlayerHealth>();
    48.     bigPill = GameObject.FindGameObjectWithTag("BigPill").gameObject;
    49.     // powerUp = bigPill.GetComponent<PowerUp>();
    50.     playerAttack = player.GetComponent<PlayerAttack>();
    51.  
    52.  
    53.      
    54.     enemyAttack = GetComponent<EnemyAttack>();
    55.     spherecollider = GetComponent<SphereCollider>();
    56.  
    57.   maxWaypoint = waypoints.Length - 1;
    58.    
    59.    }
    60.    
    61.    // Update is called once per frame
    62.     void Update () {
    63.  
    64.   if (enemyAttack.playerInRange == true)
    65.   AttackThePlayer();
    66.   else if (PowerUp.powerUpIsActive == true)
    67.   AvoidThePlayer();
    68.   else
    69.   {
    70.   Patrolling();
    71.   NormalState();
    72.   }
    73.     }
    74.  
    75.   public void NormalState()
    76.   {
    77.   spherecollider.radius = 5.0f;
    78.   nav.speed = patrolSpeed;
    79.   ResetDefaultColor();
    80.   nav.SetDestination(waypoints[curWaypoint].position);
    81.   }
    82.  
    83.  
    84.   private void ChangeColor(Color blue)
    85.   {
    86.   rend.material.color = Color.blue;
    87.   }
    88.  
    89.   void ResetDefaultColor()
    90.   {
    91.   rend.material.color = originalColor;
    92.   }
    93.  
    94.  
    95.   public void Patrolling()
    96.   {
    97.   nav.speed = patrolSpeed;
    98.    
    99.  
    100.   Vector3 tempLocalPosition = transform.position;
    101.   tempLocalPosition.y = 0f;
    102.  
    103.   Vector3 tempWaypointPosition = waypoints[curWaypoint].position;
    104.   tempWaypointPosition.y = 0f;
    105.  
    106.   if (Vector3.Distance(tempLocalPosition, tempWaypointPosition) <= minWaypointDistance)
    107.   {
    108.   if (curWaypoint == maxWaypoint)
    109.   curWaypoint = 0;
    110.   else
    111.   curWaypoint++;
    112.   }
    113.  
    114.   nav.SetDestination(waypoints[curWaypoint].position);
    115.  
    116.   if (playerHealth.currentHealth <= 0)
    117.   {
    118.   nav.Stop();
    119.   }
    120.    
    121.  
    122.   }
    123.  
    124.   public void AttackThePlayer()
    125.   {
    126.  
    127.   nav.speed = chaseSpeed;
    128.   nav.SetDestination(player.position);
    129.   }
    130.  
    131.   public void AvoidThePlayer()
    132.   {
    133.  
    134.   nav.speed = runawaySpeed;
    135.   //rend.material.color = Color.blue;
    136.   ChangeColor(Color.blue);
    137.   nav.SetDestination(transform.position - player.position);
    138.   spherecollider.radius = 1.0f;
    139.    
    140.    
    141.    
    142.  
    143.   }
    144.  
    145.  
    146.   //public IEnumerator AvoidThePlayer(float maxTimeCall)
    147.   //{
    148.   //  if (PowerUp.powerUpIsActive == true)
    149.   //  {
    150.   //  timeSinceLastCall += Time.deltaTime;
    151.   //  Debug.Log(timeSinceLastCall);
    152.   //  nav.speed = runawaySpeed;
    153.   //  rend.material.color = Color.blue;
    154.   //  nav.SetDestination(transform.position - player.position);
    155.   //  spherecollider.radius = 1.0f;
    156.   //  if (timeSinceLastCall >= maxTimeCall)
    157.   //  {
    158.   //  Debug.Log("Termino el counter!");
    159.   //  Patrolling();
    160.   //  timeSinceLastCall = 0;
    161.   //  }
    162.  
    163.   //  }
    164.  
    165.   //  yield return new WaitForEndOfFrame();
    166.   //}
    167.  
    168.    
    169.  
    170.    
    171.    
    172.    
    173.  
    174.    
    175.   }
    176.  
    177.  
    178.    
    179.  
    180.  
    181.    
    182.  
    183.  
     
  24. gdossantos87

    gdossantos87

    Joined:
    Mar 18, 2014
    Posts:
    43
    HELP PLZZ!!!
     
  25. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    A quick glance at your enemy code shows that you're only looking for one bigPill... A closer look at the rest of your scripts shows that they, too, are only looking for a single object. If you want to find all of the bigPills in the scene, you'll want to use FindGameObjectsWithTag (which will locate an array of objects) instead of FindGameObjectWithTag (which only returns the first one it finds).
     
    Last edited: May 24, 2015
  26. gdossantos87

    gdossantos87

    Joined:
    Mar 18, 2014
    Posts:
    43
    thx for your time Krougeau, how should i modify the code???
     
  27. gdossantos87

    gdossantos87

    Joined:
    Mar 18, 2014
    Posts:
    43
    in my enemiAi script?
     
  28. gdossantos87

    gdossantos87

    Joined:
    Mar 18, 2014
    Posts:
    43
    im not using these reference bigPill = GameObject.FindGameObjectWithTag("BigPill").gameObject;
     
  29. gdossantos87

    gdossantos87

    Joined:
    Mar 18, 2014
    Posts:
    43
    im activating it using a static variable public static bool powerUpIsActive;
     
  30. gdossantos87

    gdossantos87

    Joined:
    Mar 18, 2014
    Posts:
    43
    i think i found mi error
    Code (csharp):
    1.  
    2.   private void PowerUpCountTimer()
    3.   {
    4.   if (count == true)
    5.   {
    6.   powerUpIsActive = true;
    7.   powerUpTimer += Time.deltaTime;
    8.    
    9.   if (powerUpTimer >= powerUpStop)
    10.   {
    11.  
    12.   powerUpTimer = 0f;
    13.   powerUpAudio.Stop();
    14.   powerUpIsActive = false;
    15.   count = false;
    16.   }
    17.    
    18.   }
    19.   else
    20.   {
    21.   //powerUpIsActive = false;
    22.   powerUpTimer = 0f;
    23.   count = false;
    24.  
    25.   }
    26.   }
    27.  
     
  31. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Terrific. So it's working correctly for you now?
     
  32. gdossantos87

    gdossantos87

    Joined:
    Mar 18, 2014
    Posts:
    43
    yes krougeau it works perfect, now i have a new problem, now it only works when the player is out of range from the enemy when it is at range, and i pick up a bigpill it does not work from that specific enemy

     
  33. gdossantos87

    gdossantos87

    Joined:
    Mar 18, 2014
    Posts:
    43
    i can destroy it but it this functions does not work
    Code (csharp):
    1.  
    2.  public void AvoidThePlayer()
    3.   {
    4.  
    5.   nav.speed = runawaySpeed;
    6.   ChangeColor(Color.blue);
    7.   nav.SetDestination(transform.position - player.position);
    8.   spherecollider.radius = 1.0f;
    9.  
    10.  
    11.   }
    12.  
     
  34. gdossantos87

    gdossantos87

    Joined:
    Mar 18, 2014
    Posts:
    43
    thats from the EnemiAi script
    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. [RequireComponent(typeof(NavMeshAgent))]
    7. public class EnemyAI : MonoBehaviour {
    8.  
    9.    // Use this for initialization
    10.  
    11.   private NavMeshAgent nav;
    12.  
    13.   public float patrolSpeed = 2.0f;
    14.   public float chaseSpeed = 4.5f;
    15.   public float runawaySpeed = 3.5f;
    16.    
    17.    
    18.  
    19.   public Transform[] waypoints;
    20.  
    21.   private int curWaypoint = 0;
    22.   private int maxWaypoint;
    23.   private float timeSinceLastCall;
    24.   private Transform player;
    25.    
    26.    
    27.   private SphereCollider spherecollider;
    28.  
    29.   private PlayerAttack playerAttack;
    30.   private PlayerHealth playerHealth;
    31.    
    32.  
    33.   private Renderer rend;
    34.   private Color originalColor;
    35.    
    36.    
    37.   private EnemyAttack enemyAttack;
    38.  
    39.   public float minWaypointDistance = 0.0f;
    40.    
    41.    void Awake () {
    42.  
    43.   nav = GetComponent<NavMeshAgent>();
    44.     rend = GetComponent<Renderer>();
    45.  
    46.     originalColor = rend.sharedMaterial.color;
    47.     player = GameObject.FindGameObjectWithTag("Player").transform;
    48.     playerHealth = player.GetComponent<PlayerHealth>();
    49.  
    50.      
    51.     playerAttack = player.GetComponent<PlayerAttack>();
    52.  
    53.  
    54.      
    55.     enemyAttack = GetComponent<EnemyAttack>();
    56.     spherecollider = GetComponent<SphereCollider>();
    57.  
    58.   maxWaypoint = waypoints.Length - 1;
    59.    
    60.    }
    61.    
    62.    // Update is called once per frame
    63.     void Update () {
    64.  
    65.   if (enemyAttack.playerInRange == true)
    66.   AttackThePlayer();
    67.   else if (PowerUp.powerUpIsActive == true)
    68.   AvoidThePlayer();
    69.   else
    70.  
    71.   {
    72.   Patrolling();
    73.   NormalState();
    74.   }
    75.     }
    76.  
    77.   public void NormalState()
    78.   {
    79.   spherecollider.radius = 6.0f;
    80.   nav.speed = patrolSpeed;
    81.   ResetDefaultColor();
    82.   nav.SetDestination(waypoints[curWaypoint].position);
    83.   }
    84.  
    85.  
    86.   private void ChangeColor(Color blue)
    87.   {
    88.   rend.material.color = Color.blue;
    89.   }
    90.  
    91.   void ResetDefaultColor()
    92.   {
    93.   rend.material.color = originalColor;
    94.   }
    95.  
    96.  
    97.   public void Patrolling()
    98.   {
    99.   nav.speed = patrolSpeed;
    100.    
    101.  
    102.   Vector3 tempLocalPosition = transform.position;
    103.   tempLocalPosition.y = 0f;
    104.  
    105.   Vector3 tempWaypointPosition = waypoints[curWaypoint].position;
    106.   tempWaypointPosition.y = 0f;
    107.  
    108.   if (Vector3.Distance(tempLocalPosition, tempWaypointPosition) <= minWaypointDistance)
    109.   {
    110.   if (curWaypoint == maxWaypoint)
    111.   curWaypoint = 0;
    112.   else
    113.   curWaypoint++;
    114.   }
    115.  
    116.   nav.SetDestination(waypoints[curWaypoint].position);
    117.  
    118.   if (playerHealth.currentHealth <= 0)
    119.   {
    120.   nav.Stop();
    121.   }
    122.    
    123.  
    124.   }
    125.  
    126.   public void AttackThePlayer()
    127.   {
    128.   nav.speed = chaseSpeed;
    129.   nav.SetDestination(player.position);
    130.   }
    131.  
    132.   public void AvoidThePlayer()
    133.   {
    134.  
    135.   nav.speed = runawaySpeed;
    136.   ChangeColor(Color.blue);
    137.   nav.SetDestination(transform.position - player.position);
    138.   spherecollider.radius = 1.0f;
    139.  
    140.  
    141.   }
    142.  
    143.  
    144.   //public IEnumerator AvoidThePlayer(float maxTimeCall)
    145.   //{
    146.   //  if (PowerUp.powerUpIsActive == true)
    147.   //  {
    148.   //  timeSinceLastCall += Time.deltaTime;
    149.   //  Debug.Log(timeSinceLastCall);
    150.   //  nav.speed = runawaySpeed;
    151.   //  rend.material.color = Color.blue;
    152.   //  nav.SetDestination(transform.position - player.position);
    153.   //  spherecollider.radius = 1.0f;
    154.   //  if (timeSinceLastCall >= maxTimeCall)
    155.   //  {
    156.   //  Debug.Log("Termino el counter!");
    157.   //  Patrolling();
    158.   //  timeSinceLastCall = 0;
    159.   //  }
    160.  
    161.   //  }
    162.  
    163.   //  yield return new WaitForEndOfFrame();
    164.   //}
    165.  
    166.    
    167.  
    168.    
    169.    
    170.    
    171.  
    172.    
    173.   }
    174.  
    175.  
    176.    
    177.  
    178.  
    179.    
    180.  
    181.  
     
  35. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    You're not making a lot of sense to me, and that may be a failing on my part... It's been a long day. You're saying that it doesn't work if the player is too far away from the enemy when the player collects the "big pill"? Perhaps you could try creating an Event that is set off when the player collects any of the "big pill" objects, and add Event Listeners to all of your enemies so that they will all get the message when any given "big pill" item is collected.
     
  36. gdossantos87

    gdossantos87

    Joined:
    Mar 18, 2014
    Posts:
    43
    here is a video explaining what i am trying to say, thank you very much krougerau, creating an event might work

     
  37. gdossantos87

    gdossantos87

    Joined:
    Mar 18, 2014
    Posts:
    43
    when the player is to close to the enemy "Inside the trigger of the enemy"
     
  38. gdossantos87

    gdossantos87

    Joined:
    Mar 18, 2014
    Posts:
    43
    it still work, but the enemy does not runaway from the player and it does not changes color to blue
     
  39. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    In your Update function try changing
    Code (CSharp):
    1. if (enemyAttack.playerInRange == true)
    to
    Code (CSharp):
    1. if (enemyAttack.playerInRange == true && PowerUp.powerUpIsActive == false)
    and see if that helps the situation any.

    Basically, your current code says to be in attack mode if the player is in range, regardless of whether or not a power pill has been used. This should fix that so it will only be in attack mode if the player is in range and there are no power-up items active. Hope it helps!
     
  40. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
  41. gdossantos87

    gdossantos87

    Joined:
    Mar 18, 2014
    Posts:
    43
    OMG thats so cool!!!! ,, oh btw it worked!!!! thank you very much krougeau
     
    krougeau likes this.
  42. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    456
    Awesome! So glad it worked out :) Have fun!!
     
    gdossantos87 likes this.