0
\$\begingroup\$

Sometimes it works but sometimes it just doesn't. Maybe it has something to do with gravity or drag?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BirdScript : MonoBehaviour
{
    public Rigidbody2D myRigidbody;
    public float flapStrenght;

    // Start is called before the first frame update (plays ony once)
    void Start()
    {
        gameObject.name = "Roger";
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space) == true) 
        {
            myRigidbody.velocity = Vector2.up * flapStrenght;
        }
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ I don't see any obvious smoking gun in the code you've shared. This is more or less what I would write to achieve this effect, and I have not observed inconsistency from responding to keyboard input this way. It's possible the issue is coming from some other script in your project, or something in how you've configured this object that we're not able to see in this post. Can you walk us through a more complete version, or try making a test project with just this script and a default Rigidbody2D to see if the issue persists? \$\endgroup\$
    – DMGregory
    Commented Aug 21, 2024 at 22:17
  • \$\begingroup\$ It could also be a faulty keyboard. Have you tested the keyboard in (for example) Notepad to make sure it responds to every press? \$\endgroup\$
    – Paul
    Commented Aug 23, 2024 at 10:29

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.