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 tonyjoseph456 · Oct 31, 2014 at 03:58 PM · spawninginfinite runnercoins

Spawning coins randomly in a 3d infinite runner game

I have a 3d infinite runner car racing type game in which the player is stationary and the background moves. In my game I want to spawn coins randomly over time and the coins has to be spawned very much ahead of the player, and the z axis of the coins get reduced keeping y axis constant and x axis values in a random range of -2 and 2. The coins are spawning correctly but they are spawned in an irregular manner. I created four coin game objects in my scene, I want to spawn the 4 coins in a straight line because then the player can easily collect the coins since they are coming in a straight line to the player. The players motion is only in the x axis from -2 to 2. Now my problem is that the coins are spawned irregularly because of that the coins cannot be collected easily by the player. THis is my code:

 function Update()
 {
     MoveCoin();
 }
 
 function MoveCoin()
 {
     ReleaseCoin();
     //CoinsOnRoad is an array containing the current coins which are on the road
     //CoinPool is the array of coins
     for(var i:int =0;i<CoinsOnRoad.length;i++)
     {
     var gcoin:GameObject = CoinsOnRoad[i] as GameObject;
     gcoin.transform.position.z-=3*speed*Time.deltaTime;
     if(gcoin.transform.position.z>=-10)
     {
         //Do nothing if the coin is on the visible area of the road. If it becomes invisible
         //remove the coins from CoinsOnRoad Array and insert the coin back to the CoinPool Array
     }
     else
     {
         CoinPool.push(gcoin);
         CoinsOnRoad.remove(gcoin);
             
     }
             
     }
 }
 
 function ReleaseCoin()
 {
     if(CoinPool.length==0)
     {
     
     }
     else
     {
         var coin:GameObject=CoinPool.shift() as GameObject;
         CoinsOnRoad.push(Instantiate(coin,new Vector3(Random.Range(-2.0,2.0),0.3,30+Random.Range(1,10)),Quaternion .identity));
         
     }
 }

 

The coins are spawning correctly but in an irregular order. Can someone please help me out? Thanks in advance..Since I'm new to unity, I didn't knew whether even my game logic is correct or not. Can some one correct me with the code if I'm wrong some where in my code.

Comment
Add comment · Show 1
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 bubzy · Oct 31, 2014 at 04:13 PM 0
Share

try to hold a z variable in memory and increment it everytime you spawn a coin, then ins$$anonymous$$d of passing a random number to the instantiate method you can increment the z variable and have the coins in a nice straight line, you can also do some other nice stuff if you only choose a random number for the first part of the x axis, such as diagonal lines and other patterns, by incrementing a value after the first one has been randomly chosen

1 Reply

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

Answer by hav_ngs_ru · Oct 31, 2014 at 04:23 PM

 var randomX;
 var coinCount = 0;
 var coinNum = 0;
 var coinLineMin, coinLineMax; // setup max and min coins in one line
 var coinInterval; // distance between coins in line

 function ReleaseCoin()
 {
  if(CoinPool.length==0)
  {
  }
  else
  {
   if(coinNum == coinCount) {
      randomX = Random.Range(-2.0,2.0);
      coinCount = Random.Range(coinLineMin, coinLineMax);
      coinNum = 0;
   }
   coinNum ++;
   var coin:GameObject=CoinPool.shift() as GameObject;
   CoinsOnRoad.push(Instantiate(coin,new Vector3(randomX,0.3,30+coinNum*coinInterval),Quaternion .identity));
  }
 }


somethink like this.

I didnt test a code and I usually use C#, so stupid mistakes are possible :)

but i hope idea is clear :)

Comment
Add comment · Show 4 · 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 tonyjoseph456 · Nov 04, 2014 at 04:51 AM 0
Share

Thanks for the answer. I have one doubt, can you please clear me. How do I delete the coin game object created using the Instantiate after the coin leaves my camera visibility area?

avatar image tonyjoseph456 · Nov 04, 2014 at 04:55 AM 0
Share

you didn't used the randomZ variable anywhere in the code? Where to use that??

avatar image hav_ngs_ru · Nov 06, 2014 at 02:39 PM 0
Share

make coin controller in separate script, and make void OnBecameInvisible() function, it will be called every time a coin goes out of screen. And just call Destroy() in it to destroy coin.

BUT!

  1. Try to not use Instantiate and Destroy when playing a level, it may cause a hangs. Better way - is to Instantiate a objects in Start or Awake, and just Activate/Deactivate it when you need it to appear/disappear. Find an ObjectPool scripts to see an example of how to manage objects.

  2. Bear in $$anonymous$$d that if your object is visible in Editor windos (when you play your game in unity Editor) - the OnBecameVisible will call at start of game for this object (not when it addears in $$anonymous$$ainCamera`s screen), and OnBecameInvisible will NOT call, despite it hides from a $$anonymous$$ainCamera`s screen. So hide editor window (or move it viewport to NOT to see coins) when you debug game.

  3. As alternative way is to check distance between camera and coid to deter$$anonymous$$e when a coin is far away and not needed. This way will not cause features like in item "2". :)

avatar image hav_ngs_ru · Nov 06, 2014 at 02:40 PM 0
Share

randomZ - is from another idea, dont use it :)

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

Spawning infinite coins 1 Answer

Infinite runner spawning enemies, need some guidelines. 0 Answers

How Increase And Decrease And Reset Spawn Rate Over 0 Answers

,Getting a Spawner Manager Script to reference a Spawner 1 Answer

How to make enemies spawn IN the ground without rising up automatically first? 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