moving like subway surfer or any infinite runner 3d

Dear All!

I need a litle bit help, I use this script:

public KeyCode moveL;
public KeyCode moveR;
public int forwardSpeed;
public float horizVel = 0;
public float horizSpeed = 2;
public int laneNum = 0;
public string controlBlocked;

void Start()
{

}

void Update()
{
GetComponent<Rigidbody>().velocity = new Vector3(horizVel, 0, forwardSpeed);

if (Input.GetKeyDown(moveL) && (laneNum > 1) && (controlBlocked == "n"))
{
horizVel = -2;
StartCoroutine(stopSlide());
laneNum--;
controlBlocked = "y";
}

if (Input.GetKeyDown(moveR) && (laneNum < 3) && (controlBlocked == "n"))
{
horizVel = 2;
StartCoroutine(stopSlide());
laneNum++;
controlBlocked = "y";
}
}

IEnumerator stopSlide()
{

yield return new WaitForSeconds(0.5F);
//horizVel = 0;
controlBlocked = "n";
}

But when I start the game the object doesn’t move to right or left just forward…
But if I change

GetComponent<Rigidbody>().velocity = new Vector3(horizVel, 0, forwardSpeed);

to

GetComponent<Rigidbody>().velocity = new Vector3(20, 0, forwardSpeed);

then the object move this direction, so I think it doesn’t use the Input.GetKeyDown command…

I use UNITY 5.6.1f1
Can someone help me?

It’s something with the controlBlocked string, because if I comment this sections it works fine, but if I double tap on the “d”’ key it will slips away…

Using code tags properly - Unity Engine - Unity Discussions Use code tags, makes it easier to read.

Don’t use a string, use a bool. Bools are designed for true and false.

Your laneNum is set to 0 at the top. How many lanes do you have? What lane does your char start in? How are those lanes numbered? If it isn’t overwritten in the inspector, at lane 0 you can only go right. But then you’d be in lane 1 and you could still only go right.

I also don’t understand what you are saying. You do have it working if you remove the controlBlocked check?

1 Like

Thanks for your help, when I started to read your answer I realized what can I do :slight_smile:
Here is the working code:

    public KeyCode moveL;
    public KeyCode moveR;
    public int forwardSpeed;
    public float horizVel = 0;
    public float horizSpeed = 10;
    public int laneNum = 2;
    public bool controlBlocked;

    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        GetComponent<Rigidbody>().velocity = new Vector3(horizVel, 0, forwardSpeed);

        if (Input.GetKeyDown(moveL) && (laneNum > 1) && (controlBlocked == false))
        {
            horizVel = -horizSpeed;
            StartCoroutine(stopSlide());
            laneNum -= 1;
            controlBlocked = true;
        }

        if (Input.GetKeyDown(moveR) && (laneNum < 3) && (controlBlocked == false))
        {
            horizVel = horizSpeed;
            StartCoroutine(stopSlide());
            laneNum += 1;
            controlBlocked = true;
        }
        Debug.Log(horizVel);
        Debug.Log(controlBlocked);
    }

    IEnumerator stopSlide()
    {
        yield return new WaitForSeconds(.5f);
        horizVel = 0;
        controlBlocked = false;
    }

Now it’s working like a charm, thanks for your help :slight_smile:

Glad you got it working! :slight_smile:

1 Like