- Home /
Issue with adding thrust
For a project I am having to have an object fall from the sky and then land on the terrain. When the player presses the space bar thrust is applied to slow the descent.
The problem is I cannot get the thrust to apply smoothly. I have tried setting a variable to contain the gravity and another to contain thrust. The thrust is applied (by combining the negative value of the fall with the positive of the thrust, but then it remains. I need it to only apply the thrust for as long as the space bar is pressed and then have gravity take over again as the thrust is off and acceleration towards the ground continues.
Been trying to work on this for 2 days now and having no luck. Any Ideas or suggestion of where to go to find the relevant information?
Thanks in advance
Are you using rigidbody physics, and functions like AddForce? or are you making this all yourself from scratch? You probably need to post the code you have currently, whichever, in order to get some help with it.
Answer by Ashkan_gc · May 13, 2010 at 01:09 PM
you should create a coroutine for adding and removing some numbers smoothly
function AddSmooth (ref a,b,speed)
{
if (a < b)
{
while (a<b)
{
a+=speed*Time.deltaTime;
yield;
}
return;
}
else
{
while (a>b)
{
a-=speed*Time.deltaTime;
yield;
}
}
}
then you can have something like this in your Update function
if (Input.GetKeyDown (KeyCode.Space)) AddSmooth (gravity,gravity-thrust,1);
if (Input.GetKeyUp (KeyCode.Space)) AddSmooth (gravity,gravity+thrust,1);
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Change the gravity direction for CharacterController 1 Answer
Error in scripting with Physics.Raycast 0 Answers
jump script : 2D 1 Answer
Gravity and jumping is wierd 1 Answer