How change the Y position of moving forward spaceship in Unity 3D












0















i have start a project in unity 3d.I want to make a spaceship that moving forward,but when i pressed the ArrowUp then i want to change its y postion to
( currentpos+ 1.5 ) but i want this smoothly.
this is my code



transform.position += transform.forward * Time.deltaTime * 10f;

if (Input.GetKey (KeyCode.UpArrow))
transform.position = new Vector3 (transform.position.x, 5f,
transform.position.z);


through the above code the Y position of object can b changed but it work so fast and i want to make it smooth.
so please help me.










share|improve this question





























    0















    i have start a project in unity 3d.I want to make a spaceship that moving forward,but when i pressed the ArrowUp then i want to change its y postion to
    ( currentpos+ 1.5 ) but i want this smoothly.
    this is my code



    transform.position += transform.forward * Time.deltaTime * 10f;

    if (Input.GetKey (KeyCode.UpArrow))
    transform.position = new Vector3 (transform.position.x, 5f,
    transform.position.z);


    through the above code the Y position of object can b changed but it work so fast and i want to make it smooth.
    so please help me.










    share|improve this question



























      0












      0








      0








      i have start a project in unity 3d.I want to make a spaceship that moving forward,but when i pressed the ArrowUp then i want to change its y postion to
      ( currentpos+ 1.5 ) but i want this smoothly.
      this is my code



      transform.position += transform.forward * Time.deltaTime * 10f;

      if (Input.GetKey (KeyCode.UpArrow))
      transform.position = new Vector3 (transform.position.x, 5f,
      transform.position.z);


      through the above code the Y position of object can b changed but it work so fast and i want to make it smooth.
      so please help me.










      share|improve this question
















      i have start a project in unity 3d.I want to make a spaceship that moving forward,but when i pressed the ArrowUp then i want to change its y postion to
      ( currentpos+ 1.5 ) but i want this smoothly.
      this is my code



      transform.position += transform.forward * Time.deltaTime * 10f;

      if (Input.GetKey (KeyCode.UpArrow))
      transform.position = new Vector3 (transform.position.x, 5f,
      transform.position.z);


      through the above code the Y position of object can b changed but it work so fast and i want to make it smooth.
      so please help me.







      unity3d smoothing






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 days ago









      oldherl

      177110




      177110










      asked Jan 19 at 9:38









      Faisalmirza MirzaFaisalmirza Mirza

      124




      124
























          3 Answers
          3






          active

          oldest

          votes


















          0














          I think the best solution to your problem is to use Mathf.SmoothDamp.



          Example:



          private float targetY = 0f;
          private float verticalVelocity = 0f;
          private const float smoothTime = 1f;

          private void Update()
          {
          transform.position += transform.forward * Time.deltaTime * 10f;

          if (Input.GetKey(KeyCode.UpArrow))
          {
          targetY = 5f;
          }

          float y = Mathf.SmoothDamp(transform.position.y, targetY, ref verticalVelocity, smoothTime);
          transform.position = new Vector3 (transform.position.x, y, transform.position.z);

          }


          This example will smoothly change the y coordinate to 5 over the course of 1 second (you can change the smoothTime constant for a different time).






          share|improve this answer


























          • Hi dear, thanks for your response.but there are some question .

            – Faisalmirza Mirza
            Jan 19 at 11:15








          • 1





            but dear i have some question. 1) why you declare targetY variable and where i can use this? 2) when i run my project then my object position changed without any key press. but i want that when i press a key for long time or just press and released it then the y position should be changed smoothly :)

            – Faisalmirza Mirza
            Jan 19 at 11:25











          • oh thanks dear i checked again and now it work fine stay happy dear :)

            – Faisalmirza Mirza
            Jan 19 at 11:49











          • My mistake, sorry, I'll edit my answer

            – Julxzs
            Jan 19 at 14:32



















          0














          Based in your own code the easiest way for you to work it out could be something like this



          //this sets the X position
          transform.position += transform.forward * Time.deltaTime * 10f;

          //if the button is pressed then modify Y
          if (Input.GetKey (KeyCode.UpArrow))
          transform.position += new Vector3 (0, 5f * Time.deltaTime * y_speed,0);


          y_speed could be a public float y_speed = 1.0f in your script so you could modify it from the inspector to get the effect you want to achieve.



          Hope it helps!






          share|improve this answer
























          • thank for your response.:)

            – Faisalmirza Mirza
            Jan 19 at 11:27











          • @FaisalmirzaMirza if it's working now for you, please can you check this answer as the valid one?

            – Chopi
            Jan 20 at 14:56






          • 1





            I have check this its work fine but mathf.smoothdamp() function is much better it works more smoothly.

            – Faisalmirza Mirza
            Jan 20 at 15:18











          • Great! @FaisalmirzaMirza so give that answer the "green check!"

            – Chopi
            Jan 21 at 16:07



















          0














          Assuming your spaceship is a rigidbody, you should take a look at Rigidbody.AddForce
          https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html



          By working with forces, you can get a smooth movement in all directions very easily, and tweak it within the Rigidbody's parameters (like mass) without fiddling in the script again. It's part of the Unity physics model.



          If you only want to move in y-direction, input a vector like (0,1,0) but you can also input the Transform.forward vector of your spaceship's Gameobject. That way, it will always move the direction it is facing in.






          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%2f54265778%2fhow-change-the-y-position-of-moving-forward-spaceship-in-unity-3d%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            I think the best solution to your problem is to use Mathf.SmoothDamp.



            Example:



            private float targetY = 0f;
            private float verticalVelocity = 0f;
            private const float smoothTime = 1f;

            private void Update()
            {
            transform.position += transform.forward * Time.deltaTime * 10f;

            if (Input.GetKey(KeyCode.UpArrow))
            {
            targetY = 5f;
            }

            float y = Mathf.SmoothDamp(transform.position.y, targetY, ref verticalVelocity, smoothTime);
            transform.position = new Vector3 (transform.position.x, y, transform.position.z);

            }


            This example will smoothly change the y coordinate to 5 over the course of 1 second (you can change the smoothTime constant for a different time).






            share|improve this answer


























            • Hi dear, thanks for your response.but there are some question .

              – Faisalmirza Mirza
              Jan 19 at 11:15








            • 1





              but dear i have some question. 1) why you declare targetY variable and where i can use this? 2) when i run my project then my object position changed without any key press. but i want that when i press a key for long time or just press and released it then the y position should be changed smoothly :)

              – Faisalmirza Mirza
              Jan 19 at 11:25











            • oh thanks dear i checked again and now it work fine stay happy dear :)

              – Faisalmirza Mirza
              Jan 19 at 11:49











            • My mistake, sorry, I'll edit my answer

              – Julxzs
              Jan 19 at 14:32
















            0














            I think the best solution to your problem is to use Mathf.SmoothDamp.



            Example:



            private float targetY = 0f;
            private float verticalVelocity = 0f;
            private const float smoothTime = 1f;

            private void Update()
            {
            transform.position += transform.forward * Time.deltaTime * 10f;

            if (Input.GetKey(KeyCode.UpArrow))
            {
            targetY = 5f;
            }

            float y = Mathf.SmoothDamp(transform.position.y, targetY, ref verticalVelocity, smoothTime);
            transform.position = new Vector3 (transform.position.x, y, transform.position.z);

            }


            This example will smoothly change the y coordinate to 5 over the course of 1 second (you can change the smoothTime constant for a different time).






            share|improve this answer


























            • Hi dear, thanks for your response.but there are some question .

              – Faisalmirza Mirza
              Jan 19 at 11:15








            • 1





              but dear i have some question. 1) why you declare targetY variable and where i can use this? 2) when i run my project then my object position changed without any key press. but i want that when i press a key for long time or just press and released it then the y position should be changed smoothly :)

              – Faisalmirza Mirza
              Jan 19 at 11:25











            • oh thanks dear i checked again and now it work fine stay happy dear :)

              – Faisalmirza Mirza
              Jan 19 at 11:49











            • My mistake, sorry, I'll edit my answer

              – Julxzs
              Jan 19 at 14:32














            0












            0








            0







            I think the best solution to your problem is to use Mathf.SmoothDamp.



            Example:



            private float targetY = 0f;
            private float verticalVelocity = 0f;
            private const float smoothTime = 1f;

            private void Update()
            {
            transform.position += transform.forward * Time.deltaTime * 10f;

            if (Input.GetKey(KeyCode.UpArrow))
            {
            targetY = 5f;
            }

            float y = Mathf.SmoothDamp(transform.position.y, targetY, ref verticalVelocity, smoothTime);
            transform.position = new Vector3 (transform.position.x, y, transform.position.z);

            }


            This example will smoothly change the y coordinate to 5 over the course of 1 second (you can change the smoothTime constant for a different time).






            share|improve this answer















            I think the best solution to your problem is to use Mathf.SmoothDamp.



            Example:



            private float targetY = 0f;
            private float verticalVelocity = 0f;
            private const float smoothTime = 1f;

            private void Update()
            {
            transform.position += transform.forward * Time.deltaTime * 10f;

            if (Input.GetKey(KeyCode.UpArrow))
            {
            targetY = 5f;
            }

            float y = Mathf.SmoothDamp(transform.position.y, targetY, ref verticalVelocity, smoothTime);
            transform.position = new Vector3 (transform.position.x, y, transform.position.z);

            }


            This example will smoothly change the y coordinate to 5 over the course of 1 second (you can change the smoothTime constant for a different time).







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 19 at 14:32

























            answered Jan 19 at 9:56









            JulxzsJulxzs

            499415




            499415













            • Hi dear, thanks for your response.but there are some question .

              – Faisalmirza Mirza
              Jan 19 at 11:15








            • 1





              but dear i have some question. 1) why you declare targetY variable and where i can use this? 2) when i run my project then my object position changed without any key press. but i want that when i press a key for long time or just press and released it then the y position should be changed smoothly :)

              – Faisalmirza Mirza
              Jan 19 at 11:25











            • oh thanks dear i checked again and now it work fine stay happy dear :)

              – Faisalmirza Mirza
              Jan 19 at 11:49











            • My mistake, sorry, I'll edit my answer

              – Julxzs
              Jan 19 at 14:32



















            • Hi dear, thanks for your response.but there are some question .

              – Faisalmirza Mirza
              Jan 19 at 11:15








            • 1





              but dear i have some question. 1) why you declare targetY variable and where i can use this? 2) when i run my project then my object position changed without any key press. but i want that when i press a key for long time or just press and released it then the y position should be changed smoothly :)

              – Faisalmirza Mirza
              Jan 19 at 11:25











            • oh thanks dear i checked again and now it work fine stay happy dear :)

              – Faisalmirza Mirza
              Jan 19 at 11:49











            • My mistake, sorry, I'll edit my answer

              – Julxzs
              Jan 19 at 14:32

















            Hi dear, thanks for your response.but there are some question .

            – Faisalmirza Mirza
            Jan 19 at 11:15







            Hi dear, thanks for your response.but there are some question .

            – Faisalmirza Mirza
            Jan 19 at 11:15






            1




            1





            but dear i have some question. 1) why you declare targetY variable and where i can use this? 2) when i run my project then my object position changed without any key press. but i want that when i press a key for long time or just press and released it then the y position should be changed smoothly :)

            – Faisalmirza Mirza
            Jan 19 at 11:25





            but dear i have some question. 1) why you declare targetY variable and where i can use this? 2) when i run my project then my object position changed without any key press. but i want that when i press a key for long time or just press and released it then the y position should be changed smoothly :)

            – Faisalmirza Mirza
            Jan 19 at 11:25













            oh thanks dear i checked again and now it work fine stay happy dear :)

            – Faisalmirza Mirza
            Jan 19 at 11:49





            oh thanks dear i checked again and now it work fine stay happy dear :)

            – Faisalmirza Mirza
            Jan 19 at 11:49













            My mistake, sorry, I'll edit my answer

            – Julxzs
            Jan 19 at 14:32





            My mistake, sorry, I'll edit my answer

            – Julxzs
            Jan 19 at 14:32













            0














            Based in your own code the easiest way for you to work it out could be something like this



            //this sets the X position
            transform.position += transform.forward * Time.deltaTime * 10f;

            //if the button is pressed then modify Y
            if (Input.GetKey (KeyCode.UpArrow))
            transform.position += new Vector3 (0, 5f * Time.deltaTime * y_speed,0);


            y_speed could be a public float y_speed = 1.0f in your script so you could modify it from the inspector to get the effect you want to achieve.



            Hope it helps!






            share|improve this answer
























            • thank for your response.:)

              – Faisalmirza Mirza
              Jan 19 at 11:27











            • @FaisalmirzaMirza if it's working now for you, please can you check this answer as the valid one?

              – Chopi
              Jan 20 at 14:56






            • 1





              I have check this its work fine but mathf.smoothdamp() function is much better it works more smoothly.

              – Faisalmirza Mirza
              Jan 20 at 15:18











            • Great! @FaisalmirzaMirza so give that answer the "green check!"

              – Chopi
              Jan 21 at 16:07
















            0














            Based in your own code the easiest way for you to work it out could be something like this



            //this sets the X position
            transform.position += transform.forward * Time.deltaTime * 10f;

            //if the button is pressed then modify Y
            if (Input.GetKey (KeyCode.UpArrow))
            transform.position += new Vector3 (0, 5f * Time.deltaTime * y_speed,0);


            y_speed could be a public float y_speed = 1.0f in your script so you could modify it from the inspector to get the effect you want to achieve.



            Hope it helps!






            share|improve this answer
























            • thank for your response.:)

              – Faisalmirza Mirza
              Jan 19 at 11:27











            • @FaisalmirzaMirza if it's working now for you, please can you check this answer as the valid one?

              – Chopi
              Jan 20 at 14:56






            • 1





              I have check this its work fine but mathf.smoothdamp() function is much better it works more smoothly.

              – Faisalmirza Mirza
              Jan 20 at 15:18











            • Great! @FaisalmirzaMirza so give that answer the "green check!"

              – Chopi
              Jan 21 at 16:07














            0












            0








            0







            Based in your own code the easiest way for you to work it out could be something like this



            //this sets the X position
            transform.position += transform.forward * Time.deltaTime * 10f;

            //if the button is pressed then modify Y
            if (Input.GetKey (KeyCode.UpArrow))
            transform.position += new Vector3 (0, 5f * Time.deltaTime * y_speed,0);


            y_speed could be a public float y_speed = 1.0f in your script so you could modify it from the inspector to get the effect you want to achieve.



            Hope it helps!






            share|improve this answer













            Based in your own code the easiest way for you to work it out could be something like this



            //this sets the X position
            transform.position += transform.forward * Time.deltaTime * 10f;

            //if the button is pressed then modify Y
            if (Input.GetKey (KeyCode.UpArrow))
            transform.position += new Vector3 (0, 5f * Time.deltaTime * y_speed,0);


            y_speed could be a public float y_speed = 1.0f in your script so you could modify it from the inspector to get the effect you want to achieve.



            Hope it helps!







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 19 at 10:15









            ChopiChopi

            8861921




            8861921













            • thank for your response.:)

              – Faisalmirza Mirza
              Jan 19 at 11:27











            • @FaisalmirzaMirza if it's working now for you, please can you check this answer as the valid one?

              – Chopi
              Jan 20 at 14:56






            • 1





              I have check this its work fine but mathf.smoothdamp() function is much better it works more smoothly.

              – Faisalmirza Mirza
              Jan 20 at 15:18











            • Great! @FaisalmirzaMirza so give that answer the "green check!"

              – Chopi
              Jan 21 at 16:07



















            • thank for your response.:)

              – Faisalmirza Mirza
              Jan 19 at 11:27











            • @FaisalmirzaMirza if it's working now for you, please can you check this answer as the valid one?

              – Chopi
              Jan 20 at 14:56






            • 1





              I have check this its work fine but mathf.smoothdamp() function is much better it works more smoothly.

              – Faisalmirza Mirza
              Jan 20 at 15:18











            • Great! @FaisalmirzaMirza so give that answer the "green check!"

              – Chopi
              Jan 21 at 16:07

















            thank for your response.:)

            – Faisalmirza Mirza
            Jan 19 at 11:27





            thank for your response.:)

            – Faisalmirza Mirza
            Jan 19 at 11:27













            @FaisalmirzaMirza if it's working now for you, please can you check this answer as the valid one?

            – Chopi
            Jan 20 at 14:56





            @FaisalmirzaMirza if it's working now for you, please can you check this answer as the valid one?

            – Chopi
            Jan 20 at 14:56




            1




            1





            I have check this its work fine but mathf.smoothdamp() function is much better it works more smoothly.

            – Faisalmirza Mirza
            Jan 20 at 15:18





            I have check this its work fine but mathf.smoothdamp() function is much better it works more smoothly.

            – Faisalmirza Mirza
            Jan 20 at 15:18













            Great! @FaisalmirzaMirza so give that answer the "green check!"

            – Chopi
            Jan 21 at 16:07





            Great! @FaisalmirzaMirza so give that answer the "green check!"

            – Chopi
            Jan 21 at 16:07











            0














            Assuming your spaceship is a rigidbody, you should take a look at Rigidbody.AddForce
            https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html



            By working with forces, you can get a smooth movement in all directions very easily, and tweak it within the Rigidbody's parameters (like mass) without fiddling in the script again. It's part of the Unity physics model.



            If you only want to move in y-direction, input a vector like (0,1,0) but you can also input the Transform.forward vector of your spaceship's Gameobject. That way, it will always move the direction it is facing in.






            share|improve this answer




























              0














              Assuming your spaceship is a rigidbody, you should take a look at Rigidbody.AddForce
              https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html



              By working with forces, you can get a smooth movement in all directions very easily, and tweak it within the Rigidbody's parameters (like mass) without fiddling in the script again. It's part of the Unity physics model.



              If you only want to move in y-direction, input a vector like (0,1,0) but you can also input the Transform.forward vector of your spaceship's Gameobject. That way, it will always move the direction it is facing in.






              share|improve this answer


























                0












                0








                0







                Assuming your spaceship is a rigidbody, you should take a look at Rigidbody.AddForce
                https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html



                By working with forces, you can get a smooth movement in all directions very easily, and tweak it within the Rigidbody's parameters (like mass) without fiddling in the script again. It's part of the Unity physics model.



                If you only want to move in y-direction, input a vector like (0,1,0) but you can also input the Transform.forward vector of your spaceship's Gameobject. That way, it will always move the direction it is facing in.






                share|improve this answer













                Assuming your spaceship is a rigidbody, you should take a look at Rigidbody.AddForce
                https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html



                By working with forces, you can get a smooth movement in all directions very easily, and tweak it within the Rigidbody's parameters (like mass) without fiddling in the script again. It's part of the Unity physics model.



                If you only want to move in y-direction, input a vector like (0,1,0) but you can also input the Transform.forward vector of your spaceship's Gameobject. That way, it will always move the direction it is facing in.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 19 at 10:33









                Commodore YourneroCommodore Yournero

                10017




                10017






























                    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%2f54265778%2fhow-change-the-y-position-of-moving-forward-spaceship-in-unity-3d%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

                    Liquibase includeAll doesn't find base path

                    How to use setInterval in EJS file?

                    Petrus Granier-Deferre