- Home /
How to lerp a car while it's moving forward at a constant velocity
This car is a rigidbody2D object that is constantly going Vector3.up * 10 while it does not collide into anything on the 2D plane. While I can move the car left and right, it does not translate smoothly. My objective is to be able to move the car left and right smoothly, but the fact that the car is constantly moving and that I can't just lerp the X-position of the car is causing me a headache.
void Update () {
if (Input.GetKeyDown("left"))
{
if (transform.position.x > -1)
{
//Vector3 targetPos = transform.position;
//targetPos.x -= 1.25f;
MoveLeft ();
}
}
if (Input.GetKeyDown("right"))
{
if (transform.position.x < 1.5)
{
MoveRight();
}
}
}
void MoveLeft() {
Vector3 targetPos = transform.position;
targetPos.x -= 1.25f;
transform.position = Vector3.Lerp(transform.position, targetPos, 1f);
}
Answer by whydoidoit · Feb 15, 2014 at 07:31 AM
Use Mathf.Lerp rather than Vector3.Lerp to move just the X position of the car - then make your position a combination of the X you are lerping at the rest of it.
float xPos = 0;
float horizontalMovement = 2;
void MoveLeft() {
xPos = Mathf.Lerp(xPos, xPos-horizontalMovement, Time.deltaTime * 3);
}
void Update() {
//Move vehicle upwards etc
...
var pos = transform.position;
pos.x = xPos;
transform.position = pos;
}
I should have been more specific with my objective, so I apologize for the lack of detail in regards to that.
What I'm trying to do is to get the car to change lanes with just one keystroke. Eventually, I'll put the game on a mobile device so it will move into another lane with just one horizontal swipe. It would be simple to do if the car wasn't constantly moving, but the fact that it does makes it difficult to implement a solution.
So sure - what you need for that is something like this:
float xPos;
float laneWidth = 4;
float laneChangeSpeed = 2f;
void OnEnable() {
xPos = transform.position.x;
}
void Update() {
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A)) {
StartCoroutine($$anonymous$$oveLane(-laneWidth));
}
...
var pos = transform.position;
pos.x = xPos;
transform.position = pos;
}
IEnumerator $$anonymous$$oveLane(float offset) {
var t = 0f;
while(t < 1) {
t += Time.deltaTime * laneChangeSpeed;
xPos = $$anonymous$$athf.Lerp(xPos, xPos+offset, t);
yield return null;
}
}
Thank you, I will try this code as soon as I get back home.
By the way, am I supposed to adjust the offset after each lerp, or is that not necessary?
Nope - the lerp is moving from one position to the other - it will an non-eased change however.
If you want easing we need to apply a function to the t parameter to create that effect or use an Animation curve.
Your answer
![](https://koobas.hobune.stream/wayback/20220613133548im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rigidbody2D adding force vs modifying velocity for character jump 1 Answer
Why does Rigidbody.OnDisable() reset the velocity? 1 Answer
Rigidbody.velocity Not working on parent rigidbody with four child rigidbodies attached to it 1 Answer
Kinematic rigidbody movement. 2 Answers
Float RigidBody2D whitout Move any place, static position -1 Answers