简体   繁体   中英

How to improve movement code in Unity?

I am making a game in Unity using the 2D format which has the same mechanics as Flappy Bird. But, I fail to make the movement as good as I want it to be.

The main problem is that when I press the mouse button, it does not stop until I release the button. I want with one click just one movement, so that if I press the mouse button down and don't let go the bird will drop. Because, it will bounce just once.

Here is the movement code, please tell me how to fix this issue. I have skipped some parts of the code which are not used in the movement.

float flapSpeed = 25f;
float forwardSpeed = 0.9f;

void Update () {
      if  (Input.GetMouseButton(0)) { 
          didFlap = true;
      }
}

void FixedUpdate () {
// Movement
    GetComponent<Rigidbody2D>().AddForce(Vector2.right * forwardSpeed);
    if (didFlap) {
         GetComponent<Rigidbody2D>().AddForce(Vector2.up * flapSpeed);
         didFlap = false;
    }
}

It's because you are checking if the mouse button is pressed all the time and reacting to it:

void Update () {
      if  (Input.GetMouseButton(0)) { 
          didFlap = true;
      }
}

You need to keep an additional flag which will allow a flap only after the mouse button has been released. Something like:

private bool allowFlap = true;

void Update () {
      bool mouseDown = Input.GetMouseButton(0);
      if (allowFlap && mouseDown) { 
          didFlap = true;
          allowFlap = false;
      } else if (!allowFlap && !mouseDown) {
          allowFlap = true;
      }
}

This is all you need

You were calling GetMouseButton which runs once every frame the mouse button is down. Instead try calling GetMouseButtonDown which will fire once on each mouse down.

float flapSpeed = 25f;
float forwardSpeed = 0.9f;

void Update () {
     if (Input.GetMouseButtonDown(0)) 
          GetComponent<Rigidbody2D>().AddForce(Vector2.up * flapSpeed);
}

void FixedUpdate () {
      GetComponent<Rigidbody2D>().AddForce(Vector2.right * forwardSpeed);
}

If you want to use the same mechanics in Flappy Bird you need to work with the rigitbody.velocity instead of applying forces because it doesn't matter if the bird is going up, down or stopped, it will always fly up with the same initial speed.

private void HandleMouse ()
{   
    if (Input.GetMouseButtonDown(0)) {
        rigidbody.velocity = new Vector3 (rigidbody.velocity.x, velocityUp, 0f);
    }
}

To get the mouse button clicked once, use GetMouseButtonDown. Then you will need to apply an impulse instead of force, through the second param of AddForce(), ForceMode.Impulse:

void Update () {
    if (Input.GetMouseButtonDown(0)) 
        GetComponent<Rigidbody2D>().AddForce(Vector2.up * flapSpeed, ForceMode.Impulse);
}

You are checking Input.GetMouseButton(0) every frame. this function returns true if the player press and hold the button_0. what you want is to check Input.GetMouseButtonDown(0) which fires once.

by the way i have two advice for your code.

  1. store return value of Input.GetMouseButtonDown() in a local variable so you only check it once every frame (each update loop itteration).

  2. try to use GetComponent<>() on Start() function and store it in a
    global variable. because this function is not cost efficinet to be
    called every frame.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM