Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Bird jumping physics like a flappy bird game.

Discussion in 'Scripting' started by Paximaz, Mar 3, 2014.

  1. Paximaz

    Paximaz

    Joined:
    Feb 14, 2014
    Posts:
    37
    I am trying to make a bird game but it doesn't copy flappy bird at all but I just liked the physics where when the bird goes up the sprite rotates up so it looks upwards and when it falls down the bird rotates downwards. I originally was going to have 3 sprites but it would look very twitchy. So the basic idea in my mind is change the rotation of the plane depending on the velocity of the bird.

    If anyone could help I would be very grateful.
     
  2. DormanSh

    DormanSh

    Joined:
    Sep 11, 2012
    Posts:
    31
    For 2D movement:
    Code (csharp):
    1. float angle = Vector3.Angle(Vector3.right, rigidbody.velocity);
    2. if (rigidbody.velocity.y < 0)
    3.     angle = -angle;
    4.  
    5. transform.eulerAngles = new Vector3(0, 0, angle);
    And yeah, this is just one of many ways.) Play with code to suit your needs, for example - try to make it work without "angle = -angle" line. I don't like it, but it works. )
     
    Last edited: Mar 3, 2014
  3. Paximaz

    Paximaz

    Joined:
    Feb 14, 2014
    Posts:
    37
    For reason I changed the script to my setting and put a var angle : float; When I run the game it doesn't rotate on the z-axis it only rotates around the y-axis for some reason. I went over and over my script but I can't seem to understand why it would rotate on the y axis.

    Code (csharp):
    1. angle = Vector3.Angle(Vector3.right, rigidbody.velocity);
    2.  
    3.     if (rigidbody.velocity.y < 0) {
    4.  
    5.     angle = -angle;
    6.  
    7.     }
    8.  
    9. transform.eulerAngles = new Vector3(90, 180, angle);
    10.  
    EDIT : I figured out why it was doing it but I don't know how to fix it now. For some reason with my plane the rotations change after it goes past the 90*'s and changes the y rotation to 270 from 90 and z rotation from 90 to -90. The rotation is working fine but I just can't figure out how to fix this rotation issue. I was thinking of putting it on a 3d gameobject like I did the clouds. I haven't had any problems with that.
     
    Last edited: Mar 4, 2014
  4. DormanSh

    DormanSh

    Joined:
    Sep 11, 2012
    Posts:
    31
    You got a Gimbal Lock, look for some videos on YouTube for explanation. Long story short - Euler angles applied to body axis-by-axis, and sometimes one axis compensate another. This is why Quaternions even exists. ) Simple solution will be to parent your object with needed fixed rotation to object with zero rotation, and apply your Z rotation to that "clean" parent object. Or mess with quaternions. ) Or - another way - normalise your models and sprites to Unity coordinate system.
     
  5. davidhernandez

    davidhernandez

    Joined:
    Jun 2, 2017
    Posts:
    1
    I achieve a similar effect using this code:

    Code (CSharp):
    1.         float rotation = rigidBody.velocity.y * 0.01f;
    2.         if (transform.rotation.z > 0.2f && rotation > 0) rotation = 0;
    3.         else if (transform.rotation.z < -0.2f && rotation < 0) rotation = 0;
    4.         transform.Rotate(0, 0, rotation , Space.Self);  
     
  6. SebLors

    SebLors

    Joined:
    Feb 13, 2021
    Posts:
    1
    For some reason, in my case, it's not letting the "angle" enter the if() condition, what could possibly be doing that? Here's my code:

    Code (CSharp):
    1. rigidbody.gravityScale = 1;
    2.      
    3.  
    4. if (Input.GetMouseButtonDown(0) && currentClicks < allowedClicks) {
    5.             rigidbody.velocity = Vector2.up * speed;
    6.  
    7.             Debug.Log(angle);
    8.             //Angle-RotationMovement
    9.             if (rigidbody.velocity.y < 0) {
    10.                 Debug.Log(angle);
    11.                 angle = -angle;
    12.             }
    13.             transform.eulerAngles = new Vector3(0, 0, angle);
    14.             Debug.Log(angle);
    15.             currentClicks++;
    16.         }
    NOTE: Ignore the "currentClicks" part, since that's part of a broader build I'm doing which limits the amount of clicks a player has for the gameplay.
     
unityunity