# Using unity Rigidbody 2D gravity, how to add to velocity?

I am creating a top-down game, and have an object I want to create a small bouncy illusion for. Since I dont have anything to collide with I'll have to do it with Scripts.

Without Unity I would probably do something like:

function fall ()
{
this.vertical_speed = this.vertical_speed + GRAVITY;
if (this.vertical_speed > TERMINAL_VELOCITY)
{
this.vertical_speed = TERMINAL_VELOCITY;
}
this.vertical_position = this.vertical_position - this.vertical_speed;
}


However in Unity we have the Rigidbody 2D, so I want to somehow use that for this.

if I set rigidBody.velocity = new Vector2(1, 0) * speed * delta;, it gets a constant speed, and ignores gravity. How can I add gravity?

i.e: I want to create a curve like:

• You're asking quite a few questions at the end there. Besides that, you are doing new Vector2(1,0) where 0 is Y so whatever Y velocity there is will just be 0, so gravity won't impact it. IIRC – n_plum Jun 6 '17 at 13:53
• Didn't even think about that, only 1 question now. Also, I know what I am doing wrong, I just dont know how to do it right. – Green_qaue Jun 6 '17 at 13:58

Preserve the existing vertical (ie. Y) movement. That is, when you create the vector to assign, use the existing velocity.y for the y component of the vector, and only create a new x component.

• Thanks, using this I get gravity. However I would like it to go up (y+) first, then start going down. Like a curve, this just goes down :) – Green_qaue Jun 6 '17 at 15:22
• Don't try to control the Y velocity directly in that case. Instead apply an upward impulse ie. Rigidbody2D.AddForce() with force mode set to "impulse" for a sudden jolt docs.unity3d.com/ScriptReference/ForceMode2D.Impulse.html – jhocking Jun 6 '17 at 15:38
• cool, Ill try this – Green_qaue Jun 6 '17 at 15:40
• This works. For future readers; Dont be stupid and put it in the update loop like me, call it only once! – Green_qaue Jun 6 '17 at 15:45
• or put it in the update loop but with a condition so it doesn't trigger every frame; that's how I do a jump command – jhocking Jun 6 '17 at 15:47

If you are using gravity on the rigidbody and also setting the rigidbody using new Vector2 then just add the current velocity y to it

new Vector2(1, rigidBody.velocity.y);

• Thanks, using this I get gravity. However I would like it to go up (y+) first, then start going down. Like a curve, this just goes down :) – Green_qaue Jun 6 '17 at 15:20
• Yes :) That sounds like it – Green_qaue Jun 6 '17 at 15:25