I have an endless runner game i can move my player left and right by using keyboard keys but i dont know how to make my player move left and right in mobile device ?? please can anyone give me a script for my player to move left and right on mobile device ...

here is my script

using UnityEngine;

public class PlayerMovement : MonoBehaviour {

    // This is a reference to the Rigidbody component called "rb"
    public Rigidbody rb;

    public float forwardForce = 3000f;  // Variable that determines the forward force
    public float sidewaysForce = 43f;
    // Variable that determines the sideways force

    // We marked this as "Fixed"Update because we
    // are using it to mess with physics

    void FixedUpdate ()
    {
        // Add a forward force
        rb.AddForce(0, 0, forwardForce * Time.deltaTime);

        if (Input.GetKey("k"))  // If the player is pressing the "d" key
        {
            // Add a force to the right
            rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0);
        }

        if (Input.GetKey("d"))  // If the player is pressing the "a" key
        {
            // Add a force to the left
            rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0);
        }

        if (rb.position.y < -1f)
        {
            FindObjectOfType<GameManager>().EndGame();
        }


    }
}

0 Answers