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 Dalton Gray · Apr 29, 2011 at 07:24 PM · javascriptrandomspawnrpgmobs

Random Spawner using radius and numbers

I am wanting to make a javascript script people can download and import into unity and use to easily create RPG mob spawners. In there, I have variables which the user can set the amount of mobs to spawn and the radius around the spawner (Which is just an empty game object). I want to know how or where I can learn how to make them randomly spawn within that radius.

Can anyone please help me?

Comment
Add comment · Show 2
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 Joshua · Apr 29, 2011 at 07:33 PM 0
Share

Do you insist on it spawning inside a sphere?

avatar image Joshua · Apr 29, 2011 at 07:33 PM 0
Share

Because it's definitely possible, it's just a bit harder to explain how to :)

3 Replies

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

Answer by DaveA · Apr 29, 2011 at 08:00 PM

http://unity3d.com/support/documentation/ScriptReference/Random-insideUnitCircle.html and http://unity3d.com/support/documentation/ScriptReference/Random-insideUnitSphere.html and http://unity3d.com/support/documentation/ScriptReference/Object.Instantiate.html

I'd try:

var ran = Random.insideUnitSphere * mobSpawnRadius;
ran.y = 0.0;
var mobSpawnLocation : Vector3 = transform.position + ran;
Comment
Add comment · Show 1 · 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 Dalton Gray · Apr 30, 2011 at 06:33 PM 0
Share

Oh thank you so much for this one. It worked way better than $$anonymous$$e. I thought $$anonymous$$e was working until I played around with it some, the noticing it wasn't getting the radius right. This works flawlessly. Thank you so much.

avatar image
1

Answer by TheDemiurge · Apr 29, 2011 at 08:09 PM

The above post nails it. A sphere is definitely the easiest, anything else would require more calculations and more information.
If you care about the math, it's just X = spawnpoint.x + random(-radius,radius) and the same for Z.
If you're using Unity Terrain you can use the SampleHeight method to get the ground level at the point you've selected. Depending on how many mobs you're spawning and how small the radius is, the likelihood of spawning units on top of each other is pretty low, but you may not even care about that.

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 Dalton Gray · Apr 29, 2011 at 09:52 PM

I think I have one good solution after doing a lot more poking around unity answers, and I seem to have managed getting the radius spawner with this

public var mobName : String; // Sets the name of the mob that spawns in this area public var mobPrefab : GameObject; // Sets the prefab of the mob that you are spawning in this area public var mobSpawnNumber : int; // Sets the amount of mobs to spawn in this area public var mobSpawnRadius : float; // Sets the radius of the area of which to spawn the mobs around the spawner

function Start() { var mobSpawnLocation : Vector3 = new Vector3(Random.Range(this.transform.position.x, mobSpawnRadius), this.transform.position.y, Random.Range(this.transform.position.z, mobSpawnRadius)); Instantiate (mobPrefab, mobSpawnLocation, transform.rotation); }

And it has seemed to work well. Should I use a for loop on the mobSpawnNumber when choosing how many to spawn?

Actually, nevermind, that third link says it right there. Thanks =o)

Comment
Add comment · Show 1 · 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 DaveA · Apr 29, 2011 at 10:03 PM 0
Share

Edited my answer to reflect more like what you may need.

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

No one has followed this question yet.

Related Questions

Simple array an spawning question 6 Answers

Generate random number and set a GameObject active. 1 Answer

Only 2 out of 4 Objects ever spawn... Why ?(Closed) 2 Answers

Calling random functions 4 Answers

Random spawn timer 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