Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by 82MiddleMan · Feb 29, 2016 at 01:55 AM · 2d2d-platformerrigidbody2d2d-physicsslowmotion

How can I slow the movement of a single rigidbody2D?

I have objects in my game which you can throw around. They all use rigidbody2D. I want to be able to freeze them, and then when you throw one that's frozen, it will move along exactly the same path as normal only slower. To throw the object I use addForce from the player script. Ideally I want to be able to tell it what speed to go, so 0.5 would make it go half speed.

I have found a few questions along these lines but no definitive answer. I have tried playing around with the mass, drag, gravity etc. but can't get it to behave like I want it to.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by 82MiddleMan · Mar 02, 2016 at 04:32 PM

This is the script I went with in the end. It's a heavily modified version of the answer to an older similar question. It's not quite perfect but good enough for what I need. In the inspector, set your time scale before running (0.5 being half speed etc.), then tick the 'Frozen' box to go at that speed. Un-tick it and it will continue on the same path at normal speed. It should work with any object using Rigidbody2D.

 using UnityEngine;
 using System.Collections;
 
 public class SlowMoScript : MonoBehaviour {
 
     public float timeScale = 0.5f;
     public bool frozen = false;
 
     Rigidbody2D rb2d;
     float startMass;
     float startGravityScale;
 
     bool slowMoBool = true;
 
     void Awake(){
         rb2d = GetComponent<Rigidbody2D> ();
 
         startGravityScale = rb2d.gravityScale;
         startMass = rb2d.mass;
     }
 
     void Update() {
         if (frozen) {
             SlowMo ();
         } else {
             StopSlowMo ();
         }
     }
 
     void SlowMo () {
         rb2d.gravityScale = 0;
 
         if (slowMoBool) {
             rb2d.mass /= timeScale;
             rb2d.velocity *= timeScale;
             rb2d.angularVelocity *= timeScale;
         }
         slowMoBool = false;
 
         float dt = Time.fixedDeltaTime * timeScale;
         rb2d.velocity += Physics2D.gravity  / rb2d.mass * dt;        
     }
 
     void StopSlowMo() {
         if (!slowMoBool) {
             rb2d.gravityScale = startGravityScale;
             rb2d.mass = startMass;
 
             rb2d.velocity /= timeScale;
             rb2d.angularVelocity /= timeScale;
         }
         slowMoBool = true;
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unity_EfGoW0qPNcl2RA · Feb 27 at 02:51 PM 0
Share

Works perfect. But only if you change Update to FixedUpdate because we use fixedDeltaTime in SlowMo.

avatar image
0

Answer by Masterio · Feb 29, 2016 at 07:36 AM

You can save the rigidbody.velocity in an variable: oldVelocity = rigidbody.velocity; Next sets the: rigidbody.velocity = Vector3.zero; Next after some time, assign again old velocity to the rigidbody: rigidbody.velocity = oldVelocity * slowDownValue;

Press the 'start' field to start the move, press it again to stop it.

 using UnityEngine;
 using System.Collections;
 
 public class RigidbodyStopper : MonoBehaviour 
 {
     public Vector3 startVelocity = Vector3.zero;
     public float slowDownMod = 1f;
     public bool start = false;
 
     private Rigidbody _rigidbody = null;
     private Vector3 _oldVelocity;
 
     private bool _temp = true;
 
     void Start () 
     {
         _rigidbody = GetComponent<Rigidbody>();
         _rigidbody.velocity = startVelocity;
         Move(false);
     }
 
     void Update () 
     {
         if(start)
         {
             Move(_temp);
             _temp = !_temp;
             start = false;
         }
     }
 
     public void Move(bool start)
     {
         if(start)
         {
             _rigidbody.velocity = _oldVelocity * slowDownMod;
             _rigidbody.isKinematic = false;
         }
         else
         {
             _oldVelocity = _rigidbody.velocity;
             _rigidbody.velocity = Vector3.zero;
             _rigidbody.isKinematic = true;
         }
     }
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image 82MiddleMan · Feb 29, 2016 at 12:41 PM 0
Share

This didn't work. I had already tried something similar and it did the same thing. after a few frames it stops, then slowly moves towards the ground. I need it to some how know what the velocity would be normally at that point, then * 0.5. Even then I don't think it would take the same path.

avatar image Masterio 82MiddleMan · Feb 29, 2016 at 09:00 PM 0
Share

Check new code, it works for me well.

avatar image 82MiddleMan Masterio · Mar 02, 2016 at 04:37 PM 0
Share

Thank you for your help on this @$$anonymous$$asterio. I did try your updated script but I think some other elements in my game stopped it working properly. The solution I went with seems to do the job nicely so I've gone with that.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

62 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How Do I make my character move? 0 Answers

How to move rigidbodies with 2D Character controller (No rigidbody) 1 Answer

Player controlled platforms 0 Answers

Why/How 2d tower of blocks collapse? 0 Answers

[2D] Rigid body snaps through floor and back 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges