Hi,
I need player movement like this game:
I’ve got my character staying on the x,y lines, but the lane changes are not smooth.
My code:
void FixedUpdate()
{
pos = transform.position;
if (Input.GetKey(KeyCode.UpArrow))
{
pos += (Vector2.up * speed * Time.deltaTime);
pos.x = Mathf.RoundToInt(pos.x);
transform.position = pos;
}
else if (Input.GetKey(KeyCode.DownArrow))
{
pos += (Vector2.down * speed * Time.deltaTime);
pos.x = Mathf.RoundToInt(pos.x);
transform.position = pos;
}
else if (Input.GetKey(KeyCode.LeftArrow))
{
pos += (Vector2.left * speed * Time.deltaTime);
pos.y = Mathf.RoundToInt(pos.y);
transform.position = pos;
}
else if (Input.GetKey(KeyCode.RightArrow))
{
pos += (Vector2.right * speed * Time.deltaTime);
pos.y = Mathf.RoundToInt(pos.y);
transform.position = pos;
}
This question is the same as mine , but i’ve not been able to understand it. Advanced grid movement vs free movement question - Questions & Answers - Unity Discussions
“Then add a threshold such that when the character is at an intersection, it doen’t need to be perfectly aligned with the centerlines, but instead will snap to align with them automatically when close enough.”