- Home /
Question by
DjScribblez28 · Mar 26, 2014 at 11:38 PM ·
runningtap
Keep tapping button to do certain action
I'm using the first person controller and i have a running script and everything but how do i make it so whenever i am tapping W i run, Like the game receiver. #pragma strict
var walkSpeed: float = 7; // regular speed
var crchSpeed: float = 3; // crouching speed
var runSpeed: float = 20; // run speed
private var chMotor: CharacterMotor;
private var ch: CharacterController;
private var tr: Transform;
private var height: float; // initial height
public var slideSpeed: float = 20; // slide speed
private var isSliding : boolean = false;
private var slideForward : Vector3; // direction of slide
private var slideTimer : float = 0.0;
public var slideTimerMax : float = 2.5; // time while sliding
function Start()
{
chMotor = GetComponent(CharacterMotor);
tr = transform;
ch = GetComponent(CharacterController);
height = ch.height;
}
function Update()
{
var h = height;
var speed = walkSpeed;
// - run and crouch -
if (ch.isGrounded && Input.GetKey("left shift") || Input.GetKey("right shift")) // press Shift to run
{
speed = runSpeed;
}
if (Input.GetKey("c")) // press C to crouch
{
h = 0.5 * height;
speed = crchSpeed; // slow down when crouching
}
// - slide -
if (Input.GetKeyDown("f") && !isSliding) // press F to slide
{
slideTimer = 0.0; // start timer
isSliding = true;
slideForward = tr.forward;
}
if (isSliding)
{
h = 0.5 * height; // height is crouch height
speed = slideSpeed; // speed is slide speed
chMotor.movement.velocity = slideForward * speed;
slideTimer += Time.deltaTime;
if (slideTimer > slideTimerMax)
{
isSliding = false;
}
}
// - apply movement modifiers -
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
}
That is the code i am using. Also if someone can give me a link that teaches Javascript/Unityscript and/or C# without all the i'm going to show you how to start up unity and end the video in 2 minutes, that would help me out alot
Comment