- Home /
Y position keeps changing, after action is complete?
So I have this script that causes the player to duck on a timer when they hit the down key. I want the player to go back to it's original height when the timer is up, the player goes back to close to it's original height but instead the height is dragged down a little each time the action is complete. For example if the starting Y position was 1 and the player ducked, after the timer is up the Y position becomes 0.8 instead of 1. Here is my script:
public class CharacterDuck : MonoBehaviour {
public float countDown = 2;//this count down will be used to determine when the character comes back up
public bool goDown;
public float startHeight;
public Vector3 startPos;
void Start()
{
startHeight = transform.position.y;
}
void Update()
{
startPos = new Vector3(transform.position.x, startHeight, transform.position.z);
if (Input.GetKeyDown ("down"))
{
goDown = true;
}
if (goDown)
{
countDown -= Time.deltaTime;//count down the countDown to 0
if (countDown > 1)
{
transform.position = Vector3.Lerp (transform.position, (startPos + new Vector3(0, -5, 0)), Time.deltaTime*2);//use lerp to move the character from their current position to the "down" position
}
if (countDown <= 1)
{
transform.position = Vector3.Lerp (transform.position, startPos, Time.deltaTime*2);//use lerp to move the character back to the idle position
if (countDown <= 0)
{
countDown = 2;//reset the cooldown
goDown = false;//turn goDown to false
}
}
}
}
}
I think the problem is a combination of using Linear Interpolation and time to cut off your animation and reverse it before it reaches its destination. Linear Interpolation slows down as it approaches its destination.
When you leave your start point the first time to go to your "crouch" destination the destination is far away.. This means your Lerp should be moving fast.
On the way back, however, you will definitely be closer your destination (the original starting point) so it will slow down and not manage to reach the final spot.
This is just a guess
". Linear Interpolation slows down as it approaches its destination."
It shouldn't... it should be LINEAR as in a straight line. It should not have ease in or ease out.
When you're using Lerp, Linear interpolation finds the halfway point between your current location and your destination.
When you move to a point created by Lerp every frame, its only going halfway to your destination every frame.
As you apprach your destination the halfway points are going to be less far apart, so you will appear to slow down.
I think of it this way: If I am trying to lerp to 1 from 0, I will move 0.5 the first frame, then 0.25 the second frame, then 0.125 the third frame and so on. Using this for movement means the closer I get to my target the slower I appear to be moving.
Oh, right... I see what you mean... he's feeding the position into the lerp every frame...
Weird.
Answer by Superrodan · Feb 06, 2015 at 01:01 AM
After reading a bit more about Lerp it's not the halfway point so much as the point represented by the third variable in the parenthesis. It's only halfway if you put 0.5 in.
Still, I still believe that is the issue. I'd suggest going with MoveTowards instead of Lerp. MoveTowards will be a constant value of movement and will behave the same way both on the way down and on the way back.
http://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html
Your answer
Follow this Question
Related Questions
How do I make and object's position and rotation independent again? 1 Answer
Adding rotation to Physics.AddForce(); ... Getting weird error? 0 Answers
Get closest Vector3 position from a GameObject and two Transforms (and the line inbetween them) 2 Answers
Multiple Cars not working 1 Answer
Nav Mesh and transform.position failure! 0 Answers