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 Fexception · Aug 26, 2014 at 01:19 AM · prefabspawn

Randomly spawn the same object with different properties consistently.

I'm trying to create a randomly spawning object similar to a hot air balloon. I want the hot air balloon to rise and fall, to Y mins and Y maxes, my issue is, I'm not sure what route I should take to make each consecutive balloon spawmn with a different Y min and Y max, here is the code I'm using to simulate this:

 using UnityEngine;
 using System.Collections;
 
 public class BalloonScript : MonoBehaviour {
 
     // Variables for floating functions
     public Vector2 speed = new Vector2(0,0);
     public float bobSpeed = 3.0f;  //Bob speed
     public float bobHeight = 0.5f; //Bob height
     public float bobOffset = 0.0f;
     
     public float PrimaryRot = 80.0f;  //First axies degrees per second
     public float SecondaryRot = 40.0f; //Second axies degrees per second
     public float TertiaryRot = 20.0f;  //Third axies degrees per second
     private float pos;
     private float bottom;
     //^^^Variables for floating functions^^^//
     
     void Awake () {
 
         bottom = transform.position.y;
     }    
 
     void Update () {
         
 
         Vector2 movement = new Vector2 (speed.x, speed.y );
         movement *= Time.deltaTime;
         transform.Translate (movement);
 
         //transform.Rotate(Vector3(0, PrimaryRot, 0) * Time.deltaTime, Space.World);
         //transform.Rotate(Vector3(SecondaryRot, 0, 0) * Time.deltaTime, Space.Self);
         //transform.Rotate(Vector3(0, 0, TertiaryRot) * Time.deltaTime, Space.Self);
 
         pos = bottom + (((Mathf.Cos((Time.time + bobOffset) * bobSpeed) + 1) / 2 ) * bobHeight);
         Vector2 newPosY = new Vector2(transform.position.x, pos);
         transform.position = newPosY;
         
         
     }
 }

I tried to use Random.Range in this script, however, leaving this in the update function makes each balloon act erratically as the Y value (bobHeight) is flying all over the place. If I leave it in any non-update function, the balloons will all spawn each second, with the same bob height. Here is the spawn script:

 using UnityEngine;
 using System.Collections;
 
     public class NewScript : MonoBehaviour {
     public GameObject enemyPrefab;
     private GameObject newParent;
     public float xMin = 19F;
     public float xMax = 85F;
     public float yMin = 3.5F;
     public float yMax = -4.5F;
 
     // Use this for initialization
     void Start () {
 
         newParent = GameObject.Find("2-Middleground");
 
         InvokeRepeating ("SpawnUpdate", 1f, 1f);
         
     }
     void SpawnUpdate() {
         Vector3 newPos = new Vector3(Random.Range(xMin, xMax), Random.Range(yMin, yMax), 0);
         GameObject octo = Instantiate(enemyPrefab, newPos, Quaternion.identity) as GameObject;
         octo.transform.parent = newParent.transform;
         }
 }

So, my question put more clearly is. I want these balloons to spawn once per second, each consecutive balloon has a different bobHeight and bobSpeed within the same range of numbers which are randomly generated (perhaps in to an array). The balloon has the first script attached to it, and the balloon has a prefab which is what the 2nd script spawns every second.

The other thing I tried was just making an animation curve, but that locks my X and Z positions which I don't want.

What route should I take here?

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
Best Answer

Answer by Kiwasi · Aug 27, 2014 at 12:28 AM

Solution pseudo code

 class balloon {
 
     public Vector2 speed = new Vector2(0,0);
     public float bobSpeed;  
     public float bobHeight; 
     public float bobOffset;
     ....
 
     void Awake (){
         bobSpeed = Random.Range (0f,5f);
         bobHieght = Random.Range (0f,5f);
         bobOffset = Random.Range (0f,5f);
         ....
     }

     void Update (){
         // Existing movement code
     }
 }



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 Fexception · Aug 27, 2014 at 01:39 AM 0
Share

You just opened my $$anonymous$$d a little more to understand program$$anonymous$$g logic. Thanks for that, that was the solution.

And now that I think of it, I misunderstood $$anonymous$$er in his suggestions as well. I needed to see it before I understood it.

Thanks to you both.

avatar image
1

Answer by Tomer-Barkan · Aug 26, 2014 at 02:05 AM

You have two options:

  1. (Prefered) - right after instantiating a balloon, set its yMax and yMin properties randomly.

  2. In the Start() - set the yMin and yMax randomly (make sure max > min)

Any initialization should be done in Start() or Awake(). In Update, keep to the continous behaviour. In your case, the continous behaviour is moving between min and max, and there is no random there. You do want to randomize min and max, but you only need to do it once per object, so do it right after instantiating, or from the start.

Comment
Add comment · Show 3 · 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 Fexception · Aug 26, 2014 at 02:18 AM 0
Share

How would I refer to the cloned game object again once it instantiates? In the 2nd script, the Y$$anonymous$$ and Ymax are only used to give the balloon a starting point. But then I have the floating script to make it actually move, which requires modification to the Y again here:

 pos = bottom + ((($$anonymous$$athf.Cos((Time.time + bobOffset) * bobSpeed) + 1) / 2 ) * bobHeight);
         Vector2 newPosY = new Vector2(transform.position.x, pos);
         transform.position = newPosY;


This is in my update function, I can't move it to my Start/Awake because then the balloon won't float up and down on the Y axis. Every new balloon that spawns always begins at whichever point $$anonymous$$athf.Cos has moved the previous spawned balloons on the screen, so they're all floating at exactly the same time and exactly the same way with no deviation.

avatar image Fexception · Aug 26, 2014 at 03:13 PM 0
Share

......Bump

avatar image Fexception · Aug 27, 2014 at 12:07 AM 0
Share

Thoughts anyone?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Unity Crashes When Spawning Prefab 2 Answers

Make a object spawn 1 Answer

instatiate prefabs at start, at a spawn point 2 Answers

Spawned Scrolling background objects gaining a gap when speed increases. 0 Answers

Where did the avatars go? Multiplayer spawn prefabs 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