- Home /
Sprinting script, timer?
hi everyone I am making a game and i want the player to be able to have a running timer to time sprinting and then after the timer has gone of to set the speed to crouch speed and regenerate the time will using crouch speed. thx for helping if you submit a anwser.
You use Invoke(), InvokeRepeating() and InvokeCancel() for very easy timers in Unity3D. you can find 1000s of examples on here.
Invoke( "changeToCrouchSpeed", 3.5 );
etc.
im sort of new to coding in unity so im not sure how to put that into my scripts that I have, here is my run/crouch script:
var walkSpeed: float = 3; // regular speed
var crchSpeed: float = 1.5; // crouching speed
var runSpeed: float = 5; // run speed
private var ch$$anonymous$$otor: Character$$anonymous$$otor;
private var tr: Transform;
private var dist: float; // distance to ground
function Start(){
ch$$anonymous$$otor = GetComponent(Character$$anonymous$$otor);
tr = transform;
var ch:CharacterController = GetComponent
(CharacterController);
dist = ch.height/2; // calculate distance to ground
}
function Update(){
var vScale = 1.0;
var speed = walkSpeed;
if (ch$$anonymous$$otor.isGrounded && Input.Get$$anonymous$$ey("right shift") ||
Input.Get$$anonymous$$ey("left shift")){
speed = runSpeed;
}
if (Input.Get$$anonymous$$ey("v")){ // press V to crouch
vScale = 0.5;
speed = crchSpeed; // slow down when crouching
}
ch$$anonymous$$otor.movement.maxForwardSpeed = speed; // set max speed
var ultScale = tr.localScale.y; // crouch/stand up smoothly
tr.localScale.y = $$anonymous$$athf.Lerp(tr.localScale.y, vScale,
5*Time.deltaTime);
tr.position.y += dist * (tr.localScale.y-ultScale); // fix
vertical position
}
the script didnt really work so if anyone else has any anwsers please submit.
You use Invoke(), InvokeRepeating() and InvokeCancel() for very easy timers in Unity3D. You can find 1000s of examples on this site.
Invoke( "changeToCrouchSpeed", 3.5 );
etc.