- Home /
Time.deltaTime help
Can anyone help adding Time.deltaTime to this script? I'm not sure in how many positions it needs to be placed. It's for a camera bob script.
For example, if I do
var translateChange = waveslice * bobbingAmount * Time.deltaTime;
then...
translateChange = totalAxes * translateChange;
...should be * Time.deltaTime also?
var moveTouchPad : Joystick;
private var timer = 0.0;
var bobbingAmount = 0.2;
var midpoint = 2.0;
var speedLeft = 0.3;
var speedRight = 0.4;
var speedForward = 0.4;
var speedBack = 0.3;
function Update ()
{
if (moveTouchPad.position.y > 0 || moveTouchPad.position.y < 0 || moveTouchPad.position.x > 0 || moveTouchPad.position.x < 0)
{
var waveslice = 0.0;
if (Mathf.Abs(1) == 0 && Mathf.Abs(1) == 0)
{
timer = 0.0;
}
else
{
waveslice = Mathf.Sin(timer);
// FORWARD
if (moveTouchPad.position.y > 0)
timer += (moveTouchPad.position.y * speedForward); // INCREMENT TIMER
// RESET TIMER
if (timer > Mathf.PI * 2)
timer -= (Mathf.PI * 2);
}
// MOVE CAMERA
if (waveslice != 0)
{
var translateChange = waveslice * bobbingAmount;
var totalAxes = Mathf.Abs(1) + Mathf.Abs(1);
totalAxes = Mathf.Clamp (totalAxes, 0.0, 1.0);
translateChange = totalAxes * translateChange;
transform.localPosition.y = midpoint + translateChange;
if (translateChange <= (-0.05));
}
else
{
transform.localPosition.y = midpoint;
}
}
}
Comment
Answer by aldonaletto · Jun 08, 2013 at 04:24 PM
Actually, multiplying Time.deltaTime by a velocity results in the displacement made since last frame (velocity * elapsed time gives the distance traveled during this time). Time.deltaTime can also be used to decrement/increment a float variable at constant speed, which I suppose is actually what you should do:
timer += (moveTouchPad.position.y * speedForward * Time.deltaTime); // INCREMENT TIMER
This would make the timer variable be incremented at a speed proportional to moveTouchPad.position.y
So there's no need for the "move camera" code to be multiplied by it?
Your answer
![](https://koobas.hobune.stream/wayback/20220613103757im_/https://answers.unity.com/themes/thub/images/avi.jpg)