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 Kikispoke · Feb 14, 2015 at 07:08 PM · javascriptcharactersprint

Sprint cooldown(javascript)

so i just finished making my sprinting/energy bar class (javascript) this is it so far

 #pragma strict
 import UnityEngine.UI;
 
 var energy : Image;
 var playerEnergy : float;
 var  maxEnergy : float = 1;
 
 private var chMotor : CharacterMotor;
 private var controller : CharacterController;
 
 function Start () {
     chMotor = GetComponent(CharacterMotor);
     controller = GetComponent(CharacterController);
     playerEnergy = maxEnergy;
 }
 
 function Update () {

     if(controller.velocity.magnitude > 0 && Input.GetKey(KeyCode.LeftShift))
     {
         chMotor.movement.maxForwardSpeed = 10;
         chMotor.movement.maxSidewaysSpeed = 10;
         playerEnergy -= Time.deltaTime / 3000;
         
         
         }
     else
     {
         chMotor.movement.maxForwardSpeed = 6;
         chMotor.movement.maxSidewaysSpeed = 6;
     }
     if(playerEnergy > maxEnergy)
     {
         playerEnergy = maxEnergy;
     }
     if(playerEnergy < 0)
     {
         playerEnergy = 0;
     }
     
     energy.fillAmount = playerEnergy;
 }

now im not sure how to make a sprint cooldown, im fairly new to coding overall and im really stuck with this. I tried using yield waitforseconds but im not sure how to use it, any ideas?

Comment
Add comment · Show 3
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 TargonStudios · Feb 15, 2015 at 03:30 AM 0
Share

What do you mean by "sprint cool down"? Not sure what you are trying to achieve.

avatar image Kikispoke · Feb 15, 2015 at 05:20 AM 0
Share

in most games after you sprint for a set amount of time youll go back to walking and you cant sprint for a while

avatar image TargonStudios · Feb 15, 2015 at 01:12 PM 0
Share

Ooh, ok, so lets say you ran, your energy goes from 100 to zero, you want it so the player can't start sprinting again for a few seconds (Even if it regenerates)?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by stevedarby02 · Feb 15, 2015 at 03:15 PM

Maybe something like this? Sure there is a better way, but this is a start

  #pragma strict
  import UnityEngine.UI;
  
  var energy : Image;
  var playerEnergy : float;
  var  maxEnergy : float = 1;
  var canSprintTrigger : boolean = true;
  
  private var chMotor : CharacterMotor;
  private var controller : CharacterController;
  
  function Start () {
      chMotor = GetComponent(CharacterMotor);
      controller = GetComponent(CharacterController);
      playerEnergy = maxEnergy;
  }
  
  function Update () {
  
      if(controller.velocity.magnitude > 0 && Input.GetKey(KeyCode.LeftShift) && canSprintTrigger == true)
      {
          chMotor.movement.maxForwardSpeed = 10;
          chMotor.movement.maxSidewaysSpeed = 10;
          playerEnergy -= Time.deltaTime / 3000;
          canRun();         
          
          }
      else
      {
          chMotor.movement.maxForwardSpeed = 6;
          chMotor.movement.maxSidewaysSpeed = 6;
      }
      if(playerEnergy > maxEnergy)
      {
          playerEnergy = maxEnergy;
      }
      if(playerEnergy < 0)
      {
          playerEnergy = 0;
      }
      
      energy.fillAmount = playerEnergy;
  }
  
  function canRun()
  {
          yield WaitForSeconds(10); //you'll need to play around with how long someone can sprint for before it's set to false. Then again how long you wait until it's set to true again
          canSprintTrigger = false;
          yield WaitForSeconds(10);
          canSprintTrigger = true;
  }
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 Kikispoke · Feb 15, 2015 at 04:08 PM 0
Share

i tried that in the beginning but i dont know were to go with that

avatar image
0

Answer by TargonStudios · Feb 15, 2015 at 04:28 PM

Is it something like this you're looking for?:

 #pragma strict
  import UnityEngine.UI;
  
  var energy : Image;
  var playerEnergy : float;
  var  maxEnergy : float = 1;
  
  var CanSprint = true;
  
  private var chMotor : CharacterMotor;
  private var controller : CharacterController;
  
  function Start () {
      chMotor = GetComponent(CharacterMotor);
      controller = GetComponent(CharacterController);
      playerEnergy = maxEnergy;
  }
  
  function SprintTimer(){
  yield WaitForSeconds(1);
  CanSprint = true;
  }
  
  function Update () {
  
       if(Input.GetKeyUp(KeyCode.LeftShift)&&CanSprint==true)
      {
      
      CanSprint=false;
      SprintTimer();
      
      }
  
      if(controller.velocity.magnitude > 0 && Input.GetKey(KeyCode.LeftShift))
      {
      if(CanSprint==true){
          chMotor.movement.maxForwardSpeed = 10;
          chMotor.movement.maxSidewaysSpeed = 10;
          playerEnergy -= Time.deltaTime / 3000;
          }else{
          
          chMotor.movement.maxForwardSpeed = 6;
          chMotor.movement.maxSidewaysSpeed = 6;
          
          }
          
          
          }
      else
      {
          chMotor.movement.maxForwardSpeed = 6;
          chMotor.movement.maxSidewaysSpeed = 6;
      }
      if(playerEnergy > maxEnergy)
      {
          playerEnergy = maxEnergy;
      }
      if(playerEnergy < 0)
      {
          playerEnergy = 0;
      }
      
      energy.fillAmount = playerEnergy;
  }
  
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

counting seconds that character is idle. 1 Answer

How can I tell Unity to wait to do something until after and animation has finished playing? 0 Answers

Accessing A Variable Across Script In A Child Object 0 Answers

How can I change the maxForwardSpeed variable in the CharacterMotor script form a different script? 1 Answer

How do i make a character accelerate and play an animation when the click left-shift 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