Skip to main content

On Sale: GamesAssetsToolsTabletopComics
Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Hi I need help

A topic by zoomzoom1001000 created Jan 06, 2023 Views: 128 Replies: 5
Viewing posts 1 to 4

im trying to make a flappy bird game where the game will quit i got that but i cant get the flappy bird script to work the player just falls can someone help me make it not fall thorugh the ground i have a colider rigid body PLS HELP! 

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class FlappyBirdScript : MonoBehaviour

{

    // Move speed for the player

    public float moveSpeed = 5f;

    // Upward force applied to the player when space bar is pressed

    public float upwardForce = 5f;

    // Gravity force applied to the player

    public float gravity = 9.8f;

    // Initial position of the player

    Vector3 initialPosition;

    // Rigidbody component of the player

    Rigidbody rb;

    // Initialization

    void Start()

    {

        // Store the initial position of the player

        initialPosition = transform.position;

        // Get the rigidbody component of the player

        rb = GetComponent<Rigidbody>();

    }

    // Update is called once per frame

    void Update()

    {

        // If the player has fallen below the ground, reset their position to the initial position

        if (transform.position.y < initialPosition.y)

        {

            transform.position = initialPosition;

            rb.velocity = Vector3.zero;

        }

        // Move the player to the right

        transform.position += Vector3.right * moveSpeed * Time.deltaTime;

        // Apply gravity to the player

        transform.position += Vector3.down * gravity * Time.deltaTime;

        // Check if the space bar was pressed

        if (Input.GetKeyDown(KeyCode.Space))

        {

            // Apply upward force to the player

            rb.velocity = Vector3.up * upwardForce;

        }

    }

} is the script btw

Submitted

It would be better to provide more details.

  • I assume you are making a 2D game, so first of all it should be Rigidbody2D not Rigidbody.
  • I understand the use of if (transform.position.y < initialPosition.y) but the player simply has no time to react at all before it tp back to initial position. You might want to do something like if (transform.position.y < initialPosition.y - 5)
  • I have no idea why do you want to simulate gravity when there's built in gravity for Rigidbody2D or Rigidbody already. And your way to apply gravity is in linear, not in exponential. If you want to know more about gravity check it on Internet.
  • rb.velocity according to my understanding is an acceleration, not a thrust boost. There's ForceMode.Impluse.

Not trying to be mean or whatsoever, but I think you should follow a step to step guide online.

Anyway gl on your entry

You know the trick to flappy bird games is make the pipes move left, not player move right

Submitted

Well the beauty of game development is that everyone have their own idea and create something unique. Maybe they have a special idea on their flappy bird game who knows?

(+1)

"the beauty of game development" wooooaaahhh

Yeah maybe I was just giving  a little help