- Home /
Spawn Point
Hi guys, I have this spawn script that works fine, but I would like to add an OnTriggerEnter function where enemies only spawn when the player gets close to the spawnpoint. Can anyone help me do this? I would not be asking if i knew how to do it, but I'm getting desperate as it is the only thing not working properly. Cheers!
public class Spawn : MonoBehaviour { public GameObject zombie; public float delayTime = 4f;
IEnumerator Start()
{
var obj = Instantiate(zombie, transform.position, transform.rotation) as GameObject;
yield return new WaitForSeconds(delayTime);
StartCoroutine(Start());
}
}
Answer by jakovd · Apr 23, 2013 at 01:09 PM
Put a collider component on an object that has this script, check IsTrigger checkbox on the component and put a tag "Player" on your players game object (in Inspector window, just above the Transform values).
public GameObject zombie;
public void OnTriggerEnter(Collider other)
{
if ((other.tag == "Player")) //checks if it is Player that entered this trigger
{
var obj = Instantiate(zombie, transform.position, transform.rotation) as GameObject;
}
}
Answer by TonyLi · Apr 23, 2013 at 01:15 PM
Here's one solution:
Set the player's tag to "Player".
Add a SphereCollider to your spawnpoint. Set the radius to the maximum distance at which it should detect the player. Check "Is Trigger".
Change Spawn.cs to:
public class Spawn : MonoBehaviour {
public GameObject zombie; public float delayTime = 4f; private const string PlayerTag = "Player"; private bool isPlayerNearby = false; void Start() { StartCoroutine(Spawn()); } IEnumerator Spawn() { while (true) { if (isPlayerNearby) Instantiate(zombie, transform.position, transform.rotation) as GameObject; yield return new WaitForSeconds(delayTime); } } void OnTriggerEnter(Collider other) { if (string.Equals(other.tag, PlayerTag)) isPlayerNearby = true; } void OnTriggerExit(Collider other) { if (string.Equals(other.tag, PlayerTag)) isPlayerNearby = false; } }
i don't why but something's gone wrong. i tried attaching the script to another spawnpoint and i got these two errors:
error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement
and
error CS0542: `Spawn.Spawn()': member names cannot be the same as their enclosing type
please help!
your script (class) is named Spawn and you have a method named the same in it. The method of a class with a same name is a special kind of method (constructor) thus you should avoid using the same name. Rename that Spawn method into something like SpawnEnemy. Remember to also change that name in StartCoroutine call in your Start method.
@jakovd: StartCoroutine call is fine the way he has it, beside the name "Spawn". Using the string version should actually be avoided because the compiler can't help you when you mistype something or when you try to figure out who calls which functions.
Ok. Thanks.. $$anonymous$$y design choice would be to call for spawing with InvokeRepeating in OnTriggerEnter and end it with CancelInvoke in OnTriggerExit. Seems simpler to understand than this infinite while loop and WaitForSeconds. $$anonymous$$y two cents.
I agree -- Unless you plan to do additional processing in the SpawnEnemy() loop, jakovd's suggestion is more elegant.