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

No slippery Rigidbodies2D

Discussion in 'Physics' started by udham, Aug 21, 2018.

  1. udham

    udham

    Joined:
    Mar 2, 2018
    Posts:
    7
    Hi,

    I'm making an Abacus game, I'm using following code on each roller-ball to move the ball along.
    I have placed to stopping colliders on either side to stop the balls, balls have box colliders and are rigidbodies too.
    However the colliders don't stop the ball immediately and the ball kind of gets on the area where a collider is, and then elastically gets thrown out.

    The balls also get overlapped wth each other

    How can I make the balls and stopping colliders to be really rigid?

    Code (CSharp):
    1.     void OnMouseDown()
    2.     {
    3.         //gameObject.GetComponent<SpriteRenderer> ().sortingOrder = 25;
    4.         initial = gameObject.transform.position;
    5.         screenPoint = Camera.main.WorldToScreenPoint (gameObject.transform.position);
    6.         offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, screenPoint.y, screenPoint.z)) + new Vector3 (0, 0, 0);
    7.  
    8.     }
    9.  
    10.     void OnMouseDrag()
    11.     {
    12.         Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, screenPoint.y, screenPoint.z);
    13.         Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
    14.         gameObject.transform.position = curPosition;
    15.  
    16.     }
     
  2. udham

    udham

    Joined:
    Mar 2, 2018
    Posts:
    7
    I cannot believe it.
    replacing this line

    gameObject.transform.position = curPosition;

    with this line of code solved all my problems. :D

    rigidbody.MovePosition(curPosition)
     
    Deeeds likes this.