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 Fotoschnitzel · Apr 02, 2013 at 12:50 PM · timesprintofamount

Sprint for a amount of time

Hello i have a question. (I'm new to unity...) Can I make a sprint script that changes the speed back to normal after a specific amount of time and the player has to wait another amount of time, until he can run again?

I use the default CharacterMotor script and the following one i got from the internet:

 var walkSpeed: float = 7; // regular speed
 
 var crchSpeed: float = 3; // crouching speed
 
 var runSpeed: float = 24; // run speed
 
 
 
 private var chMotor: CharacterMotor;
 
 private var ch: CharacterController;
 
 private var tr: Transform;
 
 private var height: float; // initial height
 
 
 
 function Start(){
 
     chMotor = GetComponent(CharacterMotor);
 
     tr = transform;
 
     ch = GetComponent(CharacterController);
 
  height = ch.height;
 
 }
 
 
 
 function Update(){
 
 
 
     var h = height;
 
     var speed = walkSpeed;
 
     
 
     if (ch.isGrounded && Input.GetKey("left shift") || Input.GetKey("right shift")){
 
         speed = runSpeed;
         runmode();
 
     }
     
    
      
         
 
     if (Input.GetKey("c")){ // press C to crouch
 
         h = 0.5 * height;
 
         speed = crchSpeed; // slow down when crouching
 
     }
 
     chMotor.movement.maxForwardSpeed = speed; // set max speed
 
     var lastHeight = ch.height; // crouch/stand up smoothly 
 
     ch.height = Mathf.Lerp(ch.height, h, 5*Time.deltaTime);
 
     tr.position.y += (ch.height-lastHeight)/2; // fix vertical position
 
 }
 
 function runmode (runTime : float) {
 var timer = 2.0;
     while (timer < runTime) {
        speed = runSpeed;
        timer += Time.deltaTime;
        yield;
     }
 }

Please help me. :D

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 Fattie · Apr 02, 2013 at 02:51 PM 0
Share

for very simple timers in Unity, just use Invoke() or InvokeRepeating. You can replace all this with one or two lines of code. try unityGE$$anonymous$$S.com for articles on this. cheers

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by LyanApps · Apr 02, 2013 at 02:50 PM

You can mark the start of the run when the event key_down is called and compare it to the end of your running by using Time.time. See this tutorial for the phases of a button press: http://unity3d.com/learn/tutorials/modules/beginner/scripting/get-button-and-get-key

 float run_start;
 bool running = false;
 float seconds_can_run = 10.0f;
 
 function Update(){
 ...
 //Mark the time only when the button is initially pushed
 if(!running && Input.GetKeyDown("left shift")){ 
   run_start = Time.time;
   running = true;
 }
 
 //Cancel running if the key is let go or the allowed time has ellapsed
 if(running && (Input.GetKeyUp("left shift") || (run_start + seconds_can_run > Time.time)){
   running = false;
 }
 
 if (ch.isGrounded && running){
 ...
 }
Comment
Add comment · Show 3 · 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 Fotoschnitzel · Apr 02, 2013 at 03:28 PM 0
Share

Thanks this seems good now it looks like that:

 var walkSpeed: float = 7; // regular speed
 
 var crchSpeed: float = 3; // crouching speed
 
 var runSpeed: float = 24; // run speed
 
 
 
 private var ch$$anonymous$$otor: Character$$anonymous$$otor;
 
 private var ch: CharacterController;
 
 private var tr: Transform;
 
 private var height: float; // initial height
 
 float run_start;
 bool running = false;
 float seconds_can_run = 10.0f;
 
 
 
 function Start(){
 
     ch$$anonymous$$otor = GetComponent(Character$$anonymous$$otor);
 
     tr = transform;
 
     ch = GetComponent(CharacterController);
 
  height = ch.height;
 
 }
 
 
 
 function Update(){
 
 
 
     
 
     var speed = walkSpeed;
 
     if (!running && Input.Get$$anonymous$$ey("left shift")){
         
         run_start = Time.time;
         running = true;}
         
     if    (running && (Input.Get$$anonymous$$eyUp("left shift") || (run_start + seconds_can_run > Time.time)){
       running = false;}
       
       if (ch.isGrounded && running){
     speed = runSpeed;
     }        
 
 
     ch$$anonymous$$otor.movement.maxForwardSpeed = speed; // set max speed
 
     var lastHeight = ch.height; // crouch/stand up smoothly 
 
     ch.height = $$anonymous$$athf.Lerp(ch.height, h, 5*Time.deltaTime);
 
     tr.position.y += (ch.height-lastHeight)/2; // fix vertical position
 
 }

But it gives me some very strange errors...

Assets/Player $$anonymous$$ovement.js(21,6): UCE0001: ';' expected. Insert a semicolon at the end. Assets/Player $$anonymous$$ovement.js(22,5): UCE0001: ';' expected. Insert a semicolon at the end. Assets/Player $$anonymous$$ovement.js(23,6): UCE0001: ';' expected. Insert a semicolon at the end. Assets/Player $$anonymous$$ovement.js(54,104): BCE0044: expecting ), found '{'. Assets/Player $$anonymous$$ovement.js(70,1): BCE0044: expecting EOF, found '}'.

avatar image LyanApps · Apr 02, 2013 at 05:56 PM 0
Share

The variables I gave are in C# notation. try setting them to:

 var run_start : float;
 var running : bool = false;
 var seconds_can_run : float 10.0f;
avatar image Fattie · Apr 02, 2013 at 06:03 PM 0
Share

for a simple timer need like this it is very simple to just use Invoke(), cheers

avatar image
0

Answer by Atharv24 · Apr 02, 2013 at 02:52 PM

make a variable like tireness and add Time.deltaTime * tirenessModifier to it. And do an if statement for running like only run when tireness < 10. Hope that helps :)

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

13 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

Related Questions

Instantiate for amount of time 1 Answer

Problem Min and Max Time 1 Answer

fuel compsumation 0 Answers

Increase health smoothly not instantly? 2 Answers

putting the date into a variable 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