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 mahmouds · Jun 19, 2020 at 07:16 PM · unity 5cameraspawningmapdesign

Spawning System

So I'm making a game where you are a grenade trying to cross a road without getting hit by any cars. Kinda like crossy road. Here is how it looks like: alt text/;l

So I cant think of a good system that would spawn cars, like how would I manage the speed? By animation or add force? How would I say where to spawn the cars and when. When to destroy the cars, when to spawn the road etc... Really been struggling with this...

Thanks in advance :D

unity-pic.png (362.8 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by SiniKettu_ · Jun 19, 2020 at 08:27 PM

Cars speed

I would recommand you to use transform.position += Vector3.forward*speed. If you want to have random speeds, you can do something like this :

 //script on every car
 float thisCarSpeed;
 float defaultSpeed;
 float randomValue;
 void Start()
 {
     thisCarSpeed = Random.Range(defaultSpeed-randomValue, defaultSpeed+randomValue)
 }
 
 void Update()
 {
     transform.position += Vector3.forward*thisCarSpeed;
 }
 

So each car has its own random speed in range.



Spawning system

I would use a coroutine like this :

  //global script which manage car speawning from car's prefab.
     
 public GameObject[] carPrefabs; //all your car prefab (because it seems like you have several cars prefab)
 public float spawningRate; //delay between each car spawn
 public Tranform[] spawningPoints; //create some points on each road. Then they will get forward with the camera. I assume that your camera never go back.
 
 public float spawningOffset;
 
 void Start()
 {
    StartCoroutine(Spawning());
 }
 
 void Update() //moves the spawn points along the road
 {
    //keep the 'foreach' which works !!! the first works if the road is along the x axis and the second works if the road is along the z axis. I dunno in wich orientation you set up the road.
 
    //FIRST
 
     foreach(Tranform point in spawningPoints)
      {
           point.position = new Vector3(Camera.main.tranform.position.x+spawningOffset, 0, 0);
 
      }
 // THE SECOND
  foreach(Tranform point in spawningPoints)
      {
           point.position = new Vector3(0, 0,Camera.main.tranform.position.z+spawningOffset);
 
      }
 }
 
 IEnumerator Spawing ()
 {
    while(true) //makes the while repeating itself forever.
   {
        Transform randomSpawningPoint = spawningPoint[0, 
        spawningPoint.Length];
 
        Vector3 spawingPosition = randomSpawningPoint.position;
       
        Instantiate(carPrefabs[Random.Range(0, carPrefabs.Length), spawningPosition, Quaternion.identity] //the Quaternion.identity can be wrong, it depends of how do you created your game (in wich orientation your road is ?)
        //make sure the prefab have the script i also wrote about (cars speed)
 
        yield return new WaitForSeconds(spawningRate);
        yield return new WaitForEndOfFrame(); //this one avoids an infinite frame (= game crash) if spawningRate = 0)
   }
 }


I hope this is useful Tell me if you get an error with it, i didn't test it

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 SiniKettu_ · Jun 19, 2020 at 08:36 PM 0
Share

If you want to stop the spawns you can create a boolean in the global variables section and check if this boolean is true at the beging of the while(true), and if this bool is actually true then the coroutine instantiate a car.


So when you uncheck this bool from the inspector or from another script, cars won't spawn, and when you recheck the bool, the cars spawn again.

avatar image mahmouds · Jun 20, 2020 at 08:11 PM 0
Share

Hey! Thank you for replying :D this looks so scary xD I will experiment and let you know what happens!

avatar image mahmouds · Jun 21, 2020 at 12:19 AM 0
Share

Hey I am pretty confused with what this does ' IEnumerator Spawing ()'

avatar image SiniKettu_ mahmouds · Jun 22, 2020 at 08:21 AM 0
Share

An "IEnumerator" is just, in a nutshell, like a function (which starts with the 'void' keyword) but it's allow to wait (WaitForSeconds(x), WaitForSecondsRealtime(x), WaitForEndOfFrame(), WaitUntil(), WaitWhile()...)

(We use this because we cannot use these waiting methods in a "normal" 'void')

This is called a coroutine.

avatar image
0

Answer by mahmouds · Jun 21, 2020 at 05:23 PM

@SiniKettu_ I am getting errors with: spawningPoint[0, spawningPoint.Length]; When I remove the zero I don't get any errors. Thanks!

Comment
Add comment · Show 6 · 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 SiniKettu_ · Jun 22, 2020 at 08:18 AM 0
Share

Yes but is this working fine ?

avatar image mahmouds SiniKettu_ · Jun 22, 2020 at 02:29 PM 0
Share

When I remove the zero I get no errors but it doesn't spawn anything...

avatar image mahmouds mahmouds · Jun 22, 2020 at 03:33 PM 0
Share

Actually thats my bad I do get this error when i remove the zero: "Indexoutofrangeexception: index was outiside the bounds of the array. " I does let me play the game though it just gives this error and doesnt spawn anything

Show more comments

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

285 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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

Enabeling components 1 Answer

Unity 5.2 camera not recognised 1 Answer

Make unity camera appear all screen width 1 Answer

Switching camera to follow object 0 Answers

How to access camera preview frames with MediaCapture Class in Unity? 0 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