x


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

more ▼

asked May 13 '10 at 12:49 PM

Swifty gravatar image

Swifty
2 2 2 2

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.

May 13 '10 at 12:52 PM duck ♦♦
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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);
more ▼

answered May 13 '10 at 01:09 PM

Ashkan_gc gravatar image

Ashkan_gc
9k 33 56 117

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3717
x458
x8

asked: May 13 '10 at 12:49 PM

Seen: 1193 times

Last Updated: May 13 '10 at 12:49 PM