- Home /
Help me to understand how update and switch work in loop together.
Hello! I've started learning unity week ago and right now i'm doing a little project. I want to make a 3 line movement game (like Subway surfers etc.) and after a lot of troubleshooting and googling i did manage to get the movement result that i wanted. Here is the code:
public class PlayerControllerMoveTo : MonoBehaviour
{
private int lane = 1;
void Update()
{
if (Input.GetKeyDown(KeyCode.D) && lane < 2)
{
lane++;
}
if (Input.GetKeyDown(KeyCode.A) && lane > 0)
{
lane--;
}
Move(lane);
}
private void Move(int lane)
{
Vector3 rightPos = new Vector3(3, 1.5f, transform.position.z);
Vector3 leftPos = new Vector3(-3, 1.5f, transform.position.z);
Vector3 middlePos = new Vector3(0, 1.5f, transform.position.z);
float speed = 10f;
float step = speed * Time.deltaTime;
transform.Translate(Vector3.forward * Time.deltaTime * speed);
switch (lane)
{
case 0:
transform.Translate(Vector3.forward * Time.deltaTime * speed);
transform.position = Vector3.MoveTowards(transform.position, leftPos, step);
break;
case 1:
transform.Translate(Vector3.forward * Time.deltaTime * speed);
transform.position = Vector3.MoveTowards(transform.position, middlePos, step);
break;
case 2:
transform.Translate(Vector3.forward * Time.deltaTime * speed);
transform.position = Vector3.MoveTowards(transform.position, rightPos, step);
break;
}
}
}
However, i actually don't understand how code that i've written works. For some reason, if i take out "transform.Translate(Vector3.forward Time.deltaTime speed);" either from switch cases or from before the switch happens, my player only moves forward for a bit while it moves when switching lanes, but not on its own like i want it to.
Help me understand the flaw in my logic. Update function should be called constantly, so it should always go through the entire code that i've written, thus it means it should always move forward z and always stay on the specified x until lane switches, right? Then why do i need to call my forward statement twice in order for the movement to work?
Answer by gane1404 · Aug 07, 2021 at 09:44 PM
I see there is like 126 people following the question, not really sure how common it is here, but i guess i'll answer my question myself, hopefully it might help someone. What i'm assiming happening is move towards locks player when a single translate happens, so thats why i managed to make player move with 2 translates. However, solution that i posted is just outright bad because when player switches lanes using this script, player object actually speeds up twice during switch. Now, i have implemented a new way to move forward by using rigidbody instead and tested the player object moving speed by doing some precise caluclations with other object on my scene and it seems this way is bug-free.
private void Move(int lane)
{
Vector3 rightPos = new Vector3(3, 1.5f, transform.position.z);
Vector3 leftPos = new Vector3(-3, 1.5f, transform.position.z);
Vector3 middlePos = new Vector3(0, 1.5f, transform.position.z);
Vector3 playerVelocity = new Vector3(0, 0, speed);
float step = speed * Time.deltaTime;
playerRb.velocity = playerVelocity;
switch (lane)
{
case 0:
transform.position = Vector3.MoveTowards(transform.position, leftPos, step);
break;
case 1:
transform.position = Vector3.MoveTowards(transform.position, middlePos, step);
break;
case 2:
transform.position = Vector3.MoveTowards(transform.position, rightPos, step);
break;
}
}
However, if you are making an infinite runner game you are better off making scene move towards the player instead (or implement some kind of invisible player position reset), because you dont want player to end up in faraway coordinates.