Last active
December 29, 2015 09:19
-
-
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.
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 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