Moving Object that goes to it’s destination, destroys itself, and instantiates again doing the same thing
So I have an above average (ok slightly above average) of scripting in Unity. I’m just trying to wrap my head around this concept and I just can’t seem to do it for some reason. So I know how to write the code so that one moving platform goes to it’s waypoint and then destroys itself, or at least this is basically how I would do it explained below.
First I’d attach the script to the object I want to move. Then I would have an empty game object parented beneath it. I would use this object to hold the position coordinates of where ever I want the platform to translate in space. Using some fancy math I would have a variable that sets the time of how long it takes the platform to get to that location, then I would also use that same variable in a destroy function so that it destroys itself when it reaches it’s target location.
I just have no idea how to implement this when the platform destroys itself instantiating another one that does the same thing in an infinite loop. I have something similar happening with another game mechanic where barrels roll and keep spawning after the last one is destroyed. Expect for that I don’t have to set a waypoint which I found I can’t do with prefabs exactly or I just don’t know how.
I hope this makes sense I’m kind of blabbering. Below are the scripts I used for the barrels, one goes on the barrel prefab itself and the other spawns the barrel. I tried to use the same logic I used with them but to no avail. Is there anyway to implement the above?
Barrel Script Below, remember I am not asking about these scripts specifically I just tried to use them as reference to use the same logic on my proposed plan above but I can’t exactly get it to work.
var barrelDamage : int = 2; //Integer value of the damage barrel does to broc if OnCollisionEnter function intitiates
var autoDesTime : float = 5; //Time to destroy barrel if it does not hit broc
var hitPoints: float = 4.0;
var explodeParticle: GameObject;
//To auto destroy barrel after "x" seconds
function Start () {
Destroy(gameObject, autoDesTime); //Destroy object in "x" seconds after not hitting Broc
}
//Test for collision with Broc only
function OnCollisionEnter (collision : Collision) {
if(collision.gameObject.tag == "Player") { //Broc needs to have the Player tag
Destroy(gameObject); //Destroy game object after colliding with Broc
Instantiate(explodeParticle, transform.position, Quaternion.Euler(-90,0,0)); //Instantiate particle explosion
Destroy(explodeParticle, .25);
collision.collider.SendMessageUpwards("ApplyDamage", barrelDamage, SendMessageOptions.DontRequireReceiver); //Sends damage to Broc HP script
}
}
function ApplyDamage(damage : float){
Debug.Log("Barrel just took " + damage + " damage!");
if(hitPoints <= 0.0)
return;
hitPoints -= damage;
if (hitPoints <= 0.0) {
Destroy(gameObject);
Instantiate(explodeParticle, transform.position, Quaternion.Euler(-90,0,0)); //Instantiate particle explosion
Destroy(explodeParticle, .25);
}
}
Barrel Spawn Point Below
var spawnTime : float; //Time between Spawns
private var timeSinceSpawn : float; //Holds time since last spawn
var barrel : GameObject; //Object to be instantiated
//Spawn one barrel at the start
function Start () {
Instantiate(barrel, transform.position, transform.rotation);
}
//Function that spawns a new barrel after spawnTime is eclisped
function Update () {
timeSinceSpawn += Time.deltaTime;
if(timeSinceSpawn >= spawnTime) { //Checks every fram to see if spawnTime is more than time since last spawn
// spawn another barrel
Instantiate(barrel, transform.position, transform.rotation);
timeSinceSpawn = 0.0; //resets spawn counter
}
}