- Home /
incremental movement
I'm trying to use the SmoothDamp function to set up smooth incremental movement for my character on key down. I cant seem to get it to work at all. Is there something I'm doing wrong or can someone recommend another way of being able to move a character X amount of distance over Y amount of frames on keyDown?
private var currVel = Vector3.zero; private var smoothTime: float = 20;
if(Input.GetKeyUp("w")){
Debug.Log("moved");
var targetPos : Vector3 = Vector3.forward * 2;
transform.position = Vector3.SmoothDamp(transform.position,targetPos, currVel, smoothTime);
}
Answer by Scribe · Mar 23, 2011 at 08:08 PM
var start : Transform;
var end : Transform;
function Update () {
if(Input.GetKeyUp("w")){
transform.position = Vector3.Lerp(start.position, end.position, Time.time);
}
}
i think this would be a better way of doing it using the Vector3.Lerp function
thank you. I was using lerp before but I was using Time.deltaTime maybe thats why it wasnt working. I'm still getting used the Time class and what all those variations of time actually mean for program$$anonymous$$g time based actions
Answer by Eric5h5 · Mar 23, 2011 at 08:36 PM
That won't work because GetKeyUp is only true for one frame. Vector3.SmoothDamp isn't a function that runs over time or anything, it returns a value immediately, so it has to be used repeatedly to get movement. See here (it's about Lerp, but the same principle applies).
Your answer
![](https://koobas.hobune.stream/wayback/20220613114448im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
SmoothDamp smoothTime ignored! 1 Answer
How to apply smooth damp to movement? 1 Answer
rotate object by list values 0 Answers
Doing something wrong with lerp? 1 Answer
How to lerp over time help? 1 Answer