Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Gravity Always Wins?

Discussion in 'Scripting' started by Numberslayer, Jun 14, 2014.

  1. Numberslayer

    Numberslayer

    Joined:
    Jun 14, 2014
    Posts:
    7
    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.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Test : MonoBehaviour {
    5.     public int speed = 5;
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.     }
    10.  
    11.     // Update is called once per frame
    12.     void FixedUpdate () {
    13.         if(Input.GetKey(KeyCode.RightArrow)){
    14.             rigidbody2D.AddForce(new Vector2(speed, 0));
    15.         }
    16.  
    17.         if(Input.GetKey(KeyCode.LeftArrow)){
    18.             rigidbody2D.AddForce(new Vector2(-speed, 0));
    19.         }
    20.  
    21.         if(Input.GetKey(KeyCode.UpArrow)){
    22.             this.transform.Translate(new Vector2(0, speed*(float)0.04));
    23.         }
    24.  
    25.         if(Input.GetKey(KeyCode.D)){
    26.             this.transform.Translate(Vector2.right * speed * Time.deltaTime);
    27.         }
    28.  
    29.         if(Input.GetKey(KeyCode.A)){
    30.             this.transform.Translate(Vector2.right * -speed * Time.deltaTime);
    31.         }
    32.  
    33.         if(Input.GetKey(KeyCode.Space)){
    34.             this.transform.Translate(Vector2.up * speed * 2 * Time.deltaTime);
    35.         }
    36.     }
    37. }
     
  2. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    i think your are overlooking what you're doing, try like you said; rigidbody2D.AddForce ;-)
    or use Vector2.up if you stick with translate.
    Code (CSharp):
    1. if(Input.GetKey(KeyCode.UpArrow)){
    2.             this.transform.Translate(new Vector2(0, speed*(float)0.04));
    3.         }
     
  3. Numberslayer

    Numberslayer

    Joined:
    Jun 14, 2014
    Posts:
    7
    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?
     
  4. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    you could set isKinematic to true or set gravity to zero while holding the key...
     
  5. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    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.
     
  6. Numberslayer

    Numberslayer

    Joined:
    Jun 14, 2014
    Posts:
    7
    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.