- Home /
How To Slow Camera Position Transform?
The main camera changes position when triggered but, too quickly. Is there a way to "slow" the cameras journey to it's new position? Thanks for looking.
var MainCamera : Camera;
var CurveX = 90.0;
var CurveY = 45.0;
var CurveZ = 145.0;
function Update () { }
function OnTriggerEnter (other : Collider) { Camera.main.transform.position = Vector3(CurveX, CurveY, CurveZ); }
Answer by Kourosh · Apr 19, 2011 at 11:10 AM
Try this:
var CurveX = 90.0;
var CurveY = 45.0;
var CurveZ = 145.0;
var increment:float;
var moveCam:boolean = false;
function Update () { if(moveCam){
Camera.main.transform.position = Vector3.Slerp(Camera.main.transform.position,Vector3(CurveX, CurveY, CurveZ), increment);
if (increment <1) increment +=0.01;
}
}
function OnTriggerEnter (other : Collider) {
moveCam = true; }
Slerp/Lerp function will smoothly change values.
Check this also:
http://unity3d.com/support/documentation/ScriptReference/Vector3.Slerp.html
You could also try $$anonymous$$athf.SmoothDamp and $$anonymous$$athf.SmoothStep. But would'nt it be better to store the transformation values and apply them during LateUpdate()?
Well, yes, however I don't really know how different the result would be using each one of these functions. So i just took one. And about the LateUpdate(), are you talking preformance wise or the smoothness of the motion?
Answer by GameCherry · Apr 19, 2011 at 09:09 PM
Thanks for the reply Kourosh but, this code does nothing. Increasing/decreasing the increment values also has no effect on the camera. I tried adding a camera/target variable like in my original code but, again no difference. So, I changed it around a bit.
I swapped Slerp to Lerp and replaced the increment modifier with Time.deltaTime. This setup works better than my original code in that the camera movement is slowed but, still not slowed enough.
I'm a little confused on Time. It's my understanding that Time.deltaTime is an action done within a certain amount of time were-as Time.time is an action done within a certain amount of frames - is this correct? Either way, no matter how I +,-,,/ the Time factor - the amount of time it takes the camera to "move" to it's new position seems to be the same. (though and / seem to add/subtract distance rather than time)
Here is my modified code as it is now.
var MainCamera : Camera;
var CurveX = 90.0;
var CurveY = 45.0;
var CurveZ = 145.0;
var duration = 1.0;
function Update () { }
function OnTriggerEnter (other : Collider) { Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position,Vector3(CurveX, CurveY, CurveZ), Time.deltaTime + duration);
}
Time.deltaTime + duration does not seem to make the position transition any longer. If anyone could tell me what it is I am doing wrong - it would be greatly appreciated. Thanks everyone for your comments and interest.
Time.deltaTime is the time took to render your last frame. Whereas Time.time is actual time from the beginning of your application. Slerp and lerp functions need an increasing/decreasing factor from 0 to 1 or 1 to 0 to function properly. Yours is not increasing cause lets say Time.deltaTime is roughly 0.2 and it's summed with 1 so it's 1.2 and its done. So you can't see the gradual translation over time. I've edited my code. please check again.
I noticed the problem, lerp/slerp should be used on those routines that happen every frame. I mistakenly used it in OnTriggerEnter which happens only once.
Answer by whydoidoit · Apr 01, 2012 at 10:00 PM
I find that I have to do a lot of smoothing from code and don't want the overhead of adding an iTween to the game object.
To help I've built some helper classes which you can take a look at and download here. Basically they let you replace a standard float, Vector3 or Quaternion with a SmoothXXXX version that will use one of four smoothing functions to interpolate between the current and the target position over time.