- Home /
Spawning enemys by collision
Hello guys,
I want to make i quick enemy script for my game, my ideia is to instantiate the enemy whenever my character is in contact with a floor that got the "spawn" tag on it.
How can i do this?
Thank you.
Answer by Headworker · Jan 29, 2014 at 01:52 PM
Hello. That should be quite simple:
Just add the following script to the player:
public GameObject EnemyToSpawn; // To assign the enemy in the inspector
void OnCollisionEnter(Collision collision)
{
if (collision.collider.tag == "spawn")
{
GameObject newEnemy = Instataniate(EnemyToSpawn);
Vector3 spawnPoint = collision.contacts[0].point;
// add offset for enemy size;
SpawnPoint.y += 0.5f; // example
newEnemy.transform.position = spawnPoint;
}
}
One more thing: if you want the enemys to spawn continously while the player is in contact with the floor, just ask!
Thank you very much HeadWorker, that'll be usefull (:
Just for curiosity, how this script would be if i want the enemies to spawn continously in contact with the floor?
You would use OnCollisionStay()
This function however would spawn the enemies every frame, which would quickly bog down your computer.
To make it work, just couple it with a coroutine and an on/off bool. If you interested in a detailed solution just ask :)
Your answer
Follow this Question
Related Questions
Safe area from enemies 1 Answer
How do I go about adding basic pathfinding to my enemy? 1 Answer
Problem with enemy collision with the player(script in description) 1 Answer
Enemy not jumping using Raycast 1 Answer
Kill a Enemy - Answer 2 Answers