title: Moving a 3d block on a surface left and right by touch button or image instead of A - D Keys
body: <p>I've made a little game with the help of YouTube tutorials and I have a problem.
  Everything is OK with the game on pc, I use A, and D to move left or right a cube to avoid obstacles in front.
  The Block has a forwardspeed to move by itself forward with a given speed.</p>
  
  <p>But I want to test my game on my Android Phone so I have to control the object with touchscreen Buttons or touching the left or right part of the mobile screen.
  With An or D keys it's easy, but with mobile touch... not so.
  Down below is the code I use now for moving and a picture.</p>
  
  <pre class="lang-cs prettyprint-override"><code>using UnityEngine;
  
  public class PlayerMovement : MonoBehaviour {
  
      // This is a reference to the Rigidbody component called "rb"
      public Rigidbody rb;
  
      public float forwardForce = 2000f;  // Variable that determines the forward force
      public float sidewaysForce = 500f;  // 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("d"))  // If the player is pressing the "d" key
          {
              // Add a force to the right
              rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
          }
  
          if (Input.GetKey("a"))  // If the player is pressing the "a" key
          {
              // Add a force to the left
              rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
          }
  
          if (rb.position.y &lt; -1f)
          {
              FindObjectOfType&lt;GameManager&gt;().EndGame();
          }
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/0ui0K.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0ui0K.png" alt="enter image description here"></a></p>