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 TubeSocSamurai · Oct 03, 2014 at 09:19 AM · distanceinfinite runner

Temporarily Changing a Variable

What im trying to do is change a forward motion variable to 15 for 5 seconds then set it back to regular speed of 5 after the times ran out but later when your distance traveled is greater than 200 i want the speed to increase to 10 HERES MY CODE

 if (powerUpTime >= 5) {
             forwardMovementSpeed = 15.0f;
             cheesePOWActive = true;
             anim.SetBool("gotCheese",true);
 
 if (powerUpTime <= 0) {
             forwardMovementSpeed = 5.0f;
             cheesePOWActive = false;
             anim.SetBool("gotCheese",false);

And that works the problem is that when i try to use this code later it wont move the speed because its still seeing that forwardMovementSpeed = 5.0f;

 if (!dead && distanceTraveled > 200) {
                 forwardMovementSpeed = Mathf.Lerp (forwardMovementSpeed, maxSpeed, Time.deltaTime);
             }

by the way maxSpeed = 10 and this is all inside of the Update()

Comment
Add comment · Show 1
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 robertbu · Oct 03, 2014 at 09:26 AM 0
Share

Put your restore code in a separate function and use Invoke().

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by chrischu · Oct 03, 2014 at 09:31 AM

I think you misunderstood the Mathf.Lerp method. It is meant to interpolate between two values (first parameter and second parameter), the third parameter defines how far along the interpolation is (0 means the first value is used, 1 means second value is used, 0.5 means the average between the two is used).

Therefore you should not use Time.deltaTime since it represents the time between the last update call and the current one and therefore does not go from 0 to 1 like the third parameter of Mathf.Lerp should do, but rather jumps around between different values.

If correcting the Mathf.Lerp call does not help it would be good if you could post all the code in the Update method since the error most likely lies in the bits that you haven't shown.

Comment
Add comment · 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
0

Answer by TubeSocSamurai · Oct 09, 2014 at 06:42 PM

thanks i figured it out tho

Comment
Add comment · Show 1 · 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 chrischu · Oct 09, 2014 at 09:12 PM 0
Share

Still, the basic idea of Unity answers is to provide answers for problems and therefore it would be good if you could post your own solution as answer or mark another answer as accepted.

avatar image
0

Answer by Jopan · Oct 09, 2014 at 09:25 PM

 void GotCheese()
 {
      forwardMovementSpeed = 15.0f;
      cheesePOWActive = true;
      anim.SetBool("gotCheese",true);
 
      Invoke("CheesePowDeactivate", 5f); // Call function in 5 seconds
 }
  
 void CheesePowDeactivate()
 {
      forwardMovementSpeed = 5.0f;
      cheesePOWActive = false;
      anim.SetBool("gotCheese",false);
 }
 
 // As for increasing your speed
 bool speedIncreasing = false;
 float movementIncreaseRate = 1f;
 void Update()
 {
      if (!dead && distanceTraveled > 200 && !speedIncreasing)
      {
           speedIncreasing = true;
           StartCoroutine(IncreaseSpeed());
      }
 }
 
 IEnumerator IncreaseSpeed()
 {
      while(forwardMovementSpeed < maxSpeed)
      {
           forwardMovementSpeed += Time.deltaTime * movementIncreaseRate;
           yield return null;
      }
 }
Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Tracking distance after Instantiate 2 Answers

Making a 2d "radar" for space game 2 Answers

dynamic rotation distance 1 Answer

I need help about raycast and edges 2 Answers

Shader problem: length() en distance() 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