Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Kasigawa · Jul 26, 2013 at 10:36 PM · speedsmoothfadeincrement

How do I get a value to increment smoothly?

How do I get a variable that’s set to a value to increment smoothly (or smoothly ease/ fade) to a target value? Such as speeding or slowing a plane during flight. Here’s my code below:

 #pragma strict
 
 var speed : int;
 var maxSpeed : int = 100;
 var minSpeed : int = 0;
 var startSpeed : int = 50;
 var camera : GameObject;
 var zoomCamera : GameObject;
 var enemyKillRatio : int = 0;
 var health : int = 100;
 var reset : boolean = false;
 
 
 function Update () {
 
     //start speed
     speed = startSpeed;
     
     //constant forward
     transform.Translate(0, 0, speed * -1 * Time.deltaTime); //speed set at 50 here.
     
     
     
     //movments; speedup    
     if (Input.GetKey("c")) { 
         //smoothly incrimint speed up by 1.
         speed = ++ speed;//needs incrment speed.
         
         transform.Translate(0, 0, speed * -1 * Time.deltaTime);
     }
     //movements; slowdown
     if (Input.GetKey("v")) { 
         //smoothly deincrament speed by 1.
         speed = -- speed;//need increment speed.
         
         transform.Translate(0, 0, speed * Time.deltaTime);
     }
     
     
     
     //limit speed
     if(speed >= maxSpeed){
         speed = maxSpeed;
     }
     if(speed >= minSpeed){
         speed = minSpeed;
     }
     
                 
                                 
 }
 

How do I get my movement speed up and movement slow down to smoothly ease to a max or a minimum value? The output of the code so far allows the player fly forward. When c key is pushed, the player goes faster (until maxSpeed = 100). When v key is pushed the player stops instantly (when minSpeed = 0).

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Rambit · Jul 26, 2013 at 11:23 PM

Maybe you can try to use Mathf.Lerp to smooth out?

Something like this: (untested)

 speed = Mathf.Lerp (speed, minSpeed, smoothTime);

when you press the C key. And then this to smooth to max speed:

 speed = Mathf.Lerp (speed, maxSpeed, smoothTime);


Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kasigawa · Jul 27, 2013 at 04:40 AM 0
Share

I've tryed everything. Nothing seems to work.

 #pragma strict
 
 var speed : int;
 var maxSpeed : int = 100;
 var $$anonymous$$Speed : int = 0;
 var startSpeed : int = 50;
 var camera : GameObject;
 var zoomCamera : GameObject;
 var enemy$$anonymous$$illRatio : int = 0;
 var health : int = 100;
 var reset : boolean = false;
 //var smoothTime : float = 1.0;
 
 function Update () {
 
     //start speed
     speed = startSpeed;
     
     //constant forward
     transform.Translate(0, 0, speed * -1 * Time.deltaTime); //speed set at 50 here.
     
     
     
     //movments; speedup    
     if (Input.Get$$anonymous$$ey("c")) { 
         //smoothly incri$$anonymous$$t speed up by 1.
         //speed = ++ speed;
         //speed = $$anonymous$$athf.Lerp (speed, maxSpeed, smoothTime);
         
         //transform.Translate(0, 0, speed * -1 * Time.deltaTime);
         //transform.Translate(0, 0, $$anonymous$$athf.Lerp(speed, maxSpeed, smoothTime) * -1* Time.deltaTime);
     }
     //movements; slowdown
     if (Input.Get$$anonymous$$ey("v")) { 
         //smoothly deincrament speed by 1.
         //speed = -- speed;
         //speed = $$anonymous$$athf.Lerp (speed, $$anonymous$$Speed, smoothTime);
         //speed = $$anonymous$$athf.Lerp($$anonymous$$Speed, maxSpeed, timePassed/accelerateTime);
         //speed = $$anonymous$$athf.Lerp (speed, $$anonymous$$Speed, smoothTime);
         
         //transform.Translate(0, 0, $$anonymous$$athf.Lerp(speed, $$anonymous$$Speed, smoothTime)* Time.deltaTime);
         //transform.Translate(0, 0, speed * Time.deltaTime);
     
     }
     
     
     
     //limit speed
     if(speed >= maxSpeed){
         speed = maxSpeed;
     }
     if(speed <= $$anonymous$$Speed){
         speed = $$anonymous$$Speed;
     }
     
     
 }
avatar image Rambit · Jul 27, 2013 at 09:04 AM 0
Share

I tried it and it worked for me by doing:

 transform.Translate(0, 0, speed * -1 * Time.deltaTime);
         if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.J))
         {
             speed = $$anonymous$$athf.Lerp (speed, maxSpeed, smoothTime * Time.deltaTime);
         }
         
         if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.$$anonymous$$))
         {
             speed = $$anonymous$$athf.Lerp (speed, $$anonymous$$Speed, smoothTime * Time.deltaTime);
         }

In the Update() function. Also make sure to make the speed variables floats, because $$anonymous$$athf.Lerp doesn't take ints. $$anonymous$$ake sure to add * Time.deltaTime after smoothTime so it will be frame rate independant.

avatar image Kasigawa · Jul 27, 2013 at 04:17 PM 0
Share

Thank you for the reply. I did exactly as you did and found the results the same. Nothing seems to be working. Could it be something in the Inspector or Input? Here’s my code:

 #pragma strict
 
 var speed : float;
 var maxSpeed : float = 100;
 var $$anonymous$$Speed : float = 0;
 var startSpeed : float = 50;
 var camera : GameObject;
 var zoomCamera : GameObject;
 var enemy$$anonymous$$illRatio : int = 0;
 var health : int = 100;
 var reset : boolean = false;
 //-----------------------------
 var smoothTime : float = 0.5;
 
 //-----------------------------
 
 function Update () {
 
     //start speed
     speed = startSpeed;
     
     //constant forward
     transform.Translate(0, 0, speed * -1 * Time.deltaTime); //speed set at 50 here.
     
     //-----------------------------------------------------------------------------------------------
     if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.J))
        {
          speed = $$anonymous$$athf.Lerp (speed, maxSpeed, smoothTime * Time.deltaTime);
        }
  
        if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.$$anonymous$$))
        {
          speed = $$anonymous$$athf.Lerp (speed, $$anonymous$$Speed, smoothTime * Time.deltaTime);
        }
     
     
     //------------------------------------------------------------------------------------------------
     
     //limit speed
     if(speed >= maxSpeed){
         speed = maxSpeed;
     }
     if(speed <= $$anonymous$$Speed){
         speed = $$anonymous$$Speed;
     }
 }
avatar image
0

Answer by tw1st3d · Jul 27, 2013 at 04:21 PM

 var lastInc = 0.0;
 var toInc = 0;
 while(Input.GetKeyDown(KeyCode.C))
 {
     if(( Time.time > lastInc + 0.05))
     {
         toInc++;
         lastInc = Time.time;
     }
 }

Set it up to wait .05 seconds in between incrementation.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kasigawa · Jul 27, 2013 at 05:09 PM 0
Share

I want to thank you for the reply; nevertheless, your code makes my browser crash.

avatar image tw1st3d · Jul 27, 2013 at 05:12 PM 1
Share

That's.. strange.

avatar image Kasigawa · Jul 27, 2013 at 05:17 PM 0
Share

It seems I can't use while loops. I'm not sure why.

avatar image tw1st3d · Jul 27, 2013 at 05:19 PM 0
Share

Try increasing the time gap required to perhaps 0.1 seconds

avatar image tw1st3d · Jul 27, 2013 at 05:20 PM 0
Share

Also, this is untested, but you could try

 lastInc = lastInc + (1 * Time.deltaTime);
Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Fast moving object jitter. RK4 integration 1 Answer

Rotate camera with easeout 0 Answers

Problem : Change speed animation when run it not smooth 1 Answer

Smooth terrain LOD transitions? 1 Answer

How to match the speed of objects and the spawn speed of objects? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges