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);
}
}