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. Dismiss Notice

How to create multiple timescales to different rigidbodies?

Discussion in 'Scripting' started by zee_ola05, Oct 15, 2014.

  1. zee_ola05

    zee_ola05

    Joined:
    Feb 2, 2014
    Posts:
    166
    Example Scenario:
    I have 3 spheres falling down via gravity. They have same rigidbody data.
    I want to make a script that would control the "timeScale" of these rigidbodies. How do I do that?

    Big picture: I want to create a system that could control the time of different groups of objects.
    Example: Group A has 1f timeScale, Group B has 0.5f timeScale, Group C has 0f timeScale (paused).
     
  2. nysh

    nysh

    Joined:
    Oct 15, 2014
    Posts:
    11
    you cant, that just sounds wrong in more then one ways, timescale is in respect to your scene not individual gameobjects.
     
  3. zee_ola05

    zee_ola05

    Joined:
    Feb 2, 2014
    Posts:
    166
    That's why I'm asking this. I don't need to use Time.timeScale per se.

    Say, if I create my own variable "timeScale", how do I make my rigidbody be affected by my timeScale?
     
  4. Kirlim

    Kirlim

    Joined:
    Aug 6, 2012
    Posts:
    126
    You an add a force multiplied by your time scale every FixedUpdate, simulating your own gravity.
     
  5. nysh

    nysh

    Joined:
    Oct 15, 2014
    Posts:
    11
    multiple that to deltaTime.

    relativeDeltaTime=timeScale*Time.deltaTime
     
  6. djfunkey

    djfunkey

    Joined:
    Jul 16, 2012
    Posts:
    201
    Just question regarding this, and this might help him in the long run....
    How would you "slow down time" for a certain object while and after it has been on a ramp and is now airborne?
    Would it just be counteracting the gravitational force and the vector 3 forward of the object respectively?
     
  7. zee_ola05

    zee_ola05

    Joined:
    Feb 2, 2014
    Posts:
    166
    I don't understand. Wouldn't it be just the same as how you handle it with x1 timeScale? What you're actually doing is supplying it with deltaTimes, but you're not gonna be changing how physics is done.
     
  8. djfunkey

    djfunkey

    Joined:
    Jul 16, 2012
    Posts:
    201
    Basically the function for time.Timescale is this:
    Code (csharp):
    1. public float Timescale (float timefloat) {
    2.    game.everythingRunsAt = timefloat;
    3.    return timefloat;
    4. }
    So essentially you cannot use timescale to change each one individually.
    No what they meant is that you change how physics moves your object. so e.g.
    rigidbody.AddForce (Vector3.up * 10);

    Try this to get you on the right track: http://unity3d.com/learn/tutorials/modules/beginner/physics/addforce
     
  9. zee_ola05

    zee_ola05

    Joined:
    Feb 2, 2014
    Posts:
    166
    @djfunkey that reply was actually for you. I'm aware of what Time.timeScale and AddForce do.

    Maybe my question was unclear. Let me revise it: "How can I create a system that could group object and control their time?"
     
  10. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,381
    You can't.

    They're telling you that you need to multiply all of the physics inputs by that object's time differential. Per object.
     
    djfunkey likes this.
  11. zee_ola05

    zee_ola05

    Joined:
    Feb 2, 2014
    Posts:
    166
  12. wccrawford

    wccrawford

    Joined:
    Sep 30, 2011
    Posts:
    2,039
    Because you can't "control time" on individual objects.

    Instead, what you can do is use a scale for each one and have each object apply that scale when it's doing time-sensitive things. Instead of "movement * Time.deltaTime" it's "movement * Time.deltaTime * scale".

    Doing this to a "group" of objects would mean either setting each one individually or implementing some system where the object knows which scale to pull the value for and use.

    You don't actually slow down time. You just slow down an object's movements.

    This is especially important because physics is not affected.
     
    LaneFox and djfunkey like this.
  13. Perengano

    Perengano

    Joined:
    Feb 15, 2017
    Posts:
    2
    Code (CSharp):
    1. RigidBody2D rb;
    2.  
    3. private void FixedUpdate ()
    4. {
    5.     rb.gravityScale = timeScale;
    6.     rb.position = (rb.position - rb.velocity * Time.fixedDeltaTime) + rb.velocity * timeScale * Time.fixedDeltaTime;
    7.     rb.rotation = (rb.rotation - rb.angularVelocity * Time.fixedDeltaTime) + rb.angularVelocity * timeScale * Time.fixedDeltaTime;
    8. }
     
    Last edited: Jul 25, 2021