Sprint Script[FOR FPS GAMES]

Hello, I’m trying to make a BASIC FPS game - Shooting some zombies and stuff. :slight_smile:

I tried to look around the net, and found some SPRINTING SCRIPT wich could be useful for a escape when a zombie is near you.

I found this code, and added, but how can I make a ‘button’ command to sprint? Like, if I want to have ‘SHIFT’ as the key to ‘SPRINTING’

Thanks very much if you can help. :slight_smile:

LONG CODE[EXIST WALKING ETC]

private var speed = 6.0;
var aimSpeed = 2.0;
var sprintSpeed = 10.0;
private var canSprint : boolean = true;

var sprintJumpSpeed = 8.0;
var normSpeed = 6.0;
var crouchSpeed = 6.0;
var crouchDeltaHeight = 1.0;
var jumpSpeed = 8.0;
var normJumpSpeed = 8.0;
var gravity = 20.0;
private var mainCamera : GameObject;
private var weaponCamera : GameObject;
static var crouching : boolean = false;
private var stopCrouching : boolean = false;
private var standardCamHeight : float;
private var crouchingCamHeight : float;

private var moveDirection = Vector3.zero;
private var grounded : boolean = false;

static var walking : boolean = false;

function Start() {
speed = normSpeed;
mainCamera = gameObject.FindWithTag(“MainCamera”);
weaponCamera = gameObject.FindWithTag(“WeaponCamera”);
crouching = false;
standardCamHeight = mainCamera.transform.localPosition.y;
crouchingCamHeight = standardCamHeight - crouchDeltaHeight;
}

function Update() {
if(mainCamera.transform.localPosition.y > standardCamHeight){
mainCamera.transform.localPosition.y = standardCamHeight;
} else if(mainCamera.transform.localPosition.y < crouchingCamHeight){
mainCamera.transform.localPosition.y = crouchingCamHeight;
}
if(weaponCamera.transform.localPosition.y > standardCamHeight){
weaponCamera.transform.localPosition.y = standardCamHeight;
} else if(weaponCamera.transform.localPosition.y < crouchingCamHeight){
weaponCamera.transform.localPosition.y = crouchingCamHeight;
}

if (Input.GetButtonDown (“Crouch”)){
if(crouching){
stopCrouching = true;
normalSpeed();
return;
}

if(!crouching)
crouch();
}
if(crouching){
if(mainCamera.transform.localPosition.y > crouchingCamHeight){
if(mainCamera.transform.localPosition.y - (crouchDeltaHeightTime.deltaTime8) < crouchingCamHeight){
mainCamera.transform.localPosition.y = crouchingCamHeight;
} else {
mainCamera.transform.localPosition.y -= crouchDeltaHeightTime.deltaTime8;
}
}
if(weaponCamera.transform.localPosition.y > crouchingCamHeight){
if(weaponCamera.transform.localPosition.y - (crouchDeltaHeightTime.deltaTime8) < crouchingCamHeight){
weaponCamera.transform.localPosition.y = crouchingCamHeight;
} else {
weaponCamera.transform.localPosition.y -= crouchDeltaHeightTime.deltaTime8;
}
}
} else {
if(mainCamera.transform.localPosition.y < standardCamHeight){
if(mainCamera.transform.localPosition.y + (crouchDeltaHeightTime.deltaTime8) > standardCamHeight){
mainCamera.transform.localPosition.y = standardCamHeight;
} else {
mainCamera.transform.localPosition.y += crouchDeltaHeightTime.deltaTime8;
}
}
if(weaponCamera.transform.localPosition.y < standardCamHeight){
if(weaponCamera.transform.localPosition.y - (crouchDeltaHeightTime.deltaTime8) > standardCamHeight){
weaponCamera.transform.localPosition.y = standardCamHeight;
} else {
weaponCamera.transform.localPosition.y += standardCamHeightTime.deltaTime8;
}
}

}

}
function FixedUpdate() {

if (grounded) {
// We are grounded, so recalculate movedirection directly from axes
moveDirection = new Vector3(Input.GetAxis(“Horizontal”), 0, Input.GetAxis(“Vertical”));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;

if (Input.GetButton (“Jump”)) {
moveDirection.y = jumpSpeed;
if(crouching){
stopCrouching = true;
normalSpeed();
}
}

}

// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;

// Move the controller
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags CollisionFlags.CollidedBelow) != 0;

if((Mathf.Abs(moveDirection.x) > 0) grounded || (Mathf.Abs(moveDirection.z) > 0 grounded)){
if(!walking){
walking = true;
BroadcastMessage(“walking”, SendMessageOptions.DontRequireReceiver);
}
} else if(walking){
walking = false;
BroadcastMessage(“stopWalking”, SendMessageOptions.DontRequireReceiver);
}
}

@script RequireComponent(CharacterController)

function aiming() {
speed = aimSpeed;
}

function crouch() {
speed = crouchSpeed;
//mainCamera.transform.position.y -= crouchDeltaHeight;
//weaponCamera.transform.position.y -= crouchDeltaHeight;
//BroadcastMessage (“crouching”, SendMessageOptions.DontRequireReceiver);
this.GetComponent(CharacterController).height -= crouchDeltaHeight;
this.GetComponent(CharacterController).center -= Vector3(0,crouchDeltaHeight/2, 0);
crouching = true;
}

function normalSpeed () {
if(stopCrouching){
crouching = false;
//mainCamera.transform.position.y += crouchDeltaHeight;
//weaponCamera.transform.position.y += crouchDeltaHeight;
this.GetComponent(“CharacterController”).height += crouchDeltaHeight;
this.GetComponent(“CharacterController”).center += Vector3(0,crouchDeltaHeight/2, 0);
BroadcastMessage(“stopWalking”, SendMessageOptions.DontRequireReceiver);

stopCrouching = false;
} else if(crouching){
speed = crouchSpeed;
return;
}
speed = normSpeed;
jumpSpeed = normJumpSpeed;
}
function sprinting () {
if(crouching){
crouching = false;
//mainCamera.transform.position.y += crouchDeltaHeight;
//weaponCamera.transform.position.y += crouchDeltaHeight;
this.GetComponent(CharacterController).height += crouchDeltaHeight;
this.GetComponent(CharacterController).center += Vector3(0,crouchDeltaHeight/2, 0);

}
if(canSprint){
speed = sprintSpeed;
jumpSpeed = sprintJumpSpeed;
}
}

It appears its already doing that; with that script it looks like you only sprint when:

if(Input.GetButtonDown ("Crouch")){ ...

You just need to add “Crouch” to the Input Manager and assign it a key.

I forgot to tell, I’m kinda a begginer to Unity - I cant find the Input Manager. :confused:

“To see the Input Manager choose: Edit->Project Settings->Input.”

I seem to only have these Catogories:

Horizontal
Vertical
Fire1
Fire2
Fire3
Jump
Mouse X
Mouse Y
Mouse ScrollWheel
Window Shake X
Window Shake Y
Horizontal
Vertical
Fire1
Fire2
Fire3
Jump

The “Size” field is the amount of inputs… I think the default is 17. If you change that to 18 it will add a new input that you can configure.

Hm…

Im kinda lost after that part… sorry for being irratating

Can anyone help?? :confused:

I think you need to look for a tutorial on how to work with inputs, is not that difficult actually, is right there in the edit->project settings menu The name bit is the name of the input the other fields refer to where is the input taken from.

Also for sprinting basically all that you would have to do is to modify in which you move when a certain key is being pressed (basically the same thing that crouching is already doing)

Just a comment though, if you are pretty new to unity probably a complete fps is not the best project to go for, try something simpler first like a puzzle game or a shooting gallery (as in shooting moving objects in the screen). Then move up from there.