Skip to content

Instantly share code, notes, and snippets.

@jakevsrobots
Last active December 29, 2015 09:19
Show Gist options
  • Save jakevsrobots/7649182 to your computer and use it in GitHub Desktop.
Save jakevsrobots/7649182 to your computer and use it in GitHub Desktop.
Footsteps with randomness. 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 lastIndex = 0;
void Update()
{
if(controller.velocity.sqrMagnitude > 10 && controller.isGrounded)
{
stepTimer = stepTimer + Time.deltaTime;
}
else
{
stepTimer = 0;
}
if(stepTimer > 0.35f)
{
int index = Random.Range(0, footsteps.Length-1);
while(index == lastIndex)
{
index = Random.Range(0, footsteps.Length-1);
}
audio.PlayOneShot(footsteps[index]);
stepTimer = 0;
lastIndex = index;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment