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

2D Ball physics?

Discussion in 'Physics' started by Nestik, Jun 26, 2015.

  1. Nestik

    Nestik

    Joined:
    Jun 26, 2015
    Posts:
    1
    My apologies for the vague title.

    I'm working on a concept with very little C#/programming knowledge. I'm looking to have a 2D Ball that move indefinitely unless certain triggers are met. It bounces off of objects to gain speed and "rolling" affects it's curvature when traveling between contact points.

    I've got the ball moving, I've got the ball bouncing. My current issue is the ball always slows to a stop completely regardless of my current settings and if I change anything by a decimal of a fraction the results go wildly out of control (IE: the ball is too bouncy, it goes between walls, speeds up with no stop, and eventually breaks the collision boxes and flies out of the world boundaries. Funny to watch, not good for my game mechanic).

    I suppose the simplest way to solve this problem would be to set a minimum speed so it doesn't slow down and possibly a maximum speed to the player object itself but I am unaware of how to achieve this via C# (I'm sure this is simple I just lack the experience...)

    I have a few other "problems" that have me stumped but I cannot explain these without screenshots of sorts and am away from my build at the moment.

    Any help is very much appreciated!
     
  2. Aldo

    Aldo

    Joined:
    Aug 10, 2012
    Posts:
    173
    ok you store the rigidbody

    Code (CSharp):
    1. Rigidbody2D rbody = gameObject.GetComponent<Rigidbody2D>()
    2.  
    3. if(rbody.velocity > maxVelocity)
    4. rbody.velocity = rbody.velocity.normalized* maxVelocity;
    5.  
    6. //same for min velocity
    7.  
    8.  
    9. if(rbody.velocity < minVelocity)
    10. rbody.velocity = rbody.velocity.normalized* minVelocity;
     
  3. dylanjosh

    dylanjosh

    Joined:
    Jun 1, 2015
    Posts:
    10
    Try setting rigidbody drag to zero