Slowing down enemies - and making them faster again
Hey guys, i'm currently working on my very first project in Unity and I'm creating a 3D TowerDefense Game.
I've built in a tower that instantiates a bullet, which then instantiates an ice patch on the ground if it collides with a monster.
The ice patch has a sphere collider and only lasts 3 seconds before it gets destroyed.
I'm using OnEnter and OnTriggerExit to adjust the speed component of the NavMeshAgent of the monsters that are walking in or out.
However, if there are enemies within the collider at the time of destruction, they continue being slow.
I am currently trying to safe monsters in an ArrayList, which adds them OnTriggerEnter and removes them OnTriggerExit, I'm using OnDestroy function to iterate over the list with monsters that should've entered the collider but not existed yet and I'm setting up their speed.
using UnityEngine;
using System.Collections;
public class IcePatch : MonoBehaviour
{
// Speed
public float slowStrength = 2f;
private NavMeshAgent agent;
private ArrayList monsterSafe;
void OnTriggerEnter(Collider co)
{
if (co.name.Contains("Monster"))
{
agent = co.GetComponent<NavMeshAgent>();
agent.speed -= slowStrength;
monsterSafe.Add(co);
}
}
void OnTriggerExit(Collider co)
{
if (co.name.Contains("Monster"))
{
agent = co.GetComponent<NavMeshAgent>();
agent.speed += slowStrength;
monsterSafe.Remove(co);
}
}
void OnDestroy()
{
foreach (GameObject element in monsterSafe)
{
agent = element.GetComponent<NavMeshAgent>();
agent.speed += slowStrength;
}
}
}
Enemies are not getting fast again though :/ Any ideas?
Your answer
Follow this Question
Related Questions
How to place a sprite on the face of a collider 0 Answers
Per Frame BoxColliders? 0 Answers
Setting OnTriggerEnter to a second object collider 2 Answers
Unity C# - Target a Game Object (with Tag) on a Specific Vector2 Location 1 Answer
Why is my trigger collider acting like a normal collider? 1 Answer