Issue with player position on 3-lane track
Hello everybody,
While making a simple, 3-lane endless runner game (like Temple Run and Subway Surfer), I've encountered an issue I can't seem to fix.
When moving the player between the 3 lanes the position seems to mess up randomly. Obviously it's not random, but I can't seem to find the problem.
Here is my code:
void Update()
{
GetComponent<Rigidbody>().velocity = new Vector3(horizVel, 0, 8);
if ((Input.GetKeyDown(moveL)) && (playerPosition != 1) && (controlLocked == false)) //move left, if possible
{
controlLocked = true;
horizVel = -4;
StartCoroutine(NoMovement());
playerPosition--;
}
else if ((Input.GetKeyDown(moveR)) && (playerPosition != 3) && (controlLocked == false)) //move right, if possible
{
controlLocked = true;
horizVel = 4;
StartCoroutine(NoMovement());
playerPosition++;
}
}
IEnumerator NoMovement()
{
yield return new WaitForSeconds(0.25f);
horizVel = 0;
controlLocked = false;
}
Here are two screenshot to clearly show what I mean:
This screenshot is taken at the beginning of a game.
This screenshot is taken a few seconds into a game.
You can clearly see the position is different and the difference become larger the game processes.
I'm hoping somebody sees the problem and/or what I'm doing wrong.
Thanks in advance,
Frenk
Your answer
Follow this Question
Related Questions
Navmesh point 0 Answers
Transform position reference 1 Answer
Automatically flip a player game object upright on z axis? 1 Answer
How to get all child objects back to its start position. 1 Answer
How do I find y value on terrain ground? 0 Answers