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 fazraz · Aug 13, 2015 at 03:57 AM · instantiaterandomprefabs

How would I go about making random car prefabs drive up and down a road at varying speeds? (for example, crossy road)

I have created a few 3D vehicles, I have also created a road with two lanes. Ultimately I would like cars travelling in opposite directions and at varying speeds. I would also like each car which travels by to be a random car prefab.

I have some basic idea e.g. creating spawn point game object either end of the road and telling each prefab to travel from A to B.

Also using a random number selection function to determine which prefab is instantiated maybe?

I would be very grateful if someone could point me in the right direction, or at least to a helpful resource.

Many thanks,

Freddie

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer Wiki

Answer by itsharshdeep · Aug 13, 2015 at 07:04 AM

Hi,

Sorry if I misunderstood the Question.

Yes you can easily use that approach that Spawn a car at the end of the line ( i.e. Outside of Screen) to make the natural effect.

To Spawn the Random cars place all the cars in the Particular folder in the Resources ( like "Resources> AvailableCars"). Now you have to Instantiate the cars randomly from this by this line of code. ( this is not the best approach but if you are not too much familiar wid the code then just name the prefabs as 0,1,2, ... upto N numbers). Then Generate the random number.

 public int totalNumberOfItemsInAvailableCars;
    
 // write this where you want to Instantiate the car
 int randomNum = Random.Range(0, totalNumberOfItemsInFolder)
 Object objTemp = Resources.Load ("AvailableCars/" + randomNum);
 Instantiate (objTemp) as GameObject;

The above code is for spawning random cars. Now for the random speed the trick is there must be one car in the lane. This is because if there is more than one car in single lane then suppose the first car is slow and second one is faster than the second car will collide with the first one which is not the desired output.

So same approach will be present there for the speed. Write the below mentioned line of code where you are handling the speed & positioning of the car.

 float minSpeedOfTheCar = 0.5f;
 float maxSpeedOfTheCar = 3f;
     
 float randomSpeed = Random.Range(minSpeedOfTheCar, maxSpeedOfTheCar);

Now assign the value of 'randomSpeed' to the carSpeedHandler variable or use directly this variable as the carSpeed.

Thanks

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 fazraz · Aug 15, 2015 at 08:17 PM 0
Share

Incredible answer, makes so much sense now. Really really appreciate that!

$$anonymous$$any thanks,

Farid

avatar image itsharshdeep · Aug 17, 2015 at 04:39 AM 0
Share

Thanks A lot.. :) It it really my pleasure :-D

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

instantiate prefabs y+1 from last instintiated prefab 1 Answer

instantiate a prefab from array and then changing it 1 Answer

Instantiate random object from Array 2 Answers

collision with instantiated prefabs child 1 Answer

Random array issue C# 2 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