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

How to slow rigidbody?

Discussion in 'Scripting' started by caseyboundless, Jul 15, 2013.

  1. caseyboundless

    caseyboundless

    Joined:
    Oct 17, 2011
    Posts:
    590
    I'm trying slow my ball down. Sort of like a slow motion ball.

    Code (csharp):
    1. #pragma strict
    2. private var mainGameScript  : MainGame;
    3. var particles_splash        : GameObject;
    4. var ballSpawn       : GameObject;
    5.  
    6. function Awake()
    7. {
    8.     rigidbody.AddForce(0,8,0, ForceMode.Impulse);
    9.     InvokeRepeating("IncreaseBallVelocity", 5, 5);
    10. }
    11.  
    12.  
    13. function Start()
    14. {
    15.     mainGameScript = GameObject.Find("GeneralScripts").GetComponent(MainGame);  //Find main game script
    16.      Debug.Log(rigidbody.velocity.y);
    17. }
    18.  
    19.  
    20. function Update()
    21. {
    22.     Debug.Log("y" + rigidbody.velocity.y);
    23.     Debug.Log("x" + rigidbody.velocity.x);
    24.     if(transform.position.x >= 3.658267)
    25.     {
    26.         Instantiate(ballSpawn, Vector3(-0.03351761, -3, 0), transform.rotation);
    27.        
    28.         Debug.Log(transform.position.x);
    29.         audio.Play();
    30.        
    31.         if(this.gameObject)
    32.         {
    33.             Destroy(this.gameObject);
    34.         }
    35.     }
    36.    
    37.  
    38.    
    39.    
    40.    
    41.     if(transform.position.x <= -3.667952)
    42.     {
    43.         Instantiate(ballSpawn, Vector3(-0.03351761, -3, 0), transform.rotation);
    44.        
    45.         Debug.Log(transform.position.x);
    46.         audio.Play();
    47.        
    48.         if(this.gameObject)
    49.         {
    50.             Destroy(this.gameObject);
    51.         }
    52.     }
    53.    
    54.    
    55.    
    56.     if(transform.position.y >=5.684058 )
    57.     {
    58.         Instantiate(ballSpawn, Vector3(-0.03351761, -3, 0), transform.rotation);
    59.        
    60.         Debug.Log(transform.position.x);
    61.         audio.Play();
    62.        
    63.         if(this.gameObject)
    64.         {
    65.             Destroy(this.gameObject);
    66.         }
    67.     }
    68.    
    69.    
    70.    
    71.     if(transform.position.y <= -8)
    72.     {
    73.         mainGameScript.SubtractLife();
    74.         audio.Play();
    75.        
    76.         if(this.gameObject)
    77.         {
    78.             Destroy(this.gameObject);
    79.         }
    80.     }
    81. }
    82.  
    83.  
    84. function IncreaseBallVelocity()
    85. {
    86.     rigidbody.velocity *= 1.08;
    87. }
    88.  
    89.  
    90. function OnCollisionEnter(collision : Collision)
    91. {
    92.     Instantiate(particles_splash,transform.position, transform.rotation);
    93. }
    94.    
    95.    
    96. function OnTriggerEnter(other : Collider)
    97. {
    98.  
    99.  
    100.   if(other.gameObject.tag == ("Slow"))
    101.   {
    102.  
    103.         Instantiate(particles_splash,transform.position, transform.rotation);
    104.    
    105.     Slow();
    106.  
    107.   }
    108.  
    109.  }
    110.  
    111.  function Slow()
    112.  
    113.  
    114.  
    115.  {
    116.  
    117.   rigidbody.AddForce(-100,-100,0, ForceMode.Impulse);
    118.  }
    119.  
    120.  
     
  2. userTrav

    userTrav

    Joined:
    Jun 28, 2013
    Posts:
    37
    Increasing the drag over time could work, but it will change the flight path.
     
  3. Lachee1

    Lachee1

    Joined:
    Aug 21, 2012
    Posts:
    16
    A slow motion effect can be created by slow unity's time down like so:

    Code (csharp):
    1.     // Toggles the time scale between 1 and 0.7
    2.     // whenever the user hits the Fire1 button.
    3.     function Update () {
    4.         if (Input.GetButtonDown ("Fire1")) {
    5.             if (Time.timeScale == 1.0)
    6.                 Time.timeScale = 0.7;
    7.             else
    8.                 Time.timeScale = 1.0;
    9.             // Adjust fixed delta time according to timescale
    10.             // The fixed delta time will now be 0.02 frames per real-time second
    11.             Time.fixedDeltaTime = 0.02 * Time.timeScale;
    12.         }
    13.     }
    Source: http://docs.unity3d.com/Documentation/ScriptReference/Time-timeScale.html

    This however will slow ALL rigidbodies in the scene, so probably not ideal if you only want one body to slow.
     
  4. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    you can also multiply the velocity by 0.99f (or more..err less?), which is similar to drag.

    Depends on what kind of effect you are trying to achieve (slow motion, or slow down)
     
  5. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,796
    You should never directly modify the value of the velocity, in a physics system this can lead to unintended behaviour.
    A more realistic approach is to apply change from a 'physics' perspective, i.e increase the drag, or create an opposing force on the rigidbody to slow it down.
     
  6. caseyboundless

    caseyboundless

    Joined:
    Oct 17, 2011
    Posts:
    590
    thanks all i went with drag, waited a few seconds, then changed the speed back to normal.
     
  7. shaderop

    shaderop

    Joined:
    Nov 24, 2010
    Posts:
    942
    Are you sure about that? While I agree that moving a rigidbody by modifying the velocity directly should be the option of last resort because it can lead to things going haywire, I wouldn't say that it should never be done. I have done it on a couple of occasions before without any issues.
     
  8. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,796
    Well you should never, but you still can. I would call it more of a 'best practice' scenario.
    You should never build up your SQL statements with strings and variables without parameterising them, but you still can.. and you will usually get away it it.
     
  9. Instability

    Instability

    Joined:
    Apr 16, 2012
    Posts:
    288
    There is no need to modify the velocity directly. You can just find out the velocity vector v = rigidnbody.velocity. Then apply rigidbody.addforce(-v/v.magnitude * (v-v0).magnitude * dragCoefficient, ForceMode.Force). Drag is essentially a velocity-dependent force, so if you apply that code in FixedUpdate it should slow down the rigidbody to velocity v0.
     
  10. shaderop

    shaderop

    Joined:
    Nov 24, 2010
    Posts:
    942
    I think that comparing the security risk of string concatenation in SQL statement to possible physics glitches is mixing explosive apples with bouncy oranges :).

    That's a great looking game in your signature by the way.
     
  11. 12doze12

    12doze12

    Joined:
    Mar 17, 2013
    Posts:
    57
    A good way would be to apply a force to the ball in th direction of the surface it is on, kinda like brakes in a car.

    Something like this:

    Code (csharp):
    1. rigidbody.AddForce(Vector3.down * 10)
     
  12. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    There are situations where modifying the velocity directly is the best solution.

    Even Unity's jump example does it. http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody-velocity.html

    I generally use opposite forces, but I really cbf explaining how to do that here.
     
  13. caseyboundless

    caseyboundless

    Joined:
    Oct 17, 2011
    Posts:
    590
    My ball keeps getting stuck going left to right. Is there a way where the ball will always go somewhere new instead of staying with point a and point b. it starts off fine but sometimes does this. Ball is bouncing on bricks like brick breaker on x and y axis.