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

Question How to reduce "bounciness" on this hover effect?

Discussion in '2D' started by foxkingg, Jun 16, 2020.

  1. foxkingg

    foxkingg

    Joined:
    Dec 4, 2017
    Posts:
    8
    Code (CSharp):
    1. void Update()
    2.     {
    3.         if (grounded)
    4.         {
    5.             playerRB.velocity = new Vector2(playerRB.velocity.x, force);
    6.         }
    7.     }
    8.  
    9.     private void OnTriggerEnter2D(Collider2D collision)
    10.     {
    11.         if(collision.tag == "Ground")
    12.         {
    13.             grounded = true;
    14.         }
    15.     }
    16.  
    17.     private void OnTriggerExit2D(Collider2D collision)
    18.     {
    19.         if(collision.tag == "Ground")
    20.         {
    21.             grounded = false;
    22.         }
    23.     }
    This code is for a script attached to an object with a tiny Box Collider 2D, which is the child of another object with a Rigidbody 2D. The former object is positioned below the parent object. The desired effect is a smooth hover of the parent object. What I don't like about this one is that it moves (or "bounces") too much. I know this code is simple. I tried multiple things, but I have discarded all of my attempts and decided to post the initial, simplistic one here.

    tl;dr: want smoother movement
     
  2. foxkingg

    foxkingg

    Joined:
    Dec 4, 2017
    Posts:
    8
    Update: I think I'll use Raycasts after all to achieve the hover effect. I was trying to find an alternative, but i think Raycasting might be simpler after all.
     
    Last edited: Jun 16, 2020