Unity Flappy Bird: Spacebar Jump Script
Here's a script you can use in Unity to make your Flappy Bird jump every time the space bar is pressed:
using UnityEngine;
public class BirdController : MonoBehaviour
{
public float jumpForce = 5f; // The force applied when the bird jumps
private Rigidbody2D rb;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
Jump();
}
}
private void Jump()
{
rb.velocity = Vector2.up * jumpForce;
}
}
To use the script, follow these steps:
- Attach the script to your bird GameObject in Unity.
- Make sure you have a Rigidbody2D component attached to your bird GameObject.
- Set the desired jump force value in the script inspector.
- Run the game and press the space bar to make the bird jump.
This script checks for the space bar press in the 'Update' method using 'Input.GetKeyDown(KeyCode.Space)'. If the space bar is pressed, it calls the 'Jump' method, which applies an upward force to the bird's Rigidbody2D component, making it jump.
原文地址: https://www.cveoy.top/t/topic/pOb0 著作权归作者所有。请勿转载和采集!