Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by _Prism_ · Jan 26, 2014 at 04:19 AM · instantiatefollow player

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.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Invertex · Jan 26, 2014 at 04:55 AM 0
Share

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);
         }
 }
avatar image _Prism_ · Jan 26, 2014 at 05:51 AM 0
Share

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.

avatar image _Prism_ · Jan 26, 2014 at 06:54 AM 0
Share

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.

avatar image Invertex · Jan 26, 2014 at 06:57 AM 0
Share

Great to hear, converted :)

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
 
        }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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().

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image _Prism_ · Jan 26, 2014 at 06:01 AM 0
Share

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?

avatar image robertbu · Jan 26, 2014 at 07:10 AM 0
Share

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.

avatar image _Prism_ · Jan 26, 2014 at 07:18 AM 0
Share

All good, problem solved. Thanks again for replying.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges