Skip to content

Instantly share code, notes, and snippets.

@jakevsrobots
Created November 25, 2013 21:25
Show Gist options
  • Save jakevsrobots/7649166 to your computer and use it in GitHub Desktop.
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.
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