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 becoming extremely bouncy if held while falling

Discussion in '2D' started by Benzilla, Dec 13, 2014.

  1. Benzilla

    Benzilla

    Joined:
    Dec 13, 2014
    Posts:
    1
    Hello,
    I'm fairly new to Unity and am trying to make a drag and shoot system for a ball. At the moment I've added a physicsMaterial2D with a bounciness of 0.8 to the ball and just got it bouncing up and down on a surface. I've also made it so that if you click the ball, it stops where the mouse was.
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Drag : MonoBehaviour {
    6.  
    7.     private Vector2 clickPosition,cursorPosition;
    8.        
    9.     void OnMouseDown()
    10.     {
    11.         clickPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
    12.         gameObject.transform.position = clickPosition;          
    13.     }  
    14.     void OnMouseDrag()      
    15.     {          
    16.         cursorPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
    17.         gameObject.transform.position = clickPosition;
    18.     }  
    19.     void OnMouseUp()      
    20.     {
    21.                
    22.     }
    23. }
    The problem is, when the ball is on the falling part of it's cycle, and I hold down the mouse for a long time then release- the ball speeds up massively. I'm completely stumped!

    Here's a YT of the problem (no idea why it sped up):
     
  2. micael-dias

    micael-dias

    Joined:
    Nov 8, 2013
    Posts:
    4
    Gravity keeps accelerating (changing the ball's speed) even though you "grabbed" it. You're not actually stopping the ball, you're only forcing it's position to change.
    You can fix that by keep setting the ball's RigidBody2D velocity to zero on OnMouseDrag.