0

I'am trying to make a flappybird game and when i try to make the bird jump every frame it jumps once and then stops here is code

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

public class plzwork : MonoBehaviour
{
 
 public Rigidbody2D myRigidbody;
 // Start is called before the first frame update
 void Start()
 {
        
 }

 // Update is called once per frame
 void Update() 
 {
        
 myRigidbody.velocity = Vector2.up * 10;
 }
}
DevAm00
  • 369
  • 2
  • 10

3 Answers3

0

I just tried the same script that you have and it is just floating up every time.

My code is

 public Rigidbody2D rb;

// Update is called once per frame
void Update()
{
    rb.velocity = Vector2.up * 1;
    
}

My Circle is just floating slowly up.

It cant actually stop.

Or please describe your problem more detailed.

PlyfexHD
  • 13
  • 4
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 28 '23 at 03:26
0

I think what you are trying to do is :

When you receive input from the player(Depends on what platform you are targeting ) the bird goes up Then the bird goes down (Like
flappybird )

  • The mistake you did that You are calling this code in update which will only result in the bird going up forever.

  • you need to addforce for the bird so keep moving forward (You can do that in the update method) then when you get input from the player you addforce.up because the bird is going up

here is the code :

public class Bird : MonoBehaviour
{

    private Rigidbody2D birdRB;  // Pro Tip :try to not make 
    // your fields public do it only when needed , you can initialize 
    // the rigidbody in the start function

    private float jumpForce = 2;

    
    void Start()
    {
        birdRB = GetComponent<Rigidbody2D>(); 
    }

    
    void Update()
    {
        birdRB.AddForce(Vector2.right);

        if (Input.GetKeyDown(KeyCode.Space)) // this depends on the platform
        {
            
            birdRB.AddForce(Vector2.up * jumpForce);
        }
    }
}

also I suggest adding a 2D CinmaMachine that follows the bird to further improve your project .

DevAm00
  • 369
  • 2
  • 10
0

First add the RigidBody component to your object and check the (use Gravity) to be True. Then create a script on your object and open it. when you want to get the Rigidbody component on the same object it doesn't have to be public

enter code here

Rigidbody rigidBody;

private float jumpHieght = 5f;

private void Awake()
{
    rigidBody = GetComponent<Rigidbody>();  
}

private void Start()
{
    
}

private void Update()
{
    if(Input.GetKeyDown(KeyCode.Space))
    {
        rigidBody.velocity = Vector3.up * jumpHieght;
    }
}