Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 beefyt123 · Aug 04, 2015 at 04:32 AM · prefabspawn

Spawn prefab only while Player is moving

I am trying to spawn prefabs into my game. But I only want to call the spawn function while my player is actually moving. Below is the code that I am trying to use and when I run the game I get no errors. It seems that the Spawn function just is never getting called.

 using UnityEngine;
 using System.Collections;
 
 public class SpawnTesting : MonoBehaviour {
     public GameObject[] obj;
     public float spawnMin = 1f;
     public float spawnMax = 2f;
     public float yPos;
     public Rigidbody2D rb;
     
     // Get Rigidbody component of Player
     void Start(){
         if (gameObject.tag == "Player") {
             rb = GetComponent<Rigidbody2D> ();
         }
     //Check if player is moving
         if (rb.velocity.magnitude > 0) {
             Spawn ();
         }
     }
 //Spawn prefab
     void Spawn(){
             Vector2 newPosition = new Vector2 (this.transform.position.x, yPos);
             Instantiate (obj [Random.Range (0, obj.GetLength (0))], newPosition, Quaternion.identity);
             Invoke ("Spawn", Random.Range (spawnMin, spawnMax));
     }
     
 }
 


I can get the code working as soon as the game starts but I only want the spawns to happen when my player is actually moving.

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by NeverHopeless · Aug 04, 2015 at 06:48 AM

Try using FixedUpdate instead of Start:

 void FixedUpdate()
 {
      //Check if player is moving
      if (rb.velocity.magnitude > 0) {
              Spawn ();
      }
 }

If you want to limit too much occurances of this spawn you can create a float value called coolDownTimer and let it stop spawnning for desired time.

Hope it helps!

EDIT:

Discard the FixedUpdate approach I didn't noticed the spawn method implementation.

Try like this:

 void Spawn(){
 
     if (rb.velocity.magnitude > 0) {     
         Vector2 newPosition = new Vector2 (this.transform.position.x, yPos);
         Instantiate (obj [Random.Range (0, obj.GetLength (0))], newPosition, Quaternion.identity);
     }
 
     Invoke ("Spawn", Random.Range (spawnMin, spawnMax));
 }



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 beefyt123 · Aug 04, 2015 at 09:06 AM

Thanks for the help. I implemented your code and now nothing spawns into my game until my player starts moving along the x-axis which is what I wanted. But now the problem is that it will spawn in the prefabs with every FixedUpdate which is every 0.2 seconds and ignores the spawnMin and spawnMax values causing Unity to crash because it is now using too much resource.

Comment
Add comment · Show 2 · 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 NeverHopeless · Aug 04, 2015 at 09:23 AM 0
Share

Try my edited answer please.

avatar image beefyt123 · Aug 04, 2015 at 11:11 AM 0
Share

Thank you. That solution is what I was looking for. I attached a copy of my final code that is working for me in case anyone else who is looking for a similar solution

 using UnityEngine;
 using System.Collections;
 
 public class SpawnScript : $$anonymous$$onoBehaviour {
     public GameObject[] obj;
     public float spawn$$anonymous$$in = 1f;
     public float spawn$$anonymous$$ax = 2f;
     public float yPos;
     public Rigidbody2D rb;
 
     void Start(){
         Spawn ();
     }
 
     void Spawn(){
         
         if (rb.velocity.magnitude > 1) {     
             Vector2 newPosition = new Vector2 (this.transform.position.x, yPos);
             Instantiate (obj [Random.Range (0, obj.GetLength (0))], newPosition, Quaternion.identity);
         }
         Invoke ("Spawn", Random.Range (spawn$$anonymous$$in, spawn$$anonymous$$ax));
 
     }
 }

avatar image
0

Answer by Ibzy · Aug 04, 2015 at 11:31 AM

I see you already have a solution, but not entirely sure it's exactly what you are after? What happens/should happen when the player stops moving, and then when they start moving again?

I feel it should be more like:

 void Update(){
     if (rb.velocity.magnitude > 1)
         Spawn();
 }

 void Spawn(){
     Vector2 newPosition = new Vector2 (this.transform.position.x, yPos);
     Instantiate (obj [Random.Range (0, obj.GetLength (0))], newPosition, Quaternion.identity);
     Invoke ("Spawn", Random.Range (spawnMin, spawnMax));
 }
Comment
Add comment · Show 2 · 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 beefyt123 · Aug 04, 2015 at 11:35 AM 0
Share

I just tried the Update method and the same thing happened as it did with FixedUpdate. The prefab spawned way too many times and the Unity crashed

avatar image Ibzy · Aug 04, 2015 at 11:45 AM 0
Share

what is it you're trying to spawn? based on the logic i'd assume footprints?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

My prefabs always spawn twice at different positions 1 Answer

How do you instantiate just one prefab? 1 Answer

How do i spawn in a prefab with a x,y,z location? 2 Answers

How to spawn a 'boss' after all enemies defeated and then kill that 'boss'? 1 Answer

i need Spawn Object loop if find object tag ("Clone") in list ,when i delete find object tag ,cript is good but no find object, please help me 1 Answer


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