Other
Respawing Enemy After GameObject is Destroyed
At the moment, I have a simple script that destroys the enemy when it collides with my weapon:
using UnityEngine;
using System.Collections;
public class killenemy : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "enemy")
{
Destroy (other.gameObject);
}
}
}
How can I change this so that it respawns the enemy after 3 seconds at the same starting location with the same movement script attached that follows a set of waypoints around the map? I fear that destroying the gameobject itself will get rid of the movement script completely. Do I need a 'spawner' or can I do each enemy individually? I'll only have around 3 enemies in total.
Movement Script:
#pragma strict
var waypoint : Transform[];
var patrolSpeed : float = 3;
var loop : boolean = true;
var dampingLook = 6.0;
var pauseDuration : float = 0;
private var curTime : float;
private var currentWaypoint : int = 0;
private var character : CharacterController;
function Start(){
character = GetComponent(CharacterController);
}
function Update(){
if(currentWaypoint < waypoint.length){
patrol();
}else{
if(loop){
currentWaypoint=0;
}
}
}
function patrol(){
var target : Vector3 = waypoint[currentWaypoint].position;
var moveDirection : Vector3 = target - transform.position;
if(moveDirection.magnitude < 0.5){
if (curTime == 0)
curTime = Time.time;
if ((Time.time - curTime) >= pauseDuration){
currentWaypoint++;
curTime = 0;
}
}else{
var rotation = Quaternion.LookRotation(target - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * dampingLook);
character.Move(moveDirection.normalized * patrolSpeed * Time.deltaTime);
}
}
Answer by Laseph · Apr 14, 2017 at 09:45 AM
If you are using a prefab to make your enemy then it should be no problem because it always has the components you attached to it. For the spawning of enemy's I would indeed make a spawner gameobject with a script. You would need to have an event for when the enemy has died, the spawner could see it and know that there's no longer three enemy's in the playing field. Then you would have a timer of three seconds before you instantiate the new enemy.
I would personally not call destroy directly and instead would have a Die method in the enemy class in which you can destroy it and do other things (Much more flexible, this would also be the method where you would call the event)
If you don't know about events in c# you should take a look because they are really useful (you could also take a look at UnityEvents, it's practically the same thing and it also works with the inspector)
If you need me to elaborate just say so.
I don't have a prefab for any enemy. I just have the one enemy so far to test on and I just placed it in the location I want it to start from as it plays the movement script when the game starts and moves around the points I set so the 3 enemies (when I drop them in) will already be present when the game starts, I didn't think I needed to spawn them right off the bat if they are already in the position I set.
I don't know what Die methods are or events. I'm already way in over my head with this, I only posted the question as reading the manuals don't make anything clearer. I'm trying to do one thing at a time but everything's so confusing and now I'm on a time limit.
Answer by Cuttlas-U · Apr 14, 2017 at 10:55 AM
hi; u can create a function in the enemy script that change its position to where u like;
for example public void ResetPosition();
then u don't destroy it just call this function to reset the position and other changes that should be done to the enemy u do in that function;
other.gameObject.GetComponent(Movement ).ResetPosition();
/////////////////////////////////////////////
////////////// the better idea is not to destroy it at the first place; 1 good thing and optimized way is to save the objects that u don't want any more in a pool system; then u can get back any thing u want again from the pool;
u can search for pooling system there are good tutorials about it in the youtube;
but if u want to do it simple u can just dissactive it and not destroy by using :
other.gameObject.SetActive(false);
this way u don't need to do any thing ; also u need a manager to holde all of your enemy's in an array at the start of the game;
then u send a request to the manager script and ask for an enemy object; the manager script look through the array if any enemy object is dissactive and activate it for u ; /////////////////
Follow this Question
Related Questions
fpscontroller doesnt respawn (maybe transform.position issue?) 0 Answers
When I activate the animator of the enemy and play it spawns at the corner of the map and don't move 0 Answers
Making an enemy go back to its spawn after losing target 0 Answers
How to make all enemies attack after spawning? Not only 1. 1 Answer