How to *only* change x coordinate with Vector3.MoveTowards while snapping y coordinate to a curve?












1















So in the back of my mind I'm sure there's a way to do this but I can't seem to figure it out. It's such a specific question I'm having a difficult time finding an answer in my internet searches.



I have an object that I want to smoothly follow my mouse, not snap to it, so I'm using Vector3.movetowards as so:



void Update () {

mousePos = camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, camera.nearClipPlane));

targetPos = new Vector3(mousePos.x, GetHeight(mousePos.x), PaddlePosition.z);

transform.position = Vector3.MoveTowards(transform.position, targetPos, moveSpeed);

}

private float GetHeight(float xCoordinate){

float height = Mathf.Abs(xCoordinate) * heightDeviation + PaddlePosition.y;
return height;
}


Basically when it goes right or left it goes slightly upwards based on how far away from 0 the mouse's x coordinate is. That part works.



The problem is if the mouse is moved rapidly from one side to the other, because of the nature of how vector3.movetowards works, I end up with the object following a different path on the y-axis than intended. I would like to retain the smooth, slightly delayed following motion I'm getting on the x axis but always have the y-axis locked to the coordinate generated by GetHeight().










share|improve this question



























    1















    So in the back of my mind I'm sure there's a way to do this but I can't seem to figure it out. It's such a specific question I'm having a difficult time finding an answer in my internet searches.



    I have an object that I want to smoothly follow my mouse, not snap to it, so I'm using Vector3.movetowards as so:



    void Update () {

    mousePos = camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, camera.nearClipPlane));

    targetPos = new Vector3(mousePos.x, GetHeight(mousePos.x), PaddlePosition.z);

    transform.position = Vector3.MoveTowards(transform.position, targetPos, moveSpeed);

    }

    private float GetHeight(float xCoordinate){

    float height = Mathf.Abs(xCoordinate) * heightDeviation + PaddlePosition.y;
    return height;
    }


    Basically when it goes right or left it goes slightly upwards based on how far away from 0 the mouse's x coordinate is. That part works.



    The problem is if the mouse is moved rapidly from one side to the other, because of the nature of how vector3.movetowards works, I end up with the object following a different path on the y-axis than intended. I would like to retain the smooth, slightly delayed following motion I'm getting on the x axis but always have the y-axis locked to the coordinate generated by GetHeight().










    share|improve this question

























      1












      1








      1


      1






      So in the back of my mind I'm sure there's a way to do this but I can't seem to figure it out. It's such a specific question I'm having a difficult time finding an answer in my internet searches.



      I have an object that I want to smoothly follow my mouse, not snap to it, so I'm using Vector3.movetowards as so:



      void Update () {

      mousePos = camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, camera.nearClipPlane));

      targetPos = new Vector3(mousePos.x, GetHeight(mousePos.x), PaddlePosition.z);

      transform.position = Vector3.MoveTowards(transform.position, targetPos, moveSpeed);

      }

      private float GetHeight(float xCoordinate){

      float height = Mathf.Abs(xCoordinate) * heightDeviation + PaddlePosition.y;
      return height;
      }


      Basically when it goes right or left it goes slightly upwards based on how far away from 0 the mouse's x coordinate is. That part works.



      The problem is if the mouse is moved rapidly from one side to the other, because of the nature of how vector3.movetowards works, I end up with the object following a different path on the y-axis than intended. I would like to retain the smooth, slightly delayed following motion I'm getting on the x axis but always have the y-axis locked to the coordinate generated by GetHeight().










      share|improve this question














      So in the back of my mind I'm sure there's a way to do this but I can't seem to figure it out. It's such a specific question I'm having a difficult time finding an answer in my internet searches.



      I have an object that I want to smoothly follow my mouse, not snap to it, so I'm using Vector3.movetowards as so:



      void Update () {

      mousePos = camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, camera.nearClipPlane));

      targetPos = new Vector3(mousePos.x, GetHeight(mousePos.x), PaddlePosition.z);

      transform.position = Vector3.MoveTowards(transform.position, targetPos, moveSpeed);

      }

      private float GetHeight(float xCoordinate){

      float height = Mathf.Abs(xCoordinate) * heightDeviation + PaddlePosition.y;
      return height;
      }


      Basically when it goes right or left it goes slightly upwards based on how far away from 0 the mouse's x coordinate is. That part works.



      The problem is if the mouse is moved rapidly from one side to the other, because of the nature of how vector3.movetowards works, I end up with the object following a different path on the y-axis than intended. I would like to retain the smooth, slightly delayed following motion I'm getting on the x axis but always have the y-axis locked to the coordinate generated by GetHeight().







      unity3d






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 25 '18 at 4:33









      AceAce

      1827




      1827
























          1 Answer
          1






          active

          oldest

          votes


















          1














          This maybe can be done with more control, and less code, via the constraints features of Unity.



          The position constraint's properties can all be animated, both from code and animation timelines, giving you incredible creative powers for minimal effort.



          Constrain your object to the mouse pointer via an empty object at the mouse pointer, and animate and/or set distance and relative properties in the constraint as desired.






          share|improve this answer
























          • I looked into using this as you suggested. I suppose I could constrain it to follow the mouse with the empty object and then dynamically update y based on the transform's x location but then I lose the aforementioned smooth, delayed "following" I got using Vector3.MoveTowards. Is there a way to get that form of motion using the constraints?

            – Ace
            Nov 25 '18 at 5:45






          • 1





            Yes, you adjust the distance of the constraint based on whatever smoothness you want. Could be based on the speed of the mouse and an amount of distance to the object and factored by time, or any other combination of contributing factors you like.

            – Confused
            Nov 25 '18 at 11:23











          • @Ace the best gradient I've found for these things is to use an ease curve, of the exponential type, through one of the tween libraries. DOTween has a free version, and here's another that's pretty cool: assetstore.unity.com/packages/tools/animation/leantween-3595

            – Confused
            Nov 25 '18 at 11:26






          • 1





            So I figured out a fairly simple way to do this thanks to your advice! I have the object following a MouseFollower object's x-axis only and updating it's own y-axis by way of it's own x coordinate. I simply used the same Vector3.moveTowards script I had on the object on the MouseFollower and that gave me the smooth motion in the x-axis I sought but retained the y-coordinates regardless of the speed the mouse moved. Thanks for your help!

            – Ace
            Nov 25 '18 at 14:06











          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%2f53464674%2fhow-to-only-change-x-coordinate-with-vector3-movetowards-while-snapping-y-coor%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














          This maybe can be done with more control, and less code, via the constraints features of Unity.



          The position constraint's properties can all be animated, both from code and animation timelines, giving you incredible creative powers for minimal effort.



          Constrain your object to the mouse pointer via an empty object at the mouse pointer, and animate and/or set distance and relative properties in the constraint as desired.






          share|improve this answer
























          • I looked into using this as you suggested. I suppose I could constrain it to follow the mouse with the empty object and then dynamically update y based on the transform's x location but then I lose the aforementioned smooth, delayed "following" I got using Vector3.MoveTowards. Is there a way to get that form of motion using the constraints?

            – Ace
            Nov 25 '18 at 5:45






          • 1





            Yes, you adjust the distance of the constraint based on whatever smoothness you want. Could be based on the speed of the mouse and an amount of distance to the object and factored by time, or any other combination of contributing factors you like.

            – Confused
            Nov 25 '18 at 11:23











          • @Ace the best gradient I've found for these things is to use an ease curve, of the exponential type, through one of the tween libraries. DOTween has a free version, and here's another that's pretty cool: assetstore.unity.com/packages/tools/animation/leantween-3595

            – Confused
            Nov 25 '18 at 11:26






          • 1





            So I figured out a fairly simple way to do this thanks to your advice! I have the object following a MouseFollower object's x-axis only and updating it's own y-axis by way of it's own x coordinate. I simply used the same Vector3.moveTowards script I had on the object on the MouseFollower and that gave me the smooth motion in the x-axis I sought but retained the y-coordinates regardless of the speed the mouse moved. Thanks for your help!

            – Ace
            Nov 25 '18 at 14:06
















          1














          This maybe can be done with more control, and less code, via the constraints features of Unity.



          The position constraint's properties can all be animated, both from code and animation timelines, giving you incredible creative powers for minimal effort.



          Constrain your object to the mouse pointer via an empty object at the mouse pointer, and animate and/or set distance and relative properties in the constraint as desired.






          share|improve this answer
























          • I looked into using this as you suggested. I suppose I could constrain it to follow the mouse with the empty object and then dynamically update y based on the transform's x location but then I lose the aforementioned smooth, delayed "following" I got using Vector3.MoveTowards. Is there a way to get that form of motion using the constraints?

            – Ace
            Nov 25 '18 at 5:45






          • 1





            Yes, you adjust the distance of the constraint based on whatever smoothness you want. Could be based on the speed of the mouse and an amount of distance to the object and factored by time, or any other combination of contributing factors you like.

            – Confused
            Nov 25 '18 at 11:23











          • @Ace the best gradient I've found for these things is to use an ease curve, of the exponential type, through one of the tween libraries. DOTween has a free version, and here's another that's pretty cool: assetstore.unity.com/packages/tools/animation/leantween-3595

            – Confused
            Nov 25 '18 at 11:26






          • 1





            So I figured out a fairly simple way to do this thanks to your advice! I have the object following a MouseFollower object's x-axis only and updating it's own y-axis by way of it's own x coordinate. I simply used the same Vector3.moveTowards script I had on the object on the MouseFollower and that gave me the smooth motion in the x-axis I sought but retained the y-coordinates regardless of the speed the mouse moved. Thanks for your help!

            – Ace
            Nov 25 '18 at 14:06














          1












          1








          1







          This maybe can be done with more control, and less code, via the constraints features of Unity.



          The position constraint's properties can all be animated, both from code and animation timelines, giving you incredible creative powers for minimal effort.



          Constrain your object to the mouse pointer via an empty object at the mouse pointer, and animate and/or set distance and relative properties in the constraint as desired.






          share|improve this answer













          This maybe can be done with more control, and less code, via the constraints features of Unity.



          The position constraint's properties can all be animated, both from code and animation timelines, giving you incredible creative powers for minimal effort.



          Constrain your object to the mouse pointer via an empty object at the mouse pointer, and animate and/or set distance and relative properties in the constraint as desired.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 25 '18 at 4:56









          ConfusedConfused

          3,08941752




          3,08941752













          • I looked into using this as you suggested. I suppose I could constrain it to follow the mouse with the empty object and then dynamically update y based on the transform's x location but then I lose the aforementioned smooth, delayed "following" I got using Vector3.MoveTowards. Is there a way to get that form of motion using the constraints?

            – Ace
            Nov 25 '18 at 5:45






          • 1





            Yes, you adjust the distance of the constraint based on whatever smoothness you want. Could be based on the speed of the mouse and an amount of distance to the object and factored by time, or any other combination of contributing factors you like.

            – Confused
            Nov 25 '18 at 11:23











          • @Ace the best gradient I've found for these things is to use an ease curve, of the exponential type, through one of the tween libraries. DOTween has a free version, and here's another that's pretty cool: assetstore.unity.com/packages/tools/animation/leantween-3595

            – Confused
            Nov 25 '18 at 11:26






          • 1





            So I figured out a fairly simple way to do this thanks to your advice! I have the object following a MouseFollower object's x-axis only and updating it's own y-axis by way of it's own x coordinate. I simply used the same Vector3.moveTowards script I had on the object on the MouseFollower and that gave me the smooth motion in the x-axis I sought but retained the y-coordinates regardless of the speed the mouse moved. Thanks for your help!

            – Ace
            Nov 25 '18 at 14:06



















          • I looked into using this as you suggested. I suppose I could constrain it to follow the mouse with the empty object and then dynamically update y based on the transform's x location but then I lose the aforementioned smooth, delayed "following" I got using Vector3.MoveTowards. Is there a way to get that form of motion using the constraints?

            – Ace
            Nov 25 '18 at 5:45






          • 1





            Yes, you adjust the distance of the constraint based on whatever smoothness you want. Could be based on the speed of the mouse and an amount of distance to the object and factored by time, or any other combination of contributing factors you like.

            – Confused
            Nov 25 '18 at 11:23











          • @Ace the best gradient I've found for these things is to use an ease curve, of the exponential type, through one of the tween libraries. DOTween has a free version, and here's another that's pretty cool: assetstore.unity.com/packages/tools/animation/leantween-3595

            – Confused
            Nov 25 '18 at 11:26






          • 1





            So I figured out a fairly simple way to do this thanks to your advice! I have the object following a MouseFollower object's x-axis only and updating it's own y-axis by way of it's own x coordinate. I simply used the same Vector3.moveTowards script I had on the object on the MouseFollower and that gave me the smooth motion in the x-axis I sought but retained the y-coordinates regardless of the speed the mouse moved. Thanks for your help!

            – Ace
            Nov 25 '18 at 14:06

















          I looked into using this as you suggested. I suppose I could constrain it to follow the mouse with the empty object and then dynamically update y based on the transform's x location but then I lose the aforementioned smooth, delayed "following" I got using Vector3.MoveTowards. Is there a way to get that form of motion using the constraints?

          – Ace
          Nov 25 '18 at 5:45





          I looked into using this as you suggested. I suppose I could constrain it to follow the mouse with the empty object and then dynamically update y based on the transform's x location but then I lose the aforementioned smooth, delayed "following" I got using Vector3.MoveTowards. Is there a way to get that form of motion using the constraints?

          – Ace
          Nov 25 '18 at 5:45




          1




          1





          Yes, you adjust the distance of the constraint based on whatever smoothness you want. Could be based on the speed of the mouse and an amount of distance to the object and factored by time, or any other combination of contributing factors you like.

          – Confused
          Nov 25 '18 at 11:23





          Yes, you adjust the distance of the constraint based on whatever smoothness you want. Could be based on the speed of the mouse and an amount of distance to the object and factored by time, or any other combination of contributing factors you like.

          – Confused
          Nov 25 '18 at 11:23













          @Ace the best gradient I've found for these things is to use an ease curve, of the exponential type, through one of the tween libraries. DOTween has a free version, and here's another that's pretty cool: assetstore.unity.com/packages/tools/animation/leantween-3595

          – Confused
          Nov 25 '18 at 11:26





          @Ace the best gradient I've found for these things is to use an ease curve, of the exponential type, through one of the tween libraries. DOTween has a free version, and here's another that's pretty cool: assetstore.unity.com/packages/tools/animation/leantween-3595

          – Confused
          Nov 25 '18 at 11:26




          1




          1





          So I figured out a fairly simple way to do this thanks to your advice! I have the object following a MouseFollower object's x-axis only and updating it's own y-axis by way of it's own x coordinate. I simply used the same Vector3.moveTowards script I had on the object on the MouseFollower and that gave me the smooth motion in the x-axis I sought but retained the y-coordinates regardless of the speed the mouse moved. Thanks for your help!

          – Ace
          Nov 25 '18 at 14:06





          So I figured out a fairly simple way to do this thanks to your advice! I have the object following a MouseFollower object's x-axis only and updating it's own y-axis by way of it's own x coordinate. I simply used the same Vector3.moveTowards script I had on the object on the MouseFollower and that gave me the smooth motion in the x-axis I sought but retained the y-coordinates regardless of the speed the mouse moved. Thanks for your help!

          – Ace
          Nov 25 '18 at 14:06




















          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%2f53464674%2fhow-to-only-change-x-coordinate-with-vector3-movetowards-while-snapping-y-coor%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

          TypeError: fit_transform() missing 1 required positional argument: 'X'