Gravity Always Wins?

Hello,
I wrote the following script where the keys a, d, and w move a rigidbody2d using transform.Tanslate(). Meanwhile the arrow keys move the rigidbody2d using rigidbody2d.AddForce(). As I use the keys to move the game object left or right everything works as expected.

But if I press the up arrow or w key the object moves up and then falls back down. Then as the object hits the ground it bounces back up to the same height and repeats like a ball. I just don’t understand why this is happening. Shouldn’t rigidbody2d.AddForce() keep adding an upward force every time the physics updates? Thus if my upward force is larger than the gravitational force then the object should keep moving up. Same for transform.Translate(). Each update I’m trying to move the object up through the Translate() method so each update the object should move up not fall back down.

The rigidbody2d component has 0 linear and rotational drag, gravity of 1, and mass of 1. No special material added or anything. Here is the script I added to the gameobject.

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {
    public int speed = 5;

    // Use this for initialization
    void Start () {
    }
  
    // Update is called once per frame
    void FixedUpdate () {
        if(Input.GetKey(KeyCode.RightArrow)){
            rigidbody2D.AddForce(new Vector2(speed, 0));
        }

        if(Input.GetKey(KeyCode.LeftArrow)){
            rigidbody2D.AddForce(new Vector2(-speed, 0));
        }

        if(Input.GetKey(KeyCode.UpArrow)){
            this.transform.Translate(new Vector2(0, speed*(float)0.04));
        }

        if(Input.GetKey(KeyCode.D)){
            this.transform.Translate(Vector2.right * speed * Time.deltaTime);
        }

        if(Input.GetKey(KeyCode.A)){
            this.transform.Translate(Vector2.right * -speed * Time.deltaTime);
        }

        if(Input.GetKey(KeyCode.Space)){
            this.transform.Translate(Vector2.up * speed * 2 * Time.deltaTime);
        }
    }
}

i think your are overlooking what you’re doing, try like you said; rigidbody2D.AddForce :wink:
or use Vector2.up if you stick with translate.

if(Input.GetKey(KeyCode.UpArrow)){
            this.transform.Translate(new Vector2(0, speed*(float)0.04));
        }

Haha oh wow I feel like an idiot. Yes I changed that to AddForce(). Works as expected but the Translate() method still makes the object fall down after a while. Why is that? Shouldn’t it translate the object upward against gravity each Update?

you could set isKinematic to true or set gravity to zero while holding the key…

Gravity is adding to the vertical movement (remember, according to Unity the object is falling). Eventually that force moves the object faster than it is being translated.

Oh so is Translate() “teleporting” the object each update upward by a constant amount? While the physics keeps accelerating it downward and adding to the objects downward velocity as if it is not affected by any upward force?

So if I set isKinematic to true then gravity won’t be adding to the velocity any more. I think I get it now.

Thank you guys.