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
-1
Question by valtteri_m · Oct 13, 2014 at 05:23 PM · javascriptvariablesintrandomspawningspawner

Mob Spawner, choosing a random mob

I have a mob spawner script that has a timer int for how often an object spawns, variable for the prefab that I want to get spanwed and ints for how far and close the player must be for the script to disable.

But I would want to have multiple variable spots for diffrent objects and also having another variable for what % chance it has to be spawned. Any help for where to start from?

My current mob spawner scrpt:

 var enemy: GameObject;
 var spawnTime:float = 3f;
 var player : Transform;
 var CloseDistance = 3;                    //Close Enough
 var CloseEnough : boolean = false;
 var FarDistance = 12;                    //Far Enough
 var FarEnough : boolean = false;
 var isInInvoke : boolean = true;
 
 private var thisTransform : Transform;
 
 function Start(){
     thisTransform = transform;
     player = GameObject.FindWithTag("Player").transform;
 }
   
 InvokeRepeating ("Spawn", this.spawnTime, this.spawnTime);
 
 function Spawn (){
     Instantiate (enemy, thisTransform.position, thisTransform.rotation);
 }
  
 function Update(){
     //Close Enough
     if(Vector2.Distance(transform.position, player.position) < CloseDistance){
         CloseEnough = true;
     } else {
         CloseEnough = false;
     }
     if (CloseEnough == true) {
         CancelInvoke("Spawn");
         isInInvoke=false;
     }
     if (CloseEnough == false && isInInvoke==false) {
         InvokeRepeating ("Spawn", this.spawnTime, this.spawnTime);
         isInInvoke=true;
     }
     
     //Far Enough and Close Enough
         if(Vector2.Distance(transform.position, player.position) > FarDistance){
         FarEnough = true;
     } else {
         FarEnough = false;
     }
     if (FarEnough == true) {
         CancelInvoke("Spawn");
         isInInvoke=false;
     }
     if (CloseEnough == false && isInInvoke==false) {
         InvokeRepeating ("Spawn", this.spawnTime, this.spawnTime);
         isInInvoke=true;
     }
 }

Comment
Add comment · Show 4
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 JoeStrout · Oct 13, 2014 at 09:11 PM 0
Share

Why not just put this script on different spawner objects in different locations?

avatar image isfrseirra259 · Oct 13, 2014 at 09:35 PM 1
Share

Have you tried to run this script? From what I can see, there'll be quite a few compiler errors that may crop up.

avatar image Kiwasi · Oct 13, 2014 at 11:32 PM 0
Share

What a terrible piece of code. I like how you cache the transform in Start. Then proceed to ignore your cached variable and use the transform variable directly.

I suggest rewriting the entire script with simplicity and DRY principles in $$anonymous$$d. Run it in Unity to make sure it compiles and has the desired effect. Then consider adding more functionality.

At the moment it will be more work then its worth to keep adding to this script.

avatar image valtteri_m · Oct 14, 2014 at 08:51 AM 0
Share

it works! Did you not understand what I wanted? I wanted a rare chance of it spawning a diffrent type of mob

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by alexander11 · Oct 13, 2014 at 11:53 PM

try using random.Range

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 valtteri_m · Oct 14, 2014 at 08:51 AM 0
Share

Did you not understand what I wanted? I wanted a rare chance of it spawning a diffrent type of mob

avatar image
0

Answer by MrSoad · Oct 13, 2014 at 10:58 PM

Here is some code I wrote to randomly spawn either a homing mine or normal mine, maybe this will help?

 //Spawn a Mine Or Mine_Homing.   
 if (Random.Range(0, 10) > 6) {
 
     if (iActive_Mines_Homing < iMax_No_Of_Mines_Homing) {
         subSpawn_Mine_Homing();
         
     } else {
         subSpawn_Mine();
     }
     
 } else {
     
     if (iActive_Mines < iMax_No_Of_Mines) {
         subSpawn_Mine();
         
     } else {
         subSpawn_Mine_Homing();
     }
 }

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 MrSoad · Oct 13, 2014 at 11:42 PM 0
Share

Just another note, with invoke methods you can use :

 if (isInvoking("Name of your invoke") {

To see if it is running rather than having to use Boolean flags everywhere.

avatar image valtteri_m · Oct 14, 2014 at 08:53 AM 0
Share

seems like this will help, thanks! Il try to code that stuff in once I get monoDevolp working.. right now it shows errors when trying to open a script

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

33 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

Related Questions

Making a List for Inventory System, Got an error in finding constructors and variables 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Changing a variable on a different object 2 Answers

Javascript int to float 1 Answer

Script is setting variables but they're being reset 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