- Home /
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
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
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){
...
}
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 '}'.
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;
for a simple timer need like this it is very simple to just use Invoke(), cheers
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 :)
Your answer
Follow this Question
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