- Home /
Instantiating objects to trail player
Hello everyone,
I've been putting off posting a question here until I really have to, but this problem has got me stumped.
I want to instantiate objects at the player's location but with a delay of a second or two, set by a public variable. They are currently instantiated and are destroyed a few seconds later, thus forcing the player to keep moving or be caught up. These objects will not move or follow the player directly as they are spikes coming out of the ground. I'll deal with the upward movement of the spikes later, right now I just want to get them instantiating at the right time.
I've got the objects instantiating at the right place but they appear immediately under/on/in the player. I'm trying to use 'yield' but it only seems to set the delay before the first object is instantiated or the delay between each instantiated object.
Ideally I would like the game to begin, the player to have a timed head start (a public float variable), then the first object is instantiated where the player was. Then wait for a period of time (another public float variable) before instantiating another object but again this will be at the player's location immediately after the first location.
The end result will be a wave of spikes trailing the player.
Here's the code I've come up with so far...
using UnityEngine;
using System.Collections;
public class GameController2 : MonoBehaviour
{
public GameObject hazard;
public int spikeCount;
public float spikeWait;
public float chaseWait;
private GameObject playerTarget;
private Vector3 targetPos;
void Start()
{
StartCoroutine (SpawnWaves ());
}
void Update()
{
playerTarget = GameObject.FindGameObjectWithTag("Player");
targetPos = playerTarget.transform.position;
}
IEnumerator SpawnWaves()
{
while(true)
{
yield return new WaitForSeconds (chaseWait);
for(int i = 0; i < spikeCount; i++)
{
Instantiate (hazard, targetPos, Quaternion.identity);
yield return new WaitForSeconds (spikeWait);
}
}
}
}
Any help or suggestions where to look would really be appreciated.
It seems to work fine for me...
IEnumerator SpawnWaves()
{
yield return new WaitForSeconds (chaseWait);
int i = 0;
while(i < spikeCount)
{
i ++;
Instantiate (hazard, targetPos, Quaternion.identity);
yield return new WaitForSeconds (spikeWait);
}
}
Thanks for the quick response. Unfortunately this does what my current code does. So as the player is running the objects are appearing immediately under foot. Imagine the player running passed a row of houses. As she passes door number 6 the spikes are spawning outside door number 2. As she is passing door number 10, the spikes are spawning outside number 6 and so on. That's what I'm ai$$anonymous$$g for. The chaseWait yield delays the start of instantiation but as soon as it begins the objects appear exactly at the player's current location.
Fantastic! Works like a charm. Can you post this as an answer so I can give you your well deserved karma. =) Just FYI, line 8 is just "Vector3" not "new Vector3". ;-) Thanks again, you have saved me ripping out my last few remaining hairs.
Answer by Invertex · Jan 26, 2014 at 06:31 AM
Alright, I understand your problem better now, it wasn't so much an issue with the delay, but the position it appeared after the delay.
In that case, it's as simple as creating a temporary Vector3 to store the position before the delay.
IEnumerator SpawnWaves()
{
yield return new WaitForSeconds (chaseWait);
int i = 0;
while(i < spikeCount)
{
i ++;
Vector3 prevPos = targetPos;
yield return new WaitForSeconds (spikeWait);
Instantiate (hazard, prevPos, Quaternion.identity);
}
}
Answer by robertbu · Jan 26, 2014 at 04:56 AM
If you move line 21 from inside Update() to line 28 (just before the yield), you shoudl fix your problem. It would be a good programming practice to move the declairation of 'targetPos' to 26 to keep it local to SpawnWaves().
Thanks for replying. I'm getting a NullReferenceException when I put the targetPos declaration in either position you suggest. Interestingly, the code compiles and enters Game $$anonymous$$ode without a problem, but no spikes spawn at all and the NullReference pops up in the console while in Game $$anonymous$$ode. Do I need to modify any other part of my code to make it work as you suggest?
As I look closer, you need to move line 20 as well. Assu$$anonymous$$g there is only one object tagged player, and that object does not change during the game, then line 20 should be moved into the Start() function. If it can change, then it should be moved into your SpawnWaves() function.
Your answer
Follow this Question
Related Questions
Spawn and set as child 2 Answers
Police cars driving on top of each other 2 Answers
Checking if object intersects? 1 Answer
Instantiating an object by collecting a game object 0 Answers
Instantiate a Prefab as a Child? 5 Answers