- Home /
How do i spawn these cloned gameobjects with varying speeds???
Using C# for the coding, quite new to this to be honest. I want to make it so that the gameobject that is cloned in using the prefab can have a varying speed. One may spawn falling at -5 on the y axes, and another may spawn falling at -3 on the y axes. I'm trying to be as clear as possible.
The current code below makes it so that they spawn randomly across the page between a set of coordinates.
public Vector3 maxPos;
public GameObject myPrefab;
void Start () {
//Loads clones with inherited attributes
//Adds random spawn area and rotation with maximum
Instantiate(myPrefab, new Vector3(Random.value * maxPos.x, Random.value * maxPos.y, Random.value * maxPos.z), Random.rotation);
}
Answer by Landern · Jun 09, 2014 at 02:25 PM
Not sure how you are adjusting the falling speed, i will assume that myPrefab has a script on it that contains this value and this value is available through reference.
Instantiate returns a GameObject, but if you have a custom script attached, you can cast it.
public Vector3 maxPos;
public GameObject myPrefab;
void Start () {
//Loads clones with inherited attributes
//Adds random spawn area and rotation with maximum
PrefabAttachedScript newPrefab = Instantiate(myPrefab, new Vector3(Random.value * maxPos.x, Random.value * maxPos.y, Random.value * maxPos.z), Random.rotation) as PrefabAttachedScript;
newPrefab.fallSpeed = Random.Range(0, 10);
}
Answer by Jeff-Kesselman · Jun 09, 2014 at 02:22 PM
Make sure your prefab has a rigid body.
Assign speed to rigid body.velocity in the instances as appropriate.
If you don't want general gravity in your game, turn it off in the physics inspector. if you just don't want it effecting these objects, set the rigid body in the prefab to no gravity.