- Home /
How to increase rate of a falling object in pooling object?
i m currently working on a script where i want to drop random objects from a list of prefabs. i am using pool objects for this. The problem is as follow: i want these objects one by one when game starts. The rate of falling objects should be slow when game starts. But as time passes, the objects shold fall with increasing more speed. How to do that?
using UnityEngine; using System.Collections; using System.Collections.Generic; using System.Linq;
public class Poolingscript2 : MonoBehaviour {
public GameObject[] allprefabs; //array of prefab
public List<GameObject> bullets ;
public int listsize;
GameObject go;
// Use this for initialization
void Start ()
{
createlist ();
}
void createlist() //creates list of gameobjects from array of prefabs
{
allprefabs = Resources.LoadAll<GameObject> ("Prefab"); //reload prefabs in array
for (int i=0; i<listsize; i++)
{
foreach (GameObject allprifab in allprefabs)
{
go = Instantiate(allprifab)as GameObject; //make object from aray
go.SetActive(false);
bullets.Add (go); //add to list
}
}
}
GameObject temp;
void fallobject()
{
int randm = Random.Range(0,listsize); //select random number
temp = bullets.ElementAt(randm); // assign number and select random object
bullets.Remove(temp); // remove that random object
temp.transform.position = transform.position;
temp.SetActive(true); //activate, so it falls down
Debug.Log ("removed element no: " +randm);
}
public void Update()
{
}
public void addtolist(GameObject g) //function for adding fallen object back to list
{
g.SetActive (true);
bullets.Add (g);
}
}
It's not quite clear how you are making the bullets fall. If you are using physics, you could increase the value of gravity over time (http://docs.unity3d.com/ScriptReference/Physics-gravity.html)
Answer by ZacGarby · Aug 07, 2015 at 05:45 PM
Every time one drops, increase the mass by a certain amount.
temp.GetComponent.<Rigidbody>().mass = newMass
and every time one falls, increase newMass variable by, say, 0.1
Increasing the mass WILL increase the momentum with which they fall, but they will fall at the same speed & acceleration, regardless of mass.
(this is because though gravity will pull harder on the object with more mass, that object also has more inertia to overcome. These cancel each other out, unless you start adding in air-resistance and stuff.)
Your answer
Follow this Question
Related Questions
Boost Script Time problems 1 Answer
A node in a childnode? 1 Answer
Loading Screen and Lerp 3 Answers
Play whole animation over specified time via script 1 Answer
My spawned obstacle stop working properly after a certain time 1 Answer