Unity3D - object moving up and down continuously within a y-axis range












1















I have a target that moves up and down, but I'm not sure how to limit it's vertical movement to a certain y-axis range. Thanks for any advice. Code is as follows:



using UnityEngine;
using System.Collections;

public class TargetMovementVertical : MonoBehaviour
{
public int maxSpeed;

private Vector3 startPosition;

// Use this for initialization
void Start ()
{
maxSpeed = 3;

startPosition = transform.position;
}

// Update is called once per frame
void Update ()
{
MoveVertical ();
}

void MoveVertical()
{
transform.position = new Vector3(transform.position.x, startPosition.y + Mathf.Sin(Time.time * maxSpeed), transform.position.z);

if(transform.position.y > 1.0f)
{
transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z);
}
else if(transform.position.y < -1.0f)
{
transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z);
}
}
}









share|improve this question



























    1















    I have a target that moves up and down, but I'm not sure how to limit it's vertical movement to a certain y-axis range. Thanks for any advice. Code is as follows:



    using UnityEngine;
    using System.Collections;

    public class TargetMovementVertical : MonoBehaviour
    {
    public int maxSpeed;

    private Vector3 startPosition;

    // Use this for initialization
    void Start ()
    {
    maxSpeed = 3;

    startPosition = transform.position;
    }

    // Update is called once per frame
    void Update ()
    {
    MoveVertical ();
    }

    void MoveVertical()
    {
    transform.position = new Vector3(transform.position.x, startPosition.y + Mathf.Sin(Time.time * maxSpeed), transform.position.z);

    if(transform.position.y > 1.0f)
    {
    transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z);
    }
    else if(transform.position.y < -1.0f)
    {
    transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z);
    }
    }
    }









    share|improve this question

























      1












      1








      1








      I have a target that moves up and down, but I'm not sure how to limit it's vertical movement to a certain y-axis range. Thanks for any advice. Code is as follows:



      using UnityEngine;
      using System.Collections;

      public class TargetMovementVertical : MonoBehaviour
      {
      public int maxSpeed;

      private Vector3 startPosition;

      // Use this for initialization
      void Start ()
      {
      maxSpeed = 3;

      startPosition = transform.position;
      }

      // Update is called once per frame
      void Update ()
      {
      MoveVertical ();
      }

      void MoveVertical()
      {
      transform.position = new Vector3(transform.position.x, startPosition.y + Mathf.Sin(Time.time * maxSpeed), transform.position.z);

      if(transform.position.y > 1.0f)
      {
      transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z);
      }
      else if(transform.position.y < -1.0f)
      {
      transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z);
      }
      }
      }









      share|improve this question














      I have a target that moves up and down, but I'm not sure how to limit it's vertical movement to a certain y-axis range. Thanks for any advice. Code is as follows:



      using UnityEngine;
      using System.Collections;

      public class TargetMovementVertical : MonoBehaviour
      {
      public int maxSpeed;

      private Vector3 startPosition;

      // Use this for initialization
      void Start ()
      {
      maxSpeed = 3;

      startPosition = transform.position;
      }

      // Update is called once per frame
      void Update ()
      {
      MoveVertical ();
      }

      void MoveVertical()
      {
      transform.position = new Vector3(transform.position.x, startPosition.y + Mathf.Sin(Time.time * maxSpeed), transform.position.z);

      if(transform.position.y > 1.0f)
      {
      transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z);
      }
      else if(transform.position.y < -1.0f)
      {
      transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z);
      }
      }
      }






      c# unity3d






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Sep 24 '14 at 7:19









      khmer2040khmer2040

      74412




      74412
























          2 Answers
          2






          active

          oldest

          votes


















          3














          Your question may have two meanings:



          1- If you want to limit y shifting to be within -1 to 1, use the following code:
          (e.g. if your original y equals 5, the result will be within range (4,6)



          transform.position = new Vector3(0, startPosition.y + Mathf.Sin(Time.time * maxSpeed), 0);


          2- If you want to make y value always within -1 to 1, use the following code:
          (your result y value will be within range (-1,1) regardless of original y value)



          transform.position = new Vector3(transform.position.x, Mathf.Sin(Time.time * maxSpeed), transform.position.z);





          share|improve this answer


























          • Sorry, what I mean is I have an object in the scene already, and I'm trying to make it move up and down relative to where it already is. I tried both and it still doesn't work properly. It made it jump even more further up and down the y-axis.

            – khmer2040
            Sep 24 '14 at 13:45








          • 1





            I have just tested my old solution and found the problem, I think that we should not add old y value, I replaced it with startPosition.y. Please check my updated solution.

            – Mohanad S. Hamed
            Sep 24 '14 at 14:25













          • I got it to work, but thanks so much for your help. I really appreciate it :)

            – khmer2040
            Sep 24 '14 at 20:41











          • If I want to limit y to shift within two numbers that I choose, how can I do that?

            – LiziPizi
            Aug 30 '16 at 22:34



















          1














          private Vector3 startPosition;
          bool up=true;
          // Use this for initialization
          void Start ()
          {
          //maxSpeed = 3;
          startPosition = transform.position;
          }
          // Update is called once per frame
          void Update ()
          {
          MoveVertical ();
          }
          void MoveVertical()
          {
          var temp=transform.position;
          print (up);
          if(up==true)
          {
          temp.y += 0.01f;
          transform.position=temp;
          if(transform.position.y>=0.39f)
          {
          up = false;
          }
          }
          if(up==false)
          {
          temp.y -= 0.01f;
          transform.position=temp;
          if(transform.position.y<=0.14f)
          {
          up = true;
          }
          }
          }


          Adjust your values according to your need.






          share|improve this answer

























            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%2f26010855%2funity3d-object-moving-up-and-down-continuously-within-a-y-axis-range%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3














            Your question may have two meanings:



            1- If you want to limit y shifting to be within -1 to 1, use the following code:
            (e.g. if your original y equals 5, the result will be within range (4,6)



            transform.position = new Vector3(0, startPosition.y + Mathf.Sin(Time.time * maxSpeed), 0);


            2- If you want to make y value always within -1 to 1, use the following code:
            (your result y value will be within range (-1,1) regardless of original y value)



            transform.position = new Vector3(transform.position.x, Mathf.Sin(Time.time * maxSpeed), transform.position.z);





            share|improve this answer


























            • Sorry, what I mean is I have an object in the scene already, and I'm trying to make it move up and down relative to where it already is. I tried both and it still doesn't work properly. It made it jump even more further up and down the y-axis.

              – khmer2040
              Sep 24 '14 at 13:45








            • 1





              I have just tested my old solution and found the problem, I think that we should not add old y value, I replaced it with startPosition.y. Please check my updated solution.

              – Mohanad S. Hamed
              Sep 24 '14 at 14:25













            • I got it to work, but thanks so much for your help. I really appreciate it :)

              – khmer2040
              Sep 24 '14 at 20:41











            • If I want to limit y to shift within two numbers that I choose, how can I do that?

              – LiziPizi
              Aug 30 '16 at 22:34
















            3














            Your question may have two meanings:



            1- If you want to limit y shifting to be within -1 to 1, use the following code:
            (e.g. if your original y equals 5, the result will be within range (4,6)



            transform.position = new Vector3(0, startPosition.y + Mathf.Sin(Time.time * maxSpeed), 0);


            2- If you want to make y value always within -1 to 1, use the following code:
            (your result y value will be within range (-1,1) regardless of original y value)



            transform.position = new Vector3(transform.position.x, Mathf.Sin(Time.time * maxSpeed), transform.position.z);





            share|improve this answer


























            • Sorry, what I mean is I have an object in the scene already, and I'm trying to make it move up and down relative to where it already is. I tried both and it still doesn't work properly. It made it jump even more further up and down the y-axis.

              – khmer2040
              Sep 24 '14 at 13:45








            • 1





              I have just tested my old solution and found the problem, I think that we should not add old y value, I replaced it with startPosition.y. Please check my updated solution.

              – Mohanad S. Hamed
              Sep 24 '14 at 14:25













            • I got it to work, but thanks so much for your help. I really appreciate it :)

              – khmer2040
              Sep 24 '14 at 20:41











            • If I want to limit y to shift within two numbers that I choose, how can I do that?

              – LiziPizi
              Aug 30 '16 at 22:34














            3












            3








            3







            Your question may have two meanings:



            1- If you want to limit y shifting to be within -1 to 1, use the following code:
            (e.g. if your original y equals 5, the result will be within range (4,6)



            transform.position = new Vector3(0, startPosition.y + Mathf.Sin(Time.time * maxSpeed), 0);


            2- If you want to make y value always within -1 to 1, use the following code:
            (your result y value will be within range (-1,1) regardless of original y value)



            transform.position = new Vector3(transform.position.x, Mathf.Sin(Time.time * maxSpeed), transform.position.z);





            share|improve this answer















            Your question may have two meanings:



            1- If you want to limit y shifting to be within -1 to 1, use the following code:
            (e.g. if your original y equals 5, the result will be within range (4,6)



            transform.position = new Vector3(0, startPosition.y + Mathf.Sin(Time.time * maxSpeed), 0);


            2- If you want to make y value always within -1 to 1, use the following code:
            (your result y value will be within range (-1,1) regardless of original y value)



            transform.position = new Vector3(transform.position.x, Mathf.Sin(Time.time * maxSpeed), transform.position.z);






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Sep 24 '14 at 14:22

























            answered Sep 24 '14 at 7:56









            Mohanad S. HamedMohanad S. Hamed

            1769




            1769













            • Sorry, what I mean is I have an object in the scene already, and I'm trying to make it move up and down relative to where it already is. I tried both and it still doesn't work properly. It made it jump even more further up and down the y-axis.

              – khmer2040
              Sep 24 '14 at 13:45








            • 1





              I have just tested my old solution and found the problem, I think that we should not add old y value, I replaced it with startPosition.y. Please check my updated solution.

              – Mohanad S. Hamed
              Sep 24 '14 at 14:25













            • I got it to work, but thanks so much for your help. I really appreciate it :)

              – khmer2040
              Sep 24 '14 at 20:41











            • If I want to limit y to shift within two numbers that I choose, how can I do that?

              – LiziPizi
              Aug 30 '16 at 22:34



















            • Sorry, what I mean is I have an object in the scene already, and I'm trying to make it move up and down relative to where it already is. I tried both and it still doesn't work properly. It made it jump even more further up and down the y-axis.

              – khmer2040
              Sep 24 '14 at 13:45








            • 1





              I have just tested my old solution and found the problem, I think that we should not add old y value, I replaced it with startPosition.y. Please check my updated solution.

              – Mohanad S. Hamed
              Sep 24 '14 at 14:25













            • I got it to work, but thanks so much for your help. I really appreciate it :)

              – khmer2040
              Sep 24 '14 at 20:41











            • If I want to limit y to shift within two numbers that I choose, how can I do that?

              – LiziPizi
              Aug 30 '16 at 22:34

















            Sorry, what I mean is I have an object in the scene already, and I'm trying to make it move up and down relative to where it already is. I tried both and it still doesn't work properly. It made it jump even more further up and down the y-axis.

            – khmer2040
            Sep 24 '14 at 13:45







            Sorry, what I mean is I have an object in the scene already, and I'm trying to make it move up and down relative to where it already is. I tried both and it still doesn't work properly. It made it jump even more further up and down the y-axis.

            – khmer2040
            Sep 24 '14 at 13:45






            1




            1





            I have just tested my old solution and found the problem, I think that we should not add old y value, I replaced it with startPosition.y. Please check my updated solution.

            – Mohanad S. Hamed
            Sep 24 '14 at 14:25







            I have just tested my old solution and found the problem, I think that we should not add old y value, I replaced it with startPosition.y. Please check my updated solution.

            – Mohanad S. Hamed
            Sep 24 '14 at 14:25















            I got it to work, but thanks so much for your help. I really appreciate it :)

            – khmer2040
            Sep 24 '14 at 20:41





            I got it to work, but thanks so much for your help. I really appreciate it :)

            – khmer2040
            Sep 24 '14 at 20:41













            If I want to limit y to shift within two numbers that I choose, how can I do that?

            – LiziPizi
            Aug 30 '16 at 22:34





            If I want to limit y to shift within two numbers that I choose, how can I do that?

            – LiziPizi
            Aug 30 '16 at 22:34













            1














            private Vector3 startPosition;
            bool up=true;
            // Use this for initialization
            void Start ()
            {
            //maxSpeed = 3;
            startPosition = transform.position;
            }
            // Update is called once per frame
            void Update ()
            {
            MoveVertical ();
            }
            void MoveVertical()
            {
            var temp=transform.position;
            print (up);
            if(up==true)
            {
            temp.y += 0.01f;
            transform.position=temp;
            if(transform.position.y>=0.39f)
            {
            up = false;
            }
            }
            if(up==false)
            {
            temp.y -= 0.01f;
            transform.position=temp;
            if(transform.position.y<=0.14f)
            {
            up = true;
            }
            }
            }


            Adjust your values according to your need.






            share|improve this answer






























              1














              private Vector3 startPosition;
              bool up=true;
              // Use this for initialization
              void Start ()
              {
              //maxSpeed = 3;
              startPosition = transform.position;
              }
              // Update is called once per frame
              void Update ()
              {
              MoveVertical ();
              }
              void MoveVertical()
              {
              var temp=transform.position;
              print (up);
              if(up==true)
              {
              temp.y += 0.01f;
              transform.position=temp;
              if(transform.position.y>=0.39f)
              {
              up = false;
              }
              }
              if(up==false)
              {
              temp.y -= 0.01f;
              transform.position=temp;
              if(transform.position.y<=0.14f)
              {
              up = true;
              }
              }
              }


              Adjust your values according to your need.






              share|improve this answer




























                1












                1








                1







                private Vector3 startPosition;
                bool up=true;
                // Use this for initialization
                void Start ()
                {
                //maxSpeed = 3;
                startPosition = transform.position;
                }
                // Update is called once per frame
                void Update ()
                {
                MoveVertical ();
                }
                void MoveVertical()
                {
                var temp=transform.position;
                print (up);
                if(up==true)
                {
                temp.y += 0.01f;
                transform.position=temp;
                if(transform.position.y>=0.39f)
                {
                up = false;
                }
                }
                if(up==false)
                {
                temp.y -= 0.01f;
                transform.position=temp;
                if(transform.position.y<=0.14f)
                {
                up = true;
                }
                }
                }


                Adjust your values according to your need.






                share|improve this answer















                private Vector3 startPosition;
                bool up=true;
                // Use this for initialization
                void Start ()
                {
                //maxSpeed = 3;
                startPosition = transform.position;
                }
                // Update is called once per frame
                void Update ()
                {
                MoveVertical ();
                }
                void MoveVertical()
                {
                var temp=transform.position;
                print (up);
                if(up==true)
                {
                temp.y += 0.01f;
                transform.position=temp;
                if(transform.position.y>=0.39f)
                {
                up = false;
                }
                }
                if(up==false)
                {
                temp.y -= 0.01f;
                transform.position=temp;
                if(transform.position.y<=0.14f)
                {
                up = true;
                }
                }
                }


                Adjust your values according to your need.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 16 '18 at 7:51









                Pang

                6,9881666105




                6,9881666105










                answered Nov 16 '18 at 7:48









                Mudabbir AliMudabbir Ali

                111




                111






























                    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%2f26010855%2funity3d-object-moving-up-and-down-continuously-within-a-y-axis-range%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

                    Bressuire

                    Vorschmack

                    Quarantine