Movement question

Hello! Imagine a fast moving car on the highway which moves smoothly from one road strip to another. I want to make something like that. My object moves forward on the X coordinate and I have only Z coordinate to which it should move smoothly. To say short, I want to change my object Z position to another Z position (I have target Z coordinate) smoothly.(Object moves using “rb.velocity = transform.forward * 2f” )

Im not sure i follow what you mean.

If the “car” is only moving in one direction, “forward” , or "the Z axis"at all times, then it will never be able to move side to side.

So what exactly are you wanting to do, as it seems you have your object moving forward already…

Sorry for inaccuracy. My object moves forward and I want to change its Z coordinate to make it move right or left. Something like in the game “Subway surfers”. Player always running forward and to obstruct the obstacles he should change its way. In my project after calling void, player should smoothly change his Z coordinate to the target Z coordinate (it continues to move forward using X axe)

Use Rigidbody.MovePosition() to move along the other axes.

Moving how? Through player input?

For example :

Vector3 movement = new Vector3(0, 0, Input.GetAxis("Horizontal"));

transform.position += movement;

Psuedocode… just an example of simple z movement. But my point is, its fairly simple but you first need to establish exactly how and what you want to happen. Im just assuming you want player input controlled motion.

And this…

Okey. I think the main problem is how I explained the situation :smile:. I’ll try again…

You can see the example situation in the picture. Player is this white object. Player only can turn right and left. Red arrows show how player decided to move (just for example). Green arrow shows how I want this car to move. As you can see in the image 3 player decided to turn right. At this moment script records the closest Z coordinate to variable “numline” using "numline = Mathf.Round(transform.position.z);" (at this example as you can see the closest Z is 3, so numline = 3). After that script should run the “XXX” function which slowly changes player Z position to numline. As a result player will move like the green arrow shows on the picture 3. Question: how I can do this?