- Home /
Triggering Timescale with Mousedown
hello people (me again... but oh my I am Soooooo Close.) What's wrong with my code?
I am trying to click on an invisible rigid body that will translate my camera towards it in slow motion. Then I will do something and click on another object to return in slow motion to that first position.
I love the Zoom Effect but since I need to go to a specific point (and then back later) I don't think it will work.
Is the right way to trigger Timescale (an update function) but when I click/mouseDown/touch?
Here is what I have:
ZOOM IN SCRIPT:
// vector3 of Camera I want to record so the Zoom out can use it too
public var originCamPos : Vector3;
// The target II will zoom to to.
var target: Transform;
function Update () {
//this is Unity but I want to use Mouse Down
if (Input.onMouseDown ("TRIGGER_In")) {
if (Input.GetButtonDown ("Fire1")) {
if (Time.timeScale == 1.0)
Time.timeScale = 0.7;
else
Time.timeScale = 1.0;
// Adjust fixed delta time according to timescale
// The fixed delta time will now be 0.02 frames per real-time second
Time.fixedDeltaTime = 0.02 * Time.timeScale;
}
}
function OnMouseDown () {
// record original Camera position to come back to later, but I think I am mixing Vector3 & transforms
originCamPos = Camera.main.transform.position;
Camera.main.transform.position = target.transform.position;
}
Should in theory work.
One problem is Semantics... Calling the if mouse down from an update script to slow time down - but then How on earth do I scale the time back up at the end?
Another question is simply Collecting the Original Cam position to return to in a Public Var
Here is The ZOOM BACK Script:
// The original Camera pos to return to.
public var originCamPos : Vector3;
function Update () {
// Again, this is Unity but I want to use Mouse Down
// something like : if (Input.onMouseDown ("TRIGGER_Back")) {
if (Input.GetButtonDown ("Fire1")) {
if (Time.timeScale == 1.0)
Time.timeScale = 0.7;
else
Time.timeScale = 1.0;
// Adjust fixed delta time according to timescale
// The fixed delta time will now be 0.02 frames per real-time second
Time.fixedDeltaTime = 0.02 * Time.timeScale;
}
}
function OnMouseDown () {
Camera.main.transform.position = originCamPos;
}
It seems like I am close..ish am I approaching this all wrong? Thanks So much for everyone's help
~be
Thank you fifth$$anonymous$$notch!! I'm going to try Lerp now
Answer by fifthknotch · Mar 09, 2014 at 05:36 AM
This code right here is part of your problem:
Camera.main.transform.position = target.transform.position;
This says "teleport" my camera to this location. Regardless of the timescale, your camera will instantly go to that position because timescale only affects physics and animations. Check this out from the docs:
When timeScale is set to zero the game is basically paused if all your functions are frame rate independent.
Also, timeScale affects the deltaTime so you should not have to tweak that in your code.
I would suggest instead of using timescale, use lerp. Just change the speed at which you lerp to your target. If timeScale is adding slow motion effects to other objects in your scene then use both timeScale and lerp (timeScale will affect lerp if your movement is being multiplied by time.deltaTime which would solve your problems.
Thanks This woks a bit for me.... But
1) I am only able to do this ONCE each time and no more
2) my startPos & endPos are distinct Vector 3's I must program by hand each time I use the script Since I have a FCP as the startPos of my ZoomIn script (and the endPos of my ZoomOut script) and a Plane as the endPos of my ZoomIn script (and the startPos of my ZoomOut script) can't I just be passing variables for this?
ie: how to make a thing's Transform > a Vector3?
Here is my Zoom in:
var startPos: Vector3;
var endPos: Vector3;
var time : float;
function On$$anonymous$$ouseDown () {
var i = 0.0;
var rate = 1.0/time;
while (i < 1.0) {
i += Time.deltaTime * rate;
Camera.main.transform.position = Vector3.Lerp(startPos, endPos, i);
yield;
}
}
Zoom out is reverse:
var startPos: Vector3;
var endPos: Vector3;
var time : float;
function On$$anonymous$$ouseDown () {
var i = 0.0;
var rate = 1.0/time;
while (i < 1.0) {
i += Time.deltaTime * rate;
Camera.main.transform.position = Vector3.Lerp(endPos, startPos, i);
yield;
}
}
When you zoom in and then back out, is your camera located in the center of startPos and endPos or does it actually complete the movement back to the start position. I feel like since both scripts check for the mouse to be clicked they both try to move your camera at the same time. if the camera gets stuck at the midpoint between startPos and endPos, then that means the camera is lerping in both scripts.
Thanks so much for your help Fifth!
Each script is on a different object/Trigger. I start the zoom in from a specific Vector3 that I have put in, not necessarily where the player is, but somewhere in the vicinity, so that it Jumps to my Vector3 StarPos and then slow mo's all the way to my specific endPos. Same thing with the Zoom out, after the player scrolls around in the vicinity they jump back through a Vector3 the Vector3 startPos and then all the way back to the EndPos that I have predefined...
So I am wondering if I can get rid of the startPos entirely and only define the endPos.??
Originally I had wanted to keep track of the startPos the return the player back exactly, but I actually prefer to define it by hand in a Vector3 that I will eventually make private...
Does that make sense?
Yes. First, find your player or camera by its tag or name and then use it's transform as the start point. That will get rid of the pre-set start point. I don't see anything in the scripts that you posted that would keep it from working multiple times. I will test then in a bit...
I created a script that handles both zoo$$anonymous$$g in and out. Typically, I always use lerp in an Update function. This doesn't mean you can't use it in a coroutine, I just didn't in my code. The script checks for the camera to be close to the desired position and then snaps it in place to stop lerping. You start the movement by clicking the mouse button.
Also, your camera can start anywhere and it lerp from that position and return back to that position. Check out how I use the camera's current transform as its starting position:
#pragma strict
var startPos: Vector3;
var endPos: Vector3;
private var movePos : Vector3;
private var moving;
var time : float;
function Start () {
startPos = Camera.main.transform.position;
}
function Update () {
var i = 0.0;
var rate = 1.0/time;
// Checks for input to start lerp
if (Input.Get$$anonymous$$ouseButtonDown (0) && !moving) {
moving = true;
}
// Lerp from current position to movePos
// moving keeps the camera from lerping to a different position while lerping to the first position
if (moving) {
i += Time.deltaTime * rate;
Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position, movePos, i);
}
// movPos is set based on where the camera currently is (either startPos or endPos)
if ( Camera.main.transform.position.z <= startPos.z+0.1 ) {
Camera.main.transform.position = startPos;
movePos = endPos;
moving = false;
}
else if ( Camera.main.transform.position.z >= endPos.z-0.1 ) {
Camera.main.transform.position = endPos;
movePos = startPos;
moving = false;
}
}
It's not the cleanest code, but it works. I modified your code to make it using the same variables, and I changed the mouse click part for testing purposes (I did not want to try to build an entire scene with triggers and everything). From this, you should be able to tweak your own code and figure out what the difference is between them and why yours won't work multiple times.
Answer by Bentoon · Mar 09, 2014 at 06:55 PM
Thanks This woks a bit for me.... But
1) I am only able to do this ONCE each time and no more
2) my startPos & endPos are distinct Vector 3's I must program by hand each time I use the script Since I have a FCP as the startPos of my ZoomIn script (and the endPos of my ZoomOut script) and a Plane as the endPos of my ZoomIn script (and the startPos of my ZoomOut script) can't I just be passing variables for this?
ie: how to make a thing's Transform > a Vector3?
Here is my Zoom in:
var startPos: Vector3;
var endPos: Vector3;
var time : float;
function OnMouseDown () {
var i = 0.0;
var rate = 1.0/time;
while (i < 1.0) {
i += Time.deltaTime * rate;
Camera.main.transform.position = Vector3.Lerp(startPos, endPos, i);
yield;
}
}
Zoom out is reverse:
var startPos: Vector3;
var endPos: Vector3;
var time : float;
function OnMouseDown () {
var i = 0.0;
var rate = 1.0/time;
while (i < 1.0) {
i += Time.deltaTime * rate;
Camera.main.transform.position = Vector3.Lerp(endPos, startPos, i);
yield;
}
}
Your answer
Follow this Question
Related Questions
Javascript Timescale Problem 0 Answers
Can i ignore timescale when playing AudioSource.PlayClipAtPoint ? 1 Answer
'timeScale' is not a member of 'Time' 1 Answer
Game over scene when the time is up ? 2 Answers
The Speed of Built APK 0 Answers