Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Item Bounce in a Top-Down 2D Game?

Discussion in '2D' started by NoahMewes, May 6, 2023.

  1. NoahMewes

    NoahMewes

    Joined:
    May 9, 2017
    Posts:
    6
    Hi all,

    I am creating a topdown 2D and want items to have a nice little bounce when they spawn in. I found this article here that basically has the exact effect I want: https://yal.cc/top-down-bouncing-loot-effects/. HOWEVER, this article is for Gamemaker and not for Unity.

    I tried recreating his code as 1:1 as possible (Obviously making changes for Unity), but really could not get the desired effect. Instead, I more tried to apply the same concepts as in the article instead of trying to copy the code exactly. Here is what I came up with:

    Code (CSharp):
    1. public class ItemDropScript : MonoBehaviour
    2. {
    3.     float floorY;
    4.     Rigidbody2D rb;
    5.  
    6.     public int bounces = 5;
    7.  
    8.     public float speed = 10.0f;
    9.  
    10.     void Start()
    11.     {
    12.         rb = this.gameObject.GetComponent<Rigidbody2D>();
    13.         floorY = this.gameObject.transform.position.y;
    14.         this.gameObject.transform.position = new Vector3(this.gameObject.transform.position.x, floorY + 1, this.gameObject.transform.position.z);
    15.  
    16.  
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void FixedUpdate()
    21.     {
    22.  
    23.         if ((bounces > 0) && (this.gameObject.transform.position.y > floorY) && (speed > 0))
    24.         {
    25.             transform.Translate(Vector2.down * Time.deltaTime * speed);
    26.         }
    27.         else if (bounces > 0)
    28.         {
    29.             transform.Translate(Vector2.up * bounces * Time.deltaTime * speed);
    30.             bounces--;
    31.             speed = (float)(speed * 0.6 - 0.7);
    32.             print("bounces:" + bounces);
    33.         }
    34.         else
    35.         {
    36.             speed = 0;
    37.         }
    38.        
    39.     }
    40. }
    This code sort of gets the same effect, but is not nearly as smooth and appealing as the effect in the article. How can I go about recreating the bouncing effect in this article in Unity2D? I'm not attached to what I have now and am willing to completely re-write if needed.
     
  2. vonchor

    vonchor

    Joined:
    Jun 30, 2009
    Posts:
    249
    You might consider using DOTween, there’s a free version. Its handy for stuff like this.
     
  3. mrvictordiaz

    mrvictordiaz

    Joined:
    Aug 21, 2016
    Posts:
    12
    I am also a GM developer on the side. That article is often referenced when you're trying to introduce yourself with tweening in general. However it's not really practical to write the thing from scratch as there are already tons of premade libraries that can do that, and even offers a lot more customization, such as multiple tween modes, acceleration curves, etc.

    You may also want to check the source code in question and open it with GM. I think you misplaced some lines onto the wrong events, as GML is basically pseudo-code, there's hardly any language specific features there - it's almost like reading a raw inanimate algorithm.
     
  4. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    maybe you should post a video of what you have currently so we can see by how far off you are
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Don't write code for things that can simply be animations.

    And if you must write code (eg, dynamic behaviour), then as Vonchor points out, use a tweening package.

    More: