- Home /
Slowmotion in some GameObjects
Hello Guys,
I want to create the effect to slow-motion in the scene I can use (Time.timeScale), but this one controls the entire scene.
Questions: I want to control the speed (Like 0.3) of the entire scene, but only the player can move at the normal speed (Like 1.0)... It's possible to control by separated the GameObjects in Unity?
Thanks,
Deo.
Answer by Lovrenc · Dec 31, 2012 at 05:50 PM
You can control character in accordance to real time and all other objects with game time. This way, player wont be affected by timeScale.
EDIT: On the top of your class add variable:
float previousFrameTime = 0;
Then in update you do:
float currentTime = Time.realtimeSinceStartup;
float deltaTime = currentTime - reviousFrameTime;
previousFrameTime = currentTime;
deltaTime is the var you use then.
EDIT: your script fixed
using UnityEngine;
using System.Collections;
public class AI_Forward_Speed : MonoBehaviour
{
float previousFrameTime = 0;
void Update ()
{
if (Input.GetKeyDown (KeyCode.Mouse0)) {
Time.timeScale = 0.1f;
print ("Activated it");
}
if (Input.GetKeyDown (KeyCode.Mouse1)) {
Time.timeScale = 1.0f;
print ("Desactivate ir");
}
float currentTime = Time.realtimeSinceStartup;
float deltaTime = currentTime - previousFrameTime;
previousFrameTime = currentTime;
transform.Translate (Vector3.forward * deltaTime * 0.6f, Space.Self);
}
//End
}
But when you use "Time.ScaleTime" apply it to the Entire Scene, how can I use "Time.realtimeSinceStartup" just with the Player or any Object that I need?
Deo :)
You make controller script that is usint realtime ins$$anonymous$$d of Time.deltaTime. ScaleTime is NOT CONTROLING realtime.
You can also create a static variable to add to all objects that are to be slow and divide deltaTime with that.
thanks, but if I use realtime, the character accelerate progressively, be cause this class it's about the time since start the game :(
Time.deltaTime is time between frames. You do same here. Have var that holds realTime from previous frame. Now in update you do:
deltaTime = currentTime - previousTime
and use that for movement....
Looks the same, it's accelerated the time, since pass the seconds, I'm using this:
RealTime = Time.time - Time.realtimeSinceStartup;
am I correct?
Deo.
Answer by Michel-Tonon · Jan 19, 2017 at 06:00 PM
I'm new in unity. My solution...
void FixedUpdate()
{
if (slowMotion)
{
if (!Rigidbody.isKinematic)
velocity = Rigidbody.velocity;
Rigidbody.isKinematic = !Rigidbody.isKinematic;
if (!Rigidbody.isKinematic)
Rigidbody.AddForce(velocity, ForceMode.Impulse);
}
}
Your answer