Moving a 3d block on a surface left and right by touch button or image instead of A - D Keys - UNITY and C#












0















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.



But.... and here comes the part where I am useless :))



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 A 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.



Thank you , any ideea is perfect.






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 < -1f)
{
FindObjectOfType<GameManager>().EndGame();
}
}
}





enter image description here










share|improve this question



























    0















    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.



    But.... and here comes the part where I am useless :))



    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 A 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.



    Thank you , any ideea is perfect.






    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 < -1f)
    {
    FindObjectOfType<GameManager>().EndGame();
    }
    }
    }





    enter image description here










    share|improve this question

























      0












      0








      0








      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.



      But.... and here comes the part where I am useless :))



      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 A 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.



      Thank you , any ideea is perfect.






      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 < -1f)
      {
      FindObjectOfType<GameManager>().EndGame();
      }
      }
      }





      enter image description here










      share|improve this question














      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.



      But.... and here comes the part where I am useless :))



      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 A 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.



      Thank you , any ideea is perfect.






      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 < -1f)
      {
      FindObjectOfType<GameManager>().EndGame();
      }
      }
      }





      enter image description here






      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 < -1f)
      {
      FindObjectOfType<GameManager>().EndGame();
      }
      }
      }





      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 < -1f)
      {
      FindObjectOfType<GameManager>().EndGame();
      }
      }
      }






      c# unity3d unityscript






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 9:12









      Cristian MergeaCristian Mergea

      348




      348
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Its not hard at all.
          Start by extracting the movement into seperate methods



          public void MoveRight()
          {
          rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
          }

          public void MoveLeft()
          {
          rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
          }

          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
          MoveRight();
          if (Input.GetKey("a")) // If the player is pressing the "a" key
          MoveLeft();
          }


          All that's left to do is bind your buttons to those methods in the editor






          share|improve this answer
























          • Oh, Thank you. I will try right now.

            – Cristian Mergea
            Nov 23 '18 at 9:24











          • It's perfect, a few tweeks and works perfect. Thank you.

            – Cristian Mergea
            Nov 23 '18 at 9:35














          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443618%2fmoving-a-3d-block-on-a-surface-left-and-right-by-touch-button-or-image-instead-o%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Its not hard at all.
          Start by extracting the movement into seperate methods



          public void MoveRight()
          {
          rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
          }

          public void MoveLeft()
          {
          rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
          }

          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
          MoveRight();
          if (Input.GetKey("a")) // If the player is pressing the "a" key
          MoveLeft();
          }


          All that's left to do is bind your buttons to those methods in the editor






          share|improve this answer
























          • Oh, Thank you. I will try right now.

            – Cristian Mergea
            Nov 23 '18 at 9:24











          • It's perfect, a few tweeks and works perfect. Thank you.

            – Cristian Mergea
            Nov 23 '18 at 9:35


















          1














          Its not hard at all.
          Start by extracting the movement into seperate methods



          public void MoveRight()
          {
          rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
          }

          public void MoveLeft()
          {
          rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
          }

          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
          MoveRight();
          if (Input.GetKey("a")) // If the player is pressing the "a" key
          MoveLeft();
          }


          All that's left to do is bind your buttons to those methods in the editor






          share|improve this answer
























          • Oh, Thank you. I will try right now.

            – Cristian Mergea
            Nov 23 '18 at 9:24











          • It's perfect, a few tweeks and works perfect. Thank you.

            – Cristian Mergea
            Nov 23 '18 at 9:35
















          1












          1








          1







          Its not hard at all.
          Start by extracting the movement into seperate methods



          public void MoveRight()
          {
          rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
          }

          public void MoveLeft()
          {
          rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
          }

          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
          MoveRight();
          if (Input.GetKey("a")) // If the player is pressing the "a" key
          MoveLeft();
          }


          All that's left to do is bind your buttons to those methods in the editor






          share|improve this answer













          Its not hard at all.
          Start by extracting the movement into seperate methods



          public void MoveRight()
          {
          rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
          }

          public void MoveLeft()
          {
          rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
          }

          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
          MoveRight();
          if (Input.GetKey("a")) // If the player is pressing the "a" key
          MoveLeft();
          }


          All that's left to do is bind your buttons to those methods in the editor







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 9:23









          zambarizambari

          2,2331316




          2,2331316













          • Oh, Thank you. I will try right now.

            – Cristian Mergea
            Nov 23 '18 at 9:24











          • It's perfect, a few tweeks and works perfect. Thank you.

            – Cristian Mergea
            Nov 23 '18 at 9:35





















          • Oh, Thank you. I will try right now.

            – Cristian Mergea
            Nov 23 '18 at 9:24











          • It's perfect, a few tweeks and works perfect. Thank you.

            – Cristian Mergea
            Nov 23 '18 at 9:35



















          Oh, Thank you. I will try right now.

          – Cristian Mergea
          Nov 23 '18 at 9:24





          Oh, Thank you. I will try right now.

          – Cristian Mergea
          Nov 23 '18 at 9:24













          It's perfect, a few tweeks and works perfect. Thank you.

          – Cristian Mergea
          Nov 23 '18 at 9:35







          It's perfect, a few tweeks and works perfect. Thank you.

          – Cristian Mergea
          Nov 23 '18 at 9:35






















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443618%2fmoving-a-3d-block-on-a-surface-left-and-right-by-touch-button-or-image-instead-o%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          RAC Tourist Trophy