- Home /
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?
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
}
}
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.
Answer by Tomer-Barkan · Aug 26, 2014 at 02:05 AM
You have two options:
(Prefered) - right after instantiating a balloon, set its yMax and yMin properties randomly.
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.
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.