title: Using RigidBody2D in Unity
body: <p>My buddy and I recently teamed up to create a top-view space shooting game, like asteroids. We’ve watched a lot of tutorials, and we’ve managed to work with <code>translate.Transform()</code> and get our object to move.</p>
  <p>Now we want to apply force to our ship using <code>RigidBody2D</code>:</p>
  <p>Here is a screenshot of our Unity window:</p>
  <p><a href="https://i.stack.imgur.com/NmU6U.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NmU6U.jpg" alt="Screenshot of Unity window" /></a></p>
  <p>As seen in the screenshot above, we made a Player Ship prefab with a spaceship sprite, a circle collider, a rigidbody 2D and a PlayerThrust script.</p>
  <p>In <code>Thrust.cs</code>:</p>
  <pre class="lang-cs prettyprint-override"><code>using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class Thrust : MonoBehaviour
  {
      // Thrust script components
      public  Rigidbody2D rigidBody;     // GameObject's Rigidbody2D component
      public  float       thrustAmount;  // amount of thrust applied to be defined in unity
      private float       thrustInput;   // float variable to keep track of user input of the 'up' arrow key/'up' axis tilt on joystick
  
      // Start is called before the first frame update
      void Start() {}
  
      // Update is called once per frame
      void Update() 
      {
          // handle input
          thrustInput = Input.GetAxis(&quot;Vertical&quot;);  // float ranging from -1.0 to +1.0
  
          // Debug
          Debug.Log(Vector2.up * thrustInput * thrustAmount);
      }
  
      // Fixed Update is called every fixed framerate frame
      // use for physics
      void FixedUpdate() {
          // only accelerate forward
          if (thrustInput &gt; 0) 
          {
              // Apply thrust in direction ship is facing
              rigidBody.AddRelativeForce(transform.up * thrustInput * thrustAmount);
          }
      }
  }
  </code></pre>
  <p>Why doesn’t the ship move?</p>
  <p><code>Debug.Log(Vector2.up * thrustInput * thrustAmount);</code> outputs a number which makes sense, it goes up to the thrust amount when the <kbd>↑</kbd> button is pressed, and goes back to 0.0 when the button is released.</p>