Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Lathurs · Apr 18, 2017 at 09:21 AM · playergamespeedturningincrease

Increase speed from 0f-10f over time

Hey Guys :) Maybe you can help me :) I want to increase the speed of an object from 0.0f to 10.0f over 1 minute (variable maybe i do 30 sec at least) without pressing anything so the speed increases when the game starts. I read so much solutions in this forum but nothing worked for me... or do that what i wanted. You can see my latest code down low. I really hope u can support me with ur experience ;)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Movement : MonoBehaviour
 {


     float currentSpeed = 0f;
     float maxSpeed = 10f;




     public float movementSpeed = 5.0f;
     public GameObject player;
 
     private float screenCenterX;
     private void Start()
     {
         // save the horizontal center of the screen
         screenCenterX = Screen.width * 0.5f;
     }
 
 
     private void Update()
     {

//////////////// Not really worked /////////////////////////////////////////////////////////////////////////////

         currentSpeed = Mathf.Lerp(currentSpeed, maxSpeed, Time.deltaTime);
         transform.position -= transform.forward * currentSpeed * Time.deltaTime;


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

         // if there are any touches currently
         if (Input.touchCount > 0)
         {
             // get the first one
             Touch firstTouch = Input.GetTouch(0);
 
             // if it began this frame
             if (firstTouch.phase == TouchPhase.Began)
             {
                 if (firstTouch.position.x > screenCenterX)
                 {
                     // if the touch position is to the right of center
                     // move right
 
                         StartCoroutine(RotateAround(Vector3.up, 90.0f, 0.35f));
 
                 }
                 else if (firstTouch.position.x < screenCenterX)
                 {
                     // if the touch position is to the left of center
                     // move left
                  
                         StartCoroutine(RotateAround(Vector3.up, -90.0f, 0.35f));
 
               
                 }
                 
             }
         }
         else
         {
             transform.position -= transform.forward * Time.deltaTime * movementSpeed;
 
         }
         
     }
     IEnumerator RotateAround(Vector3 axis, float angle, float duration)
     {
         float elapsed = 0.0f; 
         float rotated = 0.0f; 
         while (elapsed < duration)
         {
             float step = angle / duration * Time.deltaTime;
             transform.RotateAround(transform.position, axis, step);
             elapsed += Time.deltaTime;
             rotated += step;
             yield return null;
         }
         transform.RotateAround(transform.position, axis, angle - rotated);
     }
 }



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

1 Reply

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by Hellium · Apr 18, 2017 at 09:21 AM

Try something like this :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Movement : MonoBehaviour
 {
     float currentSpeed = 0f;
     float maxSpeed = 10f;
     public float movementSpeed = 5.0f;
     public GameObject player;
     private float screenCenterX;

     // New variables :
     public float accelerationTime = 60;    
     private float minSpeed ;
     private float time ;
 
     private void Start()
     {
         // save the horizontal center of the screen
         screenCenterX = Screen.width * 0.5f;
         minSpeed = currentSpeed; 
         time = 0 ;
     }
 
 
     private void Update()
     {
         // https://docs.unity3d.com/ScriptReference/Mathf.SmoothStep.html
         currentSpeed = Mathf.SmoothStep(minSpeed, maxSpeed, time / accelerationTime );
         transform.position -= transform.forward * currentSpeed * Time.deltaTime;
         time += Time.deltaTime ;
         // ....
     }
Comment
Add comment · Show 6 · 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 Lathurs · Apr 18, 2017 at 03:34 PM 0
Share

Hey :) Yes that is what i want ! it increases the speed over the seconds i write down in the script ^^ This was very useful thank you for this code it helps a lot ^^

avatar image Abdo023 · Apr 27, 2021 at 06:14 PM 0
Share

I know this is an old post but I was hoping you could still help me. I understand that "accelerationTime" is the duration which it takes to reach "maxSpeed" My question is how can I control the actual speeding up and the slowing down of the value?

avatar image Hellium Abdo023 · Apr 27, 2021 at 06:29 PM 0
Share
      currentSpeed = Mathf.SmoothStep($$anonymous$$Speed, maxSpeed, time / accelerationTime );


time tracks the time elapsed since the call to the Start method (since the start of the scene in most of the cases).


accelerationTime is the time needed for the object to reach its full speed ( maxSpeed )


When time is equal to 0, time / accelerationTime returns 0, so Mathf.SmoothStep($$anonymous$$Speed, maxSpeed, time / accelerationTime ) returns $$anonymous$$Speed


When time is equal or greater than accelerationTime, time / accelerationTime returns a value greater than 1, so Mathf.SmoothStep($$anonymous$$Speed, maxSpeed, time / accelerationTime ) returns maxSpeed


      transform.position -= transform.forward * currentSpeed * Time.deltaTime;

Since the speed (velocity) of an object is calculated as follow: v = (position at t₂ - position at t₁ ) / (Δt), it's easy to deduce that position at t₂ = position at t₁ + vΔt. Here v = -transform.forward * currentSpeed (`v` is the velocity vector)


      time += Time.deltaTime ;

Just keeps track of the timer


If you want the current speed to reach the value of maxSpeed in a shorter duration, decrease accelerationTime. If you want to keep the duration but influence how currentSpeed reaches the max speed, take a look at easing functions. Here, I've used Mathf.SmoothStep, but you could have used Lerp, or any other easing methods.

avatar image Abdo023 Hellium · Apr 28, 2021 at 11:14 PM 0
Share

Thank you very much for your help.

avatar image Bunny83 Abdo023 · Apr 27, 2021 at 06:37 PM 0
Share

You can't with the smooth step function as it has a built-in s-shape curve (specifically the h01 hermit spline function). You can find the definition of SmoothStep over here.


Smoothstep is the same as using an ordinary Lerp but the t value put through the hermit polynomial so it does not grow linear but in an S shape. You are free to come up with a different function that maps the 0 to 1 range back to the 0 to 1 range but has your desired behaviour. Though that's kinda tricky.


The easiest solution would be to simply use an AnimationCurve that you can adjust in the editor to your liking. Just make sure the input and output range is 0 to 1 and you're good to go ^^.

 public AnimationCurve speedRamp; // edit in the inspector
 
 // [ ... ]
 currentSpeed = Mathf.Lerp($$anonymous$$Speed, maxSpeed, speedRamp(time / accelerationTime));

Of course if you like you can simply drop the "/ accelerationTime" and now the animation curve would have seconds as input values. This may be useful if you want to describe a more complex speed behaviour over time. Keep in $$anonymous$$d that you can / should provide a reasonable wrap mode at the end of the curve since the time keeps increasing. Clamping the last value is usually what you want.

avatar image Abdo023 Bunny83 · Apr 28, 2021 at 11:13 PM 0
Share

Thanks for your help. You put me on the right track. I'm going to test that out.

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

6 People are following this question.

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

Related Questions

Pathfinding with specific end-direction and turning speed limit 0 Answers

I'm having issues increasing my lives 2 Answers

Player acts differently if not selected in the inspector 6 Answers

Increasing speed of Character Motor on collision help 2 Answers

Intensifying rate over time... 1 Answer


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