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 Simulating friction on a floating object

Discussion in 'Scripting' started by questforben, Jun 30, 2022.

  1. questforben

    questforben

    Joined:
    May 17, 2022
    Posts:
    3
    I'm trying to make a simple game where the player just launches a ball at targets, and I thought it would add some fun to the movement for the ball to hover and push away from the nearest objects. I have this working, however the ball predictably doesn't exactly slow down or stop. Is there a good way to simulate friction as the ball slows down?
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    Do you mean like Air Friction? RigidBodies already have an Air Drag variable which will make them stop floating indefinitely.
     
  3. questforben

    questforben

    Joined:
    May 17, 2022
    Posts:
    3
    Well I only want it to affect movement in the x and y values. If I can choose which axis to affect on air drag and change air drag from the script then that would be a good choice. I can't see anything about that specifically on the Unity docs though
     
  4. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    Well, it's easy enough to simulate, you just need to dampen the velocity every FixedUpdate frame.
     
  5. questforben

    questforben

    Joined:
    May 17, 2022
    Posts:
    3
    Is there any good documentation on dampening velocity? I can't find anything that I can really understand super well atm. Like I get the concept, but I can't really reproduce the math
     
  6. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    You can just do something like
    rigidBody.velocity = rigidBody.velocity * 0.9f;
    and it will eventually get to something close to 0.