# return object

This topic is 4856 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

float d = +2.0f, e = -10.0f, f; .... void myIdle() { ..... e += 0.2; } Using the above code I have made an object move upwards. How do I make the object go back down the way it came up(i dont mean wrapping)? I have tried the following but doesnt work. if (e >= 10.0f) { e = 10.0f; e -= 2.0; } Can anyone help?

##### Share on other sites
I usually have a bool to where it is going. For example:
//Assuming this is C++bool up = true;if (up)    e += 0.2f;if (!up)    e -= 0.2f;if (e > MAXHEIGHT)    up = false;if (e < MAXHEIGHT)    up = true;

I've seen people do it with velocities but for some reason I never got them...

C++

##### Share on other sites
EDIT: The poster above just got in while I was writing, the same code though

The if statement block code won't work because e -= 2 will only happen when e>=10, which i guess isn't what you want it to do.

For simplicity, you could do this

Set up a global bool called movingUp and set it to true.

Write a an if else to test to see your object is at the bottom or top of the screen

if(e >= 10) //at the top
movingUp = FALSE
if(e <= 0) //at the bottom
movingUp = TRUE

Then write an if else to move your object such as:

if(movingup)
e += 0.2;
else
e -=2;

##### Share on other sites
float e = 0.0f; // The bodyfloat velocity = 4f; // The speed of the body.//...void myIdle(){    // Move the body    e += velocity;    // Add resistance    velocity -= 0.1f;        // Bounce off the ground    if(e < 0f)        e *= -1f;}

Implementing the bodies velocity will greatly make your life easier. If you run the above example, you'll see that e is thrown up in the air, and then peaks and slowly comes back down, much like gravity would act on a flying ball.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 23
• 12
• 12
• 11
• ### Forum Statistics

• Total Topics
634779
• Total Posts
3019241
×