2
\$\begingroup\$

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: enter image description here

\$\endgroup\$
  • \$\begingroup\$ 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 \$\endgroup\$ – n_plum Jun 6 '17 at 13:53
  • \$\begingroup\$ 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. \$\endgroup\$ – Green_qaue Jun 6 '17 at 13:58
2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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 :) \$\endgroup\$ – Green_qaue Jun 6 '17 at 15:22
  • \$\begingroup\$ 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 \$\endgroup\$ – jhocking Jun 6 '17 at 15:38
  • \$\begingroup\$ cool, Ill try this \$\endgroup\$ – Green_qaue Jun 6 '17 at 15:40
  • \$\begingroup\$ This works. For future readers; Dont be stupid and put it in the update loop like me, call it only once! \$\endgroup\$ – Green_qaue Jun 6 '17 at 15:45
  • \$\begingroup\$ 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 \$\endgroup\$ – jhocking Jun 6 '17 at 15:47
1
\$\begingroup\$

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);
\$\endgroup\$
  • \$\begingroup\$ 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 :) \$\endgroup\$ – Green_qaue Jun 6 '17 at 15:20
  • \$\begingroup\$ Yes :) That sounds like it \$\endgroup\$ – Green_qaue Jun 6 '17 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.