-1
\$\begingroup\$

I am making a 2D subway surfer type of game right now and I found this code on the internet that switches the lane of the player but the problem is it doesn't switch. When I play the scene it won't move it will just give the of "int" desiredLane. Why is this happening?


CODE

private const float LANE_DISTANCE = 3.0f;

[Header("Movement")]
public float moveSpeed;

public int desiredLane = 1; //0 = Left 1 = Middle 2 = Right

void MobileSwipeControl()
{
    //Movement Input
    if (Input.GetKeyDown(KeyCode.A))
        MoveLane(false);
    if (Input.GetKeyDown(KeyCode.D))
        MoveLane(true);

    //Calculate where we should be in the future
    Vector3 targetPosition = transform.position.y * Vector3.up;

    if (desiredLane == 0)
        targetPosition += Vector3.left * LANE_DISTANCE; 
    else if (desiredLane == 2)
        targetPosition += Vector3.right * LANE_DISTANCE;

    //Let's calculate our move delta
    Vector3 moveVector = Vector3.zero;
    moveVector.x = (targetPosition - transform.position).normalized.x * moveSpeed;
    moveVector.y = moveSpeed;

    //Move the player
    myBody.velocity = Vector2.up * moveSpeed;
 }

private void MoveLane(bool goingRight)
{
    desiredLane += (goingRight) ? 1 : -1;
    desiredLane = Mathf.Clamp(desiredLane, 0, 2);
}
\$\endgroup\$
1
\$\begingroup\$

So I found out the answer thanks to Zibelas! I forgot to apply moveVector to my movement.


//Move the player
myBody.velocity = moveVector * moveSpeed;

| improve this answer | |
\$\endgroup\$
  • 2
    \$\begingroup\$ Don't forget to mark the answer as Accepted, if your problem has been fixed. \$\endgroup\$ – verified_tinker Jun 20 at 10:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.