- Home /
How do make a gameObject accelerate downwards?
I have a prefab that is constantly spawning and falling downwards. How do I them start off slower and then have them drop down faster every 30 seconds?
I tried using a rigidbody, but nothing seems to be occurring other than changing the constant falling speed.
Answer by Lylek · Jul 02, 2014 at 02:57 AM
Increasing Drag in the Rigidbody component will start it out falling slower. Then you can either decrease the drag, or ad force to the rigidbody, or both.
gameObject.transform.rigidbody.AddForce(-Vector3.up * 100);
gameObject.transform.rigidbody.drag -= 1;
If you want the speed to change every 30sec, you need to create a timer. Such as:
var time : float;
function Update() {
time += 1 * Time.deltaTime; //Increase the variable 'time' by 1, every seceond
if(time >= 30) {
//Increase Speed
time = 0;
}
}
This is assuming you are using JavaScript?
I am using JavaScript.
Do I insert this into a text mesh or do I attach to the gameObject. I did both, but nothing seems to be happening. I'm only using the timer script.
Attach it to whatever you want to fall. And, the first part of code I posted will need to be put with the second part, where it says "//Increase Speed".
I'm receiving this error: There is no 'Rigidbody' attached to the "Star(Clone)" game object, but a script is trying to access it. You probably need to add a Rigidbody to the game object "Star(Clone)". Or your script needs to check if the component is attached before using it.
And my game seems to be lagging.
Answer by Lazdude17 · Jul 02, 2014 at 04:09 AM
private float time;
private float fallForce; //Start this at whatever speed or make it public so you can change it in the inspector
void FixedUpdate()
{
rigidbody.AddForce(0, fallForce, 0);
if(time > 30)
{
fallForce += 2f; //Every second after 30 secs will increase the fallForce by 2 or whatever number you use
}
time += 1;
}
In C#
Your answer
Follow this Question
Related Questions
Enemy shooting problem 0 Answers
Object coordinates not updating as expected 0 Answers
Instantiating prefabs dynamically in script 1 Answer