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.