• 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 Lanipoop · Oct 25, 2010 at 01:02 AM · timespeedincreaseuppick

Increasing speed for a few seconds

How would I increase the speed of my character for just a few seconds?

This is what I've got, but it doesn't seem to work.

bool ApplyPickup (PlayerScript playerStatus) { switch (pickupType) { case PickupType.Health: healthPickup(); break;

     case PickupType.Speed:
     PlayerScript.moveSpeed += 5000;
     StartCoroutine("speedPickup");
     break;
 }

 return true;

}

IEnumerator speedPickup () { yield return new WaitForSeconds(3); PlayerScript.moveSpeed = PlayerScript.originalSpeed; }

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 DoneDesign · Oct 25, 2010 at 02:24 AM 0
Share

please edit your question with your moving script in it...

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by joedrigon · Oct 25, 2010 at 01:48 AM

You could set a variable to say, 0 for seconds then increment it with Time.time and when it hit say, 3 seconds you could stop the temp speed increase.

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 Lanipoop · Oct 25, 2010 at 02:38 AM 0
Share

Thank you! I tried it out, but it doesn't seem to work. I definitely did it wrong...

avatar image
0

Answer by Marowi · Oct 25, 2010 at 02:56 AM

Time.time is "the time in seconds since the start of the game".

I assume that speedPickup() is only called when you actually pick up the object, so it's only being called once. If it occurs more than 6 seconds after the start of the game, speedBoost will immediately set the moveSpeed back to the originalSpeed.

What you could do is:

void speedPickup () { AIscript.moveSpeed += 50; yield WaitForSeconds (6); revertSpeed(); }

void revertSpeed () { AIscript.moveSpeed = AIscript.originalSpeed; }

(Preserving original answer and adding edits below)


Have you tried adding debug printouts to your code? I use this to debug that the code is being executed when/where I'd expect.

bool ApplyPickup (PlayerScript playerStatus) { print("ApplyPickup() called"); // ApplyPickup() has been called switch (pickupType) { case PickupType.Health: healthPickup(); break;

     case PickupType.Speed:
       print("Speed pickup found");       // found pickup of Enum type 'Speed'
       PlayerScript.moveSpeed += 5000;
       StartCoroutine("speedPickup");
     break;
 }

 return true;

}

IEnumerator speedPickup () { print("speedPickup() running"); // speedPickup() Coroutine running yield return new WaitForSeconds(3); PlayerScript.moveSpeed = PlayerScript.originalSpeed; print("moveSpeed reverted to original"); // reverted the move speed }

If all 4 messages print out when you pickup the speed boost, then your problem is with the application of your moveSpeed to your player movement. Otherwise, seeing which messages do and don't appear will help track down the problem.

Comment
Add comment · Show 9 · 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 Lanipoop · Oct 25, 2010 at 03:16 AM 0
Share

The Invoke function you provided only seems to work when I put it in the Awake function, but not when I put it in speedPickup. speedPickup() is called in a Switch statement, not sure if that would be the reason why it's not working.

avatar image Marowi · Oct 25, 2010 at 03:20 AM 0
Share

I have limited experience with 'Invoke', so I've updated it to a more familiar method. If this doesn't work, could you perhaps update your question with the code that calls 'speedPickup' too?

avatar image Lanipoop · Oct 25, 2010 at 03:44 AM 0
Share

Hmm.. I totally thought that that would work, but it didn't... I've provided my updated code. (I changed "AIscript" to "PlayerScript" and tweaked a little bit of your code so that it was C# friendly)

avatar image Marowi · Oct 25, 2010 at 04:10 AM 0
Share

I would've thought so too! Not to worry, there's more debugging that we can do. See my updated answer, and please comment back with the results!

avatar image Lanipoop · Oct 25, 2010 at 04:22 AM 0
Share

Good idea! I've ended up with only "ApplyPickup() called", "Speed pickup found", and "speedPickup() running" showing, in that order.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

How To Add A Simple Speed Boost On Collision With A Specific Object With A Character Controller 0 Answers

Objects moving faster after time 1 Answer

Increase ball speed by time 1 Answer

Help with my "speeding up time" script 1 Answer

Increase time for rigidbody game objects? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges