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?
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.
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.
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”.
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' .