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 Digital-Phantom · Feb 19, 2015 at 07:01 PM · coroutinetimerspeedwaitforsecondsienumerator

SpeedBoost won't reset : Problem with either WaitForSeconds or Coroutine (Solved)

I've attached a script to my player character that increases move speed once the player collects a certain item. First part is fine, player collects the item then sets off like his butt is on fire. However after 5 seconds I want the effect to wear off and here's where I'm getting my problem.

(just added a Dubug.Log to see if the coroutine is being called and it is so the problem(I'm guessing) is with my WaitForSeconds code)

C# script -

 using UnityEngine;
 using System.Collections;
 
 public class SpeedBoostActivate : MonoBehaviour
 {
 
     public GameObject SpeedIcon;
     public float walkSpeed = 7; // regular speed
     public float runSpeed = 20; // run speed
     private CharacterMotor chMotor;
 
     private bool Supersonic = false;
     
     
     // Use this for initialization
     void Start () 
     {
         chMotor =  GetComponent<CharacterMotor>();
     }
 
 
     void Update()
     {
     
     }
 
 
     void OnTriggerEnter(Collider collider)
     { 
         float speed = walkSpeed;
 
         if(collider.gameObject.name == "SpeedBooster")
         {
             speed = runSpeed;
             Supersonic = true;
         }
 
         chMotor.movement.maxForwardSpeed = speed; // set max speed
 
         StartCoroutine(ResetRunSpeed());
 
     }
 
 
         IEnumerator ResetRunSpeed()
         {
             Debug.Log ("This is working so far"); //Add this to see if coroutine is being called?
 
             //yield return new WaitForSeconds(5f);
 
             if(Supersonic == true)
             {
                 yield return new WaitForSeconds(5f);
                 walkSpeed = 7;
                 Supersonic = false;
             }
         }
 
 }
 
 

What am I doing wrong? As usual all help is greatly appreciated !

???

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
1
Best Answer

Answer by tanoshimi · Feb 19, 2015 at 07:15 PM

My guess is that the coroutine is working correctly, but that the rest of your logic is wrong. Why in the coroutine are you setting walkSpeed = 7; (when nothing has changed it anyway? And what's the point of setting superSonic when nothing else relies on it? And you never set the character motor max speed back to walkSpeed except for when it triggers something.

My guess is that you wanted to do something more like this:

 using UnityEngine;
 using System.Collections;
 public class SpeedBoostActivate : MonoBehaviour
 {
     public GameObject SpeedIcon;
     public float walkSpeed = 7; // regular speed
     public float runSpeed = 20; // run speed
     private CharacterMotor chMotor;
 
     // Use this for initialization
     void Start ()
     {
         chMotor = GetComponent<CharacterMotor>();
     }
     
     void OnTriggerEnter(Collider collider)
     {
         if(collider.gameObject.name == "SpeedBooster")
         {
             chMotor.movement.maxForwardSpeed = runSpeed;
             StartCoroutine(ResetRunSpeed());
         }
     }
     IEnumerator ResetRunSpeed()
     {
         Debug.Log ("This is working so far"); //Add this to see if coroutine is being called?
 
         yield return new WaitForSeconds(5f);
         chMotor.movement.maxForwardSpeed = walkSpeed;
     }
 }
Comment
Add comment · Show 2 · 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 Digital-Phantom · Feb 19, 2015 at 07:23 PM 0
Share

I was actually looking at what I'd wrote and thought how difficult I'd made what only needed to be a simple script.

Yours worked perfectly (but I'm guessing you already knew that...lol).

Thank you both

:)

avatar image tanoshimi · Feb 19, 2015 at 07:26 PM 0
Share

Nope - had no idea if it would work - reply was written on my phone. But I'm glad it helped! :)

avatar image
1

Answer by hexagonius · Feb 19, 2015 at 07:09 PM

  • Do a StartCoroutine, when you trigger the speedbooster, and set runspeed

  • Do a StopCoroutine and set walkspeed, if not

Use this for the coroutine

 IEnumerator ResetRunSpeed()
 {
   yield return new WaitForSeconds(5f);
   walkSpeed = 7;
 }



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

21 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 avatar image avatar image avatar image

Related Questions

How to make loop with call to IEnumerator actually pause? 2 Answers

wait a second before say path is valid? 2 Answers

yield return waitforseconds not waiting? 3 Answers

Need Help with a Null Reference 1 Answer

Coroutine doesn't work when called from another method. 3 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