# C# Five lane movement

Updated on April 20, 2018 in
8 on April 19, 2018

I want my gameobject to move on a five lane road.. After watching lot of tutorial the gameobject was able to use three lanes out of five.. Below is my code

private int desiredLane =1;
private const float lanedistance = 1.0f;
//Movement
private float jumpForce = 7.0f;
private float gravity = 12.0f;
private float verticalVelocity;
//speed modifier
private float originalSpeed = 10.0f;
private float speed;
private CharacterController controller;
private void Update(){
if (Input.GetKeyDown (KeyCode.LeftArrow)|| (Input.GetMouseButtonDown(0) && Input.mousePosition.x < Screen.width / 2)) {
MoveLane (false);
}
if (Input.GetKeyDown (KeyCode.RightArrow)|| (Input.GetMouseButtonDown(0) && Input.mousePosition.x > Screen.width / 2))
{
MoveLane (true);
}
//calculate where we should be
Vector3 targetPosition = transform.position.z * Vector3.forward;
if (desiredLane == 0)
targetPosition += Vector3.left * lanedistance;
else if (desiredLane == 2)
targetPosition += Vector3.right * lanedistance;
//lets calculate our move delta
Vector3 moveVector = Vector3.zero;
moveVector.x = (targetPosition - transform.position).normalized.x * speed;
//Y  Up and Down
moveVector.y = verticalVelocity;
//move the player
moveVector.z = speed;
controller.Move(moveVector * Time.deltaTime);
}
private void MoveLane(bool goingRight){
desiredLane += (goingRight) ? 1 : -1;
desiredLane = Mathf.Clamp (desiredLane, 0, 2);
}
• Liked by
on April 19, 2018

That’s because you have that Mathf.Clamp(desiredLane, 0, 2) at line 36, which forces that value to stay between 0 and 2, I didn’t read all the code but I imagine that you want that to clamp between 0 and 4

on April 19, 2018

I’ve tried changing it to 4 and also the desiredlane to 2, BT its not working, I have to tap twice before it move to the next lane.. I think the problem lies I’m the targetposition ..

Hope you could help with it.. Thanks.
Kinda new to unity & c#

on April 20, 2018

It worked.. Thanks..

Words can’t express my appreciation

• Liked by
on April 19, 2018

Where you clamp desiredLane at the bottom, you’re only clamping it between 0 and 2 (0, 1, 2) that’s only 3 lanes. Change the 2 to a 4.

on April 19, 2018

I’ve tried changing it to 4 and also the desiredlane to 2, BT its not working, I have to tap twice before it move to the next lane.. I think the problem lies I’m the targetposition ..

Hope you could help with it.. Thanks.
Kinda new to unity & c#

Wise
on April 19, 2018

It’s the same problem, you’re only checking if they’re in lane 0 or 2, you need to check for all 5 lanes now.

Edit: you can actually skip the checks all together if instead you just multiply Vector3.right by the lane they’re currently in.

so 0 will be the far left (Vector3.right * 0 = (0, 0, 0))

1 will be the 2nd to left (Vector3.right * 1 = (1, 0, 0))

2 will be the center (Vector3.right * 2 = (2, 0, 0))

and so on. skipping the if statements all together

• Liked by
on April 19, 2018

Just a hint, when you follow a tutorial, expecially if you’re not a programmer you should try to read each line of the code, and try to figure out what each line do and why. You should have analyzed by yourself that Clamp function if you didn’t know what it did

on April 19, 2018

I’ve tried changing it to 4 and also the desiredlane to 2, BT its not working, I have to tap twice before it move to the next lane.. I think the problem lies I’m the targetposition ..

Hope you could help with it.. Thanks.
Kinda new to unity & c#