- Home /
Decreasing timescale for a single object.
Is it possible to somehow decrease or increase timescale only for one object instead of the entire world? Also is it possible to change gravity for a specific rigidbody only?
Answer by yoyo · Mar 05, 2011 at 12:37 AM
For the timescale, you could use an object-specific multiplier in all your own time-based calculations -- something like elapsedTime = Time.deltaTime * myTimeScale
. Whether this is effective or not depends on how much control your code has over the things being updated.
For rigidbody physics, you could adjust the mass and drag of the rigidbody to see if you can get the effect you want. If not then set rigidbody.useGravity = false
and use rigidbody.AddForce to add your own gravity, with a different gravitational constant.
Answer by petrucio · Mar 13, 2012 at 02:55 AM
The private timescale multiplier in it's simple form will not work with a timescale of zero. This is what I did to have a logical Timer that can be paused while the rest of the game continues on it's usual pace:
using UnityEngine;
using System.Collections;
//=========================================================================
// Class: TimeKeeper
// Mirrors Unity's Time.time, allowing the game to be logically paused
// while the animations, UI, and other Time.time dependant tasks keep running at the usual pace.
// TimeKeeper can be paused by the game when timestamp is set to 0,
// or by the user pressing the Pause/Break key.
//=========================================================================
public class TimeKeeper : MonoBehaviour {
// Variable: time
// Access TimeKeeper.time to keep track of time with a different timescale then Time.time (read-only)
public static float time {
get { return instance.mTime; }
}
// Variable: timescale
// Current timescale of the TimeKeeper.
public static float timescale {
get { return instance.mPaused ? 0 : instance.mTimescale; }
set { instance.mTimescale = value; }
}
//=========================================================================
private static TimeKeeper instance;
private float mTime = 0.0f;
private float mTimescale = 1.0f;
private float mLastTimestamp = 0.0f;
private bool mPaused = false;
//=========================================================================
void Start ()
{
if (instance)
Debug.LogError("Singleton violated (ooh!)");
instance = this;
instance.mLastTimestamp = Time.realtimeSinceStartup;
}
//=========================================================================
void Update ()
{
if (Input.GetKeyDown(KeyCode.Pause))
this.mPaused = !this.mPaused;
float realDelta = Time.realtimeSinceStartup - this.mLastTimestamp;
this.mLastTimestamp = Time.realtimeSinceStartup;
this.mTime += realDelta * timescale;
}
}
I did it with my needs in mind - you could 'un-Singletonize' that and have an arbitrary number of TimeKeepers and attach them to your objects.
Answer by Broesel82 · Dec 12, 2011 at 10:40 PM
Can anyone please help me with this problem. I use timescale to freeze the game. But while still everything is freezed i want to do the movement in "normal" speed. For the movement i use the first person controller from unity. But i still dont know how to assign this "private time.timescale".
Answer by by0log1c · Mar 05, 2011 at 02:04 AM
Much similar to yoyo's idea. You could try something like this, in JS:
function Update(){ myTimeFactor = 1; myTimeScale = Time.timeScale * myTimeFactor;
//transform.Translate(Vector3.forward * myTimeScale);
}
I did not test it, but I believe this should create a 'private Time.timeScale' .
Your answer
Follow this Question
Related Questions
How to return unity back to its normal time scale and fixedDeltaTime 0 Answers
Can you lerp an object when Time.timeScale = 0? 1 Answer
C# | How to disable gravity for a certain amount of time? 1 Answer
Time not slowing down on iPhone 3 Answers
Script is only working when slowing down timescale 0 Answers