unity smooth 2D horizontal transform












0














Currently, I have a script for mobile that simply has a ball on the screen, and when you touch to the left of the ball, it moves left, and when you touch to its right, it moves right. However, when it moves, it is not smooth unless I lower the float for how much it moves by (which causes it to be too slow). How can I maintain a fast translation speed whilst the movement being smooth?



if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);

var point = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y,
transform.position.z - Camera.main.transform.position.z));

float dx = point.x - transform.position.x;
if (point.x < transform.position.x)
{
//transform.Translate(-15f * Time.deltaTime, 0, 0);

transform.Translate (dx * (1 - Mathf.Pow (0.5f, Time.deltaTime / halfWayTime)), 0, 0);


}
else if (point.x >= transform.position.x + 0.5f)
{
//transform.Translate(15f * Time.deltaTime, 0, 0);
transform.Translate (dx * (1 - Mathf.Pow (0.5f, Time.deltaTime / halfWayTime)), 0, 0);
}
}









share|improve this question





























    0














    Currently, I have a script for mobile that simply has a ball on the screen, and when you touch to the left of the ball, it moves left, and when you touch to its right, it moves right. However, when it moves, it is not smooth unless I lower the float for how much it moves by (which causes it to be too slow). How can I maintain a fast translation speed whilst the movement being smooth?



    if (Input.touchCount > 0)
    {
    Touch touch = Input.GetTouch(0);

    var point = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y,
    transform.position.z - Camera.main.transform.position.z));

    float dx = point.x - transform.position.x;
    if (point.x < transform.position.x)
    {
    //transform.Translate(-15f * Time.deltaTime, 0, 0);

    transform.Translate (dx * (1 - Mathf.Pow (0.5f, Time.deltaTime / halfWayTime)), 0, 0);


    }
    else if (point.x >= transform.position.x + 0.5f)
    {
    //transform.Translate(15f * Time.deltaTime, 0, 0);
    transform.Translate (dx * (1 - Mathf.Pow (0.5f, Time.deltaTime / halfWayTime)), 0, 0);
    }
    }









    share|improve this question



























      0












      0








      0







      Currently, I have a script for mobile that simply has a ball on the screen, and when you touch to the left of the ball, it moves left, and when you touch to its right, it moves right. However, when it moves, it is not smooth unless I lower the float for how much it moves by (which causes it to be too slow). How can I maintain a fast translation speed whilst the movement being smooth?



      if (Input.touchCount > 0)
      {
      Touch touch = Input.GetTouch(0);

      var point = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y,
      transform.position.z - Camera.main.transform.position.z));

      float dx = point.x - transform.position.x;
      if (point.x < transform.position.x)
      {
      //transform.Translate(-15f * Time.deltaTime, 0, 0);

      transform.Translate (dx * (1 - Mathf.Pow (0.5f, Time.deltaTime / halfWayTime)), 0, 0);


      }
      else if (point.x >= transform.position.x + 0.5f)
      {
      //transform.Translate(15f * Time.deltaTime, 0, 0);
      transform.Translate (dx * (1 - Mathf.Pow (0.5f, Time.deltaTime / halfWayTime)), 0, 0);
      }
      }









      share|improve this question















      Currently, I have a script for mobile that simply has a ball on the screen, and when you touch to the left of the ball, it moves left, and when you touch to its right, it moves right. However, when it moves, it is not smooth unless I lower the float for how much it moves by (which causes it to be too slow). How can I maintain a fast translation speed whilst the movement being smooth?



      if (Input.touchCount > 0)
      {
      Touch touch = Input.GetTouch(0);

      var point = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y,
      transform.position.z - Camera.main.transform.position.z));

      float dx = point.x - transform.position.x;
      if (point.x < transform.position.x)
      {
      //transform.Translate(-15f * Time.deltaTime, 0, 0);

      transform.Translate (dx * (1 - Mathf.Pow (0.5f, Time.deltaTime / halfWayTime)), 0, 0);


      }
      else if (point.x >= transform.position.x + 0.5f)
      {
      //transform.Translate(15f * Time.deltaTime, 0, 0);
      transform.Translate (dx * (1 - Mathf.Pow (0.5f, Time.deltaTime / halfWayTime)), 0, 0);
      }
      }






      c# unity3d






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 at 8:47

























      asked Nov 20 at 20:46









      Mattattack

      738




      738
























          1 Answer
          1






          active

          oldest

          votes


















          1














          float targetX;
          const float halfWayTime = 0.1f;

          void Update () {
          float dx = targetX - transform.position.x;
          transform.Translate (dx * (1 - Mathf.Pow (0.5f, Time.deltaTime / halfWayTime), 0, 0);
          }


          This creates movement pattern which is fast at first, but slows down in the end, and targetX can be edited at any time. halfWayTime is the time for which the object will complete half of its path to target, if it's 0.1, then for 0.1s it will move 50% of the initial distance, for 0.2s - 75%, for 0.3s - 87.5% and so on.



          By the way, you can modify this code to work with all coordinates:



          void Update () {
          transform.position = Vector3.Lerp (
          target,
          transform.position,
          Mathf.Pow (0.5f, Time.deltaTime / halfWayTime)
          );
          }





          share|improve this answer





















          • This works great, but only along the negative x axis, in the positive direction it seems to jump about a lot. Any idea why>
            – Mattattack
            Nov 20 at 21:54










          • It should work in both directions in the same manner. @Mattattack , did you somehow change this code for opposite direction? If yes, you shouldn't. And please say how you set targetX, maybe it's there
            – trollingchar
            Nov 21 at 7:51












          • For targetX, I worked out the point the users finger is touching, then worked it our relative to the world instead of the screen, I know this works fine. I have not changed the code for the other direction. I will update my question to show the new code.
            – Mattattack
            Nov 21 at 8:44










          • Simply removing the unnecessary if statement has seemed to got it working! Thanks for all the help!
            – Mattattack
            Nov 21 at 8:53











          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%2f53401249%2funity-smooth-2d-horizontal-transform%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














          float targetX;
          const float halfWayTime = 0.1f;

          void Update () {
          float dx = targetX - transform.position.x;
          transform.Translate (dx * (1 - Mathf.Pow (0.5f, Time.deltaTime / halfWayTime), 0, 0);
          }


          This creates movement pattern which is fast at first, but slows down in the end, and targetX can be edited at any time. halfWayTime is the time for which the object will complete half of its path to target, if it's 0.1, then for 0.1s it will move 50% of the initial distance, for 0.2s - 75%, for 0.3s - 87.5% and so on.



          By the way, you can modify this code to work with all coordinates:



          void Update () {
          transform.position = Vector3.Lerp (
          target,
          transform.position,
          Mathf.Pow (0.5f, Time.deltaTime / halfWayTime)
          );
          }





          share|improve this answer





















          • This works great, but only along the negative x axis, in the positive direction it seems to jump about a lot. Any idea why>
            – Mattattack
            Nov 20 at 21:54










          • It should work in both directions in the same manner. @Mattattack , did you somehow change this code for opposite direction? If yes, you shouldn't. And please say how you set targetX, maybe it's there
            – trollingchar
            Nov 21 at 7:51












          • For targetX, I worked out the point the users finger is touching, then worked it our relative to the world instead of the screen, I know this works fine. I have not changed the code for the other direction. I will update my question to show the new code.
            – Mattattack
            Nov 21 at 8:44










          • Simply removing the unnecessary if statement has seemed to got it working! Thanks for all the help!
            – Mattattack
            Nov 21 at 8:53
















          1














          float targetX;
          const float halfWayTime = 0.1f;

          void Update () {
          float dx = targetX - transform.position.x;
          transform.Translate (dx * (1 - Mathf.Pow (0.5f, Time.deltaTime / halfWayTime), 0, 0);
          }


          This creates movement pattern which is fast at first, but slows down in the end, and targetX can be edited at any time. halfWayTime is the time for which the object will complete half of its path to target, if it's 0.1, then for 0.1s it will move 50% of the initial distance, for 0.2s - 75%, for 0.3s - 87.5% and so on.



          By the way, you can modify this code to work with all coordinates:



          void Update () {
          transform.position = Vector3.Lerp (
          target,
          transform.position,
          Mathf.Pow (0.5f, Time.deltaTime / halfWayTime)
          );
          }





          share|improve this answer





















          • This works great, but only along the negative x axis, in the positive direction it seems to jump about a lot. Any idea why>
            – Mattattack
            Nov 20 at 21:54










          • It should work in both directions in the same manner. @Mattattack , did you somehow change this code for opposite direction? If yes, you shouldn't. And please say how you set targetX, maybe it's there
            – trollingchar
            Nov 21 at 7:51












          • For targetX, I worked out the point the users finger is touching, then worked it our relative to the world instead of the screen, I know this works fine. I have not changed the code for the other direction. I will update my question to show the new code.
            – Mattattack
            Nov 21 at 8:44










          • Simply removing the unnecessary if statement has seemed to got it working! Thanks for all the help!
            – Mattattack
            Nov 21 at 8:53














          1












          1








          1






          float targetX;
          const float halfWayTime = 0.1f;

          void Update () {
          float dx = targetX - transform.position.x;
          transform.Translate (dx * (1 - Mathf.Pow (0.5f, Time.deltaTime / halfWayTime), 0, 0);
          }


          This creates movement pattern which is fast at first, but slows down in the end, and targetX can be edited at any time. halfWayTime is the time for which the object will complete half of its path to target, if it's 0.1, then for 0.1s it will move 50% of the initial distance, for 0.2s - 75%, for 0.3s - 87.5% and so on.



          By the way, you can modify this code to work with all coordinates:



          void Update () {
          transform.position = Vector3.Lerp (
          target,
          transform.position,
          Mathf.Pow (0.5f, Time.deltaTime / halfWayTime)
          );
          }





          share|improve this answer












          float targetX;
          const float halfWayTime = 0.1f;

          void Update () {
          float dx = targetX - transform.position.x;
          transform.Translate (dx * (1 - Mathf.Pow (0.5f, Time.deltaTime / halfWayTime), 0, 0);
          }


          This creates movement pattern which is fast at first, but slows down in the end, and targetX can be edited at any time. halfWayTime is the time for which the object will complete half of its path to target, if it's 0.1, then for 0.1s it will move 50% of the initial distance, for 0.2s - 75%, for 0.3s - 87.5% and so on.



          By the way, you can modify this code to work with all coordinates:



          void Update () {
          transform.position = Vector3.Lerp (
          target,
          transform.position,
          Mathf.Pow (0.5f, Time.deltaTime / halfWayTime)
          );
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 at 21:12









          trollingchar

          1616




          1616












          • This works great, but only along the negative x axis, in the positive direction it seems to jump about a lot. Any idea why>
            – Mattattack
            Nov 20 at 21:54










          • It should work in both directions in the same manner. @Mattattack , did you somehow change this code for opposite direction? If yes, you shouldn't. And please say how you set targetX, maybe it's there
            – trollingchar
            Nov 21 at 7:51












          • For targetX, I worked out the point the users finger is touching, then worked it our relative to the world instead of the screen, I know this works fine. I have not changed the code for the other direction. I will update my question to show the new code.
            – Mattattack
            Nov 21 at 8:44










          • Simply removing the unnecessary if statement has seemed to got it working! Thanks for all the help!
            – Mattattack
            Nov 21 at 8:53


















          • This works great, but only along the negative x axis, in the positive direction it seems to jump about a lot. Any idea why>
            – Mattattack
            Nov 20 at 21:54










          • It should work in both directions in the same manner. @Mattattack , did you somehow change this code for opposite direction? If yes, you shouldn't. And please say how you set targetX, maybe it's there
            – trollingchar
            Nov 21 at 7:51












          • For targetX, I worked out the point the users finger is touching, then worked it our relative to the world instead of the screen, I know this works fine. I have not changed the code for the other direction. I will update my question to show the new code.
            – Mattattack
            Nov 21 at 8:44










          • Simply removing the unnecessary if statement has seemed to got it working! Thanks for all the help!
            – Mattattack
            Nov 21 at 8:53
















          This works great, but only along the negative x axis, in the positive direction it seems to jump about a lot. Any idea why>
          – Mattattack
          Nov 20 at 21:54




          This works great, but only along the negative x axis, in the positive direction it seems to jump about a lot. Any idea why>
          – Mattattack
          Nov 20 at 21:54












          It should work in both directions in the same manner. @Mattattack , did you somehow change this code for opposite direction? If yes, you shouldn't. And please say how you set targetX, maybe it's there
          – trollingchar
          Nov 21 at 7:51






          It should work in both directions in the same manner. @Mattattack , did you somehow change this code for opposite direction? If yes, you shouldn't. And please say how you set targetX, maybe it's there
          – trollingchar
          Nov 21 at 7:51














          For targetX, I worked out the point the users finger is touching, then worked it our relative to the world instead of the screen, I know this works fine. I have not changed the code for the other direction. I will update my question to show the new code.
          – Mattattack
          Nov 21 at 8:44




          For targetX, I worked out the point the users finger is touching, then worked it our relative to the world instead of the screen, I know this works fine. I have not changed the code for the other direction. I will update my question to show the new code.
          – Mattattack
          Nov 21 at 8:44












          Simply removing the unnecessary if statement has seemed to got it working! Thanks for all the help!
          – Mattattack
          Nov 21 at 8:53




          Simply removing the unnecessary if statement has seemed to got it working! Thanks for all the help!
          – Mattattack
          Nov 21 at 8:53


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53401249%2funity-smooth-2d-horizontal-transform%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

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          Refactoring coordinates for Minecraft Pi buildings written in Python