- Home /
Movement over Time
Hello, I'm new to Unity, and I'm trying to do a side-stepping / dashing movement for my character.
var startTime : float;
var duration : float = 3.0;
var x : float;
if(Input.GetButtonUp("Shift"))
{
if(combat_active == true)
{
if(Input.GetAxis("Vertical") > 0 && !animation.IsPlaying("combat_stance_swift_fwd"))
{
startTime = Time.deltaTime;
if(Time.deltaTime - startTime > duration)
{
Debug.Log("Dashing forward");
animation.CrossFade("combat_stance_swift_fwd");
move_direction = Vector3(0,0,1);
move_direction = transform.TransformDirection(move_direction);
//if(animation)
movement_block = true; // disable further x,z input movement
controller.Move(move_direction);
}
}
}
this code block stands in the Update() function. And doesn't work.
I'm a bit confused how to make the transition from one positon to the forward position smooth. All I manage to do is a teleportation forwards.
I also tried if and for, but i didn't got the results. Can someone please explain how to do a movement in a timeframe?
Thank you
Answer by Benproductions1 · Jul 25, 2013 at 01:20 AM
Hello,
Just looking at the movement code, I can already see many problems.
1 it will never execute
if startTime
is set to Time.deltaTime
before every check wether Time.deltaTime - startTime > duration
,
then Time.deltaTime - startTime
will always equal 0
and will never be over duration (or always under)
2 You are not taking into account time
Update
is performed every frame. If every frame you move by a set amount, then firstly, your speed (distance over time) will vary depending on yoru framerate and secondly with the amoutn of movement you've coded in, you will move at the speed of sound at 300 fps.
If you want to make it framerate independant, you need to "find" the amount of time that has passed since the last frame.
Unity includes this functionality in it's Time
class, called deltaTime
.
var velocity:Vector3 = Vector3.foward;
function Update() {
transform.position += velocity; //distance per frame
transform.position += velocity*Time.deltaTime; //distance over time
}
Hope this helps,
Benproductions1
Answer by 0tacun · Jul 25, 2013 at 01:07 PM
Thank you very much for your hints!
I rethought my code and changed it. It is still ugly, but at the moment it does what i want.
if(Input.GetButtonUp("Shift") || dashing == true)
{
if(combat_active == true)
{
if( Input.GetAxis("Vertical") > 0)
{
dashing = true;
if(timer > 0)
{
Debug.Log("Dashing forward");
animation.CrossFade("combat_stance_swift_fwd");
move_direction = Vector3(0,0,2);
move_direction = transform.TransformDirection(move_direction);
movement_block = true; // disable further x,z input movement
controller.Move(move_direction*Time.deltaTime);
timer -= Time.deltaTime;
counter++;
Debug.Log(timer);
}
else if(timer < 0)
{
dashing = false;
timer = animation["combat_stance_swift_fwd"].length/2;
}
}
}
It isn't the best solution, so now I'm going to try to use subfunctions for the dashing movement.
Thank you again for your help!
0tacun