• 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 Sayden96 · Dec 21, 2020 at 06:53 PM · scripting beginnerfunction update2d controller

Make a sprint with time respawn

I have this code to my sprint function but when i run and the time is out i don't know how restart the time and run again the function to sprint. I need to know how to put a loop in this code.

 float sprintTime;

 if (Input.GetKey(KeyCode.LeftShift))
         {
             sprintTime += 1 / (1 / Time.deltaTime);
         }
 
         if (Input.GetKeyDown(KeyCode.LeftShift) && sprintTime <= 2f)
         {
             walkSpeed = 5.5f;
         }
 
         if (Input.GetKeyUp(KeyCode.LeftShift) || sprintTime > 2f)
         {
             walkSpeed = 2.5f;
         }
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
1
Best Answer

Answer by Pokedlg3 · Dec 21, 2020 at 08:06 PM

as I understand it, after sprintTime reaches 2, time runs out. However, time is increasing and not decreasing, so I think this resolves:

  float sprintTime;
  if (Input.GetKey(KeyCode.LeftShift))
      {
          sprintTime += 1 / (1 / Time.deltaTime);
      }
 
      if (Input.GetKeyDown(KeyCode.LeftShift) && sprintTime <= 2f)
      {
          walkSpeed = 5.5f;
      }
 
      if (Input.GetKeyUp(KeyCode.LeftShift) || sprintTime > 2f)
      {
          sprintTime = 0f;
          walkSpeed = 2.5f;
      }
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 Sayden96 · Dec 21, 2020 at 08:36 PM 0
Share

yes, it resolved but when the cycle ends of the sprint i need to put a limit of time to try to sprint again. its possible with delta time?

avatar image Pokedlg3 Sayden96 · Dec 21, 2020 at 09:22 PM 1
Share

Do you want to set a limit of 2, and when you reach the limit, do you want it to start counting again?

avatar image Sayden96 Pokedlg3 · Dec 21, 2020 at 09:31 PM 0
Share

I want to limit the sprite when use the key shift, because if I push shift intermittent the player runs always.

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

121 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 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 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 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 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 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

Move object when player reach trigger 2 Answers

Searching throguh array in custom inspector 0 Answers

Animation while walking 1 Answer

Help! This script is causing my Enemy to die on Spawn?? 2 Answers

How to access Leap motion's bone direction property within C# script ? 0 Answers

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