Newbie having trouble with Rigidbody2D.AddForce

Guys, I’m just starting out so I’m sure I’ve done something wrong, but I’m trying to make a simple flappy bird clone to learn some Unity Basics. I have done a few short tutorials, but I learn best by messing around and doing.

I have an object called “Birdy” which I have applied the RigidBody 2D and Box Collider components to, as well as created a new cs script called Birdy.cs and applied to it.

At the moment I just want pressing the A key on the keyboard to apply a force upwards to the bird. This is the code I have:

public class Birdy : MonoBehaviour
{
public Rigidbody2D birdBody;
// Start is called before the first frame update
void Start()
{

}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.A))
{
birdBody.AddForce(Vector2.up * 100F, ForceMode2D.Impulse);
}
}
void Awake()
{
birdBody = GetComponent<Rigidbody2D>();
}
}

For some reason this doesn’t work. I have gravity active on the bird, so it just drops down off the screen, and pressing the A key doesn’t do anything.

Please help.

OK nevermind, I am a moron. I hadn’t linked my script file to the object. :rage:

Note that you’ll add this impulse per-frame so if the frame-rate goes faster, it’ll add more, same for slower frame-rate. Also, physics by default doesn’t run per-frame unless you change it in the physics 2D settings, it runs during the fixed-update. There’s plenty of info about this online so I won’t go into it further here.

Also, so you know, here’s how to post code on the forums: Using code tags properly