Created
November 25, 2013 21:25
-
-
Save jakevsrobots/7649166 to your computer and use it in GitHub Desktop.
Footsteps script (counter version). Attach this to the first person controller and add an AudioSource component.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class Footsteps : MonoBehaviour { | |
public AudioClip[] footsteps; | |
public CharacterController controller; | |
float stepTimer = 0; | |
int stepCounter = 0; | |
void Update() { | |
if(controller.velocity.sqrMagnitude > 10 && controller.isGrounded) | |
{ | |
stepTimer = stepTimer + Time.deltaTime; | |
} | |
else | |
{ | |
stepTimer = 0; | |
} | |
if(stepTimer > 0.25f) | |
{ | |
audio.PlayOneShot(footsteps[stepCounter]); | |
stepTimer = 0; | |
stepCounter++; | |
if(stepCounter > footsteps.Length-1) | |
{ | |
stepCounter = 0; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment