Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

jump flappy bird sort of game

Discussion in 'Scripting' started by cybertree, Aug 19, 2015.

  1. cybertree

    cybertree

    Joined:
    Jun 19, 2015
    Posts:
    100
    Hi. Im making a flappy bird sort of game but when my bird(a cube) crashes to the floor it bounces up and down. Also,I can only jump once. Please tell me what is wrong with this script
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class mainscript : MonoBehaviour {
    5.  
    6.  
    7.  
    8.     private int startedjump;
    9.     private float ywhenstarted;
    10.  
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.    
    15.  
    16.  
    17.     }
    18.  
    19.  
    20.     // Update is called once per frame
    21.     void Update () {
    22.  
    23.         if (Input.GetMouseButtonDown (0)) {
    24.  
    25.             ywhenstarted = transform.position.y;
    26.             startedjump = 1;
    27.         }
    28.  
    29.         if (startedjump == 1) {
    30.  
    31.             if (transform.position.y >= ywhenstarted + 10) {
    32.                
    33.                 startedjump = 0;
    34.                
    35.             }
    36.  
    37.         }
    38.  
    39.     }
    40.  
    41.  
    42.     void FixedUpdate () {
    43.  
    44.         transform.position = new Vector2 (transform.position.x + 0.25f, transform.position.y);
    45.  
    46.         if (startedjump == 1) {
    47.  
    48.             transform.position = new Vector2 (transform.position.x, transform.position.y + 0.25f);
    49.  
    50.         }
    51.  
    52.     }
    53.    
    54. }
    ‼️
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    Hey neat — I was helping some middle schoolers last year, and one of them made a "Flappy Cube" project too!

    But he did it with physics, which I think is going to be easier than what you're trying to do here. You're directly moving the object via its transform, which is also fine, but it means you have to implement all the physics yourself. That's not super hard in this case, but if you don't feel you have a good grasp of F = ma, you might want to let Unity handle it for you.

    I don't see anything in this script that would cause your block to ever move down. You only ever move it forward, and sometimes up. So if it's doing anything else, then you do have physics on it, and now your code is fighting the physics engine (both of you trying to control the transform position), which is certainly going to cause all sorts of problems.

    So, look at Rigidbody.MovePosition for the continuous forward movement, and maybe set the velocity to do a jump.
     
    cybertree likes this.
  3. cybertree

    cybertree

    Joined:
    Jun 19, 2015
    Posts:
    100
    I have phyisics for the gravity
     
  4. cybertree

    cybertree

    Joined:
    Jun 19, 2015
    Posts:
    100
    and please show a simple example
     
  5. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Those are simple examples- check the bottom of each page. The text description explains what they do, and the code shows how to use it. If you need a full tutorial on using rigidbodies, you should definitely check out this. While you're at it, watch everything you can stomach in the Learn section- the videos are very informative.
     
    cybertree and JoeStrout like this.
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    And, in case it wasn't perfectly clear, if you're using physics for the gravity you need to use physics for everything. Do not assign to transform.position when using physics. Doing so will only lead to suffering and grief.
     
    cybertree likes this.
  7. cybertree

    cybertree

    Joined:
    Jun 19, 2015
    Posts:
    100
    ok thanks a lot again everyone,this community is being so useful
     
    JoeStrout likes this.