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 Reroshaggy · Jun 17, 2013 at 03:21 PM · randomspawn points

Two specific transform values instead of random int? Even distribution...

Hi community I am in need of a little assistance. I'm relatively new to JS (or any other language for that matter and have hit a snag. I have managed to cobble together a script for objects in my game that dictates what happens when they fall through the world or out of bounds or fall asleep. The problem I'm having is what I want is for the balls to spawn at 2 (or three or four depending on var) points in the level, but only 1 then the next and so forth. It's to evenly distribute balls amongst the players. Seeing as I am not a scripting genius I can't seem to figure it out without your help.

EXAMPLE IMAGE alt text

MY CODE:

 #pragma strict
 var floorHeight = 0;
 var spawnHeight = .03;
 function Update ()
 {
         if(transform.position.y < floorHeight) //if falls through floor transform to int up
         {
             transform.position.y = spawnHeight;
         }
         if(transform.position.y > .5) //above floor .5
         {
             rigidbody.AddForce (0, -30, 0); // accelerate its speed down
         }
            if(rigidbody.IsSleeping()) //if at rest transform location to position
         {
               transform.position = Vector3(Random.Range(4, 7), 1.6, 6.7);
         }
 //This line of code is to add random movement to ball if sleeping
 //           if(rigidbody.IsSleeping())
 //           {
 //           rigidbody.velocity = Vector3(Random.Range(-5.0, 5.0), 0.01, Random.Range(-5.0, 5.0));
 //        }
 }
example.png (171.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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by AntiLunchBox · Jun 17, 2013 at 04:01 PM

You can make an array of spawn points and loop through those everytime you spawn an object.

So,

 var spawnPoints: Transform[]; //populate this with your spawn points
 var currentIndex = 0;
 
 function SpawnBall()
 {
      if(spawnPoints.Length == 0)
           return;
      
      if(currentIndex >= spawnPoints.Length)
           currentIndex = 0;
 
      GameObject.Instantiate(ballPrefab, spawnPoints[currendIndex], Quaternion.identity);
 
      currentIndex++;
 }

Excuse my javascript, I always use c#, but it should atleast get you in the right direction

Comment
Add comment · Show 5 · 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 AntiLunchBox · Jun 17, 2013 at 04:10 PM 0
Share

Or if you want something more literal to paste into your code, just alternate between points by replacing this line with:

transform.position = Vector3(4+(Random.Range(0, 1)*3), 1.6, 6.7);

avatar image Reroshaggy · Jun 17, 2013 at 05:45 PM 0
Share

tried your idea Antilunchbox but it just seemed to drop everything from the same point.

avatar image AntiLunchBox · Jun 17, 2013 at 06:08 PM 0
Share

oh sorry it's:

transform.position = Vector3(4+(Random.Range(0, 2)*3), 1.6, 6.7);

The second parameter is a maximum and is excluded. so that will include 0 and 1

avatar image AntiLunchBox · Jun 17, 2013 at 07:27 PM 0
Share

Did it work???

avatar image Reroshaggy · Jun 18, 2013 at 04:03 AM 0
Share

Sorry went to work for 3pm It almost worked consistently but occasionally would just start spawning on one side upto five balls.

As far as your first idea (the more elegant and longer coded one) I can't use it. The game has a set amount of balls and your script would spawn them, i'm just looking to move ones that have fallen through or gotten stuck, evenly amongst the players.

For the time being I'll use the snippet code which is usefull thanks.

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

15 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

Related Questions

Placing my GameObject on a Ground with differences in height 2 Answers

Spawn GameObjects without overlap 1 Answer

Why does this script crash Unity? Also simple random 2D movement? 1 Answer

How to get a game object to rotate (change direction from clockwise to anticlockwise) randomly around a circle. 1 Answer

problem with ai 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