• 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 unityGEMS.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 chMotor: CharacterMotor;
 
 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(){
 
     chMotor = GetComponent(CharacterMotor);
 
     tr = transform;
 
     ch = GetComponent(CharacterController);
 
  height = ch.height;
 
 }
 
 
 
 function Update(){
 
 
 
     
 
     var speed = walkSpeed;
 
     if (!running && Input.GetKey("left shift")){
         
         run_start = Time.time;
         running = true;}
         
     if    (running && (Input.GetKeyUp("left shift") || (run_start + seconds_can_run > Time.time)){
       running = false;}
       
       if (ch.isGrounded && running){
     speed = runSpeed;
     }        
 
 
     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
 
 }

But it gives me some very strange errors...

Assets/Player Movement.js(21,6): UCE0001: ';' expected. Insert a semicolon at the end. Assets/Player Movement.js(22,5): UCE0001: ';' expected. Insert a semicolon at the end. Assets/Player Movement.js(23,6): UCE0001: ';' expected. Insert a semicolon at the end. Assets/Player Movement.js(54,104): BCE0044: expecting ), found '{'. Assets/Player Movement.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.

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

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

How to create an agenda or timetable system? 1 Answer

Animation Window: How to scale keyframes to increase time? 9 Answers

Minute Timer Issue 1 Answer

Unity and music tracks - load track at certain point and fading tracks in and out 1 Answer

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