I’ve got a spawning script set up to start spawning enemies when the player enters certain radius around it (a Sphere collider). I used one of the default unity character controllers (Third Person Character Controller specifically) just so I could have a moving character to test with. The problem is, when I make an object without the character controller and it’s accompanying script the OnTriggerEnter of the spawner doesn’t work. I didn’t reference the ThirdPersonCharacter Controller itself or it’s script in my spawner script, I just search for a collider attached to an object tagged player, which the new object it.
Here’s the spawner’s code.
public GameObject[] spawnObject;
public GameObject[] spawnPosition;
public float radius = 1.0f;
public float minSpawnTime = 1.0f;
public float maxSpawnTime = 10.0f;
public bool constantSpawn = false;
public float SpawnCount = 0f;
public float LookAtDistance = 10.0f;
public float Distance;
public Transform Target;
public float Damping = 5.0f;
private Quaternion rotation;
public bool deathspawn = true;
public bool TriggerActivate = false;
void Start()
{
Target = GameObject.FindGameObjectWithTag ("Player").transform;
if (TriggerActivate == false)
{
Invoke ("SpawnEnemy", 45);
}
}
void Update(){
Distance = Vector3.Distance (Target.position + Target.transform.forward, transform.position);
if (Distance < LookAtDistance)
{
LookAt();
}
}
void OnTriggerEnter(Collider Player)
{
if (TriggerActivate == true) {
Invoke ("SpawnEnemy", Random.Range (minSpawnTime, maxSpawnTime));
}
}
void OnTriggerExit(Collider Player)
{
CancelInvoke ("SpawnEnemy");
deathspawn = false;
}
void SpawnEnemy ()
{
int spawnObjectIndex = Random.Range(0,spawnObject.Length);
int spawnPositionIndex = Random.Range (0, spawnPosition.Length);
Instantiate(spawnObject[spawnObjectIndex],spawnPosition[spawnPositionIndex].transform.position,spawnPosition[spawnPositionIndex].transform.rotation);
SpawnCount += 1f;
if (constantSpawn == true && SpawnCount <= 5)
{
Invoke ("SpawnEnemy", Random.Range (minSpawnTime,maxSpawnTime));
}
}
void LookAt(){
rotation = Quaternion.LookRotation (Target.localPosition - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
}
public void SpawnDown (float reduction){
SpawnCount -= reduction;
if (deathspawn == true) {
Invoke ("SpawnEnemy", Random.Range (minSpawnTime, maxSpawnTime));
}
}
}
Secondary info, the spawner will not even look at the player as per the LookAt function.
I have an enemy in the scene who will look at and follow this player, but will not parent when it enters the players collider (Something the enemy is supposed to do).
Just a clarification, TriggerActivate is set to true for the spawner, both the spawner object and the player have their colliders set to Is Trigger, the player is within the Spawners radius as soon as I press play (this worked with the ThirdPersonCharacter controller form of this test) and the player is tagged as “Player”.
Hmm, didn't know that about the OnTrigger methods. Now that you point it out, every time I've used it I think the object it was attached to or the object that was triggering it had a rigid body, I just didn't really think about it. That fixed the problem so thank you very much. Also, good to know that for later.
– Chappy