Hi @JesperGreen
There are a few tricks to this, but looking at your code, you perhaps need to consider the "distPos" value and make it Clamp01(...) (Keeps it between 0 and 1) and use the key press to flip the direction. Something like (I've just typed this in here, not tested / syntax checked)...
Extra declarations...
float direction = 1f; // The direction of travel
float maxDistance = 2f; // The maximum move distance
Start()
{
StartPos = transform.position;
EndPos = StartPos + Vector3.forward * maxDistance;
}
Update()
{
if (Input.GetKey(KeyCode.UpArrow))
direction = 1f;
else
direction = -1f;
distPos = Mathf.Clamp01(distPos + (time.deltaTime * moveSpeed * direction));
transform.position = Vector3.Lerp(StartPos, EndPos, distPos);
}
Hope this helps :)
↧