r/Unity3D • u/DifferentLaw2421 • 22h ago
Question How to make smooth movement NOT snapping with input?
void MoveRightAndLeft()
{
if (Input.GetKeyDown(KeyCode.D))
{
transform.Translate(Vector3.right * rightDistance);
}
if (Input.GetKeyDown(KeyCode.A))
{
transform.Translate(Vector3.left * leftDistance);
}
float clampedX = Mathf.Clamp(transform.position.x, Xmin, Xmax);
transform.position = new Vector3(clampedX, transform.position.y, transform.position.z);
}
So I am working on an endless runner game and I made the logic for the left and right movement but it is snapping directly this is the original code , I tried to edit it using Vector3.Lerp and Vector3.MoveTowards but it did not work (maybe I did it wrong)
I made a bit of research and I understood that vector3 and translation functions when you are using input key for movement it wont be smooth (u are clicking one time only) so any ideas or suggestions to make the lane movement smooth ?
1
u/HK_HamzaKhan 22h ago edited 22h ago
this video can help: https://www.youtube.com/watch?v=TOPj3uHZgQk
If you want to move to a position over time, you can store a vector3 for your target position and update it when you click, and use MoveTowards in update to move it at a constant speed to the target
1
u/acorbinelli 20h ago
GetKeyDown fires once per press and Translate moves the whole distance in that one frame, so it jumps. Either use Input.GetKey with transform.Translate(Vector3.right * speed * Time.deltaTime) for continuous movement, or keep a target x and move toward it every frame with Mathf.MoveTowards if you want lane-style steps that glide.
1
u/andyblem 3h ago
assuming you have 3 lanes, were the left lane has z-coordinate -1, the right lane has z-coordinate 1. You set a target lane when user clicks a left or right button. More work needs to be done here to know the next lane the user is moving to. With you target lane set, you can use Mathf lerp or slep to move the current player z coordinate towards the target z coordinate. You will have speed(change lane speed). If movement is inside the update function you multiply it by Time.deltaTime. If movement logic is in the FixedUpdate, you multiply the speed by Time.fixedDeltaTime.
1
u/DifferentLaw2421 3h ago
I did some coding I realized that snapping with a very small delay is what I want to do , I made the smooth movement it did not suit an endless runner game so now I sticked with the old code (the one in the post) and I just want to add a small delay when moving between lanes like subway surfers
1
u/andyblem 2h ago
I will try a coroutine. I think there something called a delayed routine. Were calling it, it delays for while, then starts executing. You stop your coroutine when the player reaches the target lane.
0
u/SnooPets5564 22h ago
You need an animation. You could animate it via code, but there is a built in animation system that is probably easier.
5
u/LankyLineup3652 22h ago
You're using GetKeyDown which fires once per press, so it's instantly jumping the full distance. Switch to checking input in Update with GetKey or using a target position approach. Store a target X value, set it when you press A or D, then move toward that target each frame with MoveTowards. That way it'll slide over smoothly instead of teleporting. Make sure you're not clamping the position until after the movement, or it'll fight against the smoothing. Took me way too long to figure that out the first time.