User:Igor

From Unify Community Wiki
Jump to: navigation, search

I found this website very useful and wanted to contribute with a script.

Maybe you can update it for me as that might waive the need for account registration.

The script is for the FootstepHandler (c#) and I've added the ability to have different sounds for different surfaces.

Here is the code, if you could add it or if not, I'll take care of it once I finish registration. Thanks in advance. -igor


code -----------------------------------


using UnityEngine; using System.Collections; using System.Collections.Generic;

[RequireComponent(typeof(AudioSource))] [RequireComponent(typeof(CharacterController))]

[System.Serializable] public class FootstepSounds : System.Object { public enum GroundSurface{ Clay, Dirt, Forest, Glass, GlassBroken, Grass, Gravel, Metal, Mud, Sand, Snow, Stone, StoneInterior, Water, WetPavment, Wood, };

public GroundSurface surfaceType; public AudioClip[] leftFootSounds; public AudioClip[] rightFootSounds; }

public class FootstepHandler : MonoBehaviour { public float minInterval = 0.1f; public float maxVelocity = 8.0f; public float bias = 1.1f; public float volume = 0.6f;

//public List<Nested> nestedList; public FootstepSounds[] footstepSounds;

private CharacterController _controller; private bool firstStep = true;

void Awake() { _controller = GetComponent<CharacterController>(); }

// Use this for initialization IEnumerator Start () { while(true) { float vel = _controller.velocity.magnitude; if(_controller.isGrounded && vel > 0.2f) {

int numberOfSounds = footstepSounds.GetLength(0);

// (gets first sound of left foot of the first footstepsounds object) if(numberOfSounds < 1){ Debug.LogWarning("Footstep Handler script failed to run because no sounds were set."); return false; }

Debug.Log ("NuMBER of Sounds::::: " + footstepSounds.GetLength(0));


// set current sound set FootstepSounds currentFootstepSound = footstepSounds[1];

// sound variations for this surface int availableFootstepSoundVariations = currentFootstepSound.leftFootSounds.Length;

// check for null sound variations and break out of here. if (availableFootstepSoundVariations < 1){ Debug.LogWarning("No sound assigned for this footstepSounds object. Failed to generate footstep sound."); return false; }

// set default sound audio.clip = currentFootstepSound.leftFootSounds[0];


// check if we have any sounds before continuing if (availableFootstepSoundVariations > 0) { // select random set (so we are not always hearing the same sound) int rnd = Random.Range(0, availableFootstepSoundVariations);

// switch between two sounds (left foot, right foot sounds) if (firstStep) { // set sound for first foot audio.clip = currentFootstepSound.leftFootSounds[rnd]; firstStep = false; } else { // set sound for second foot audio.clip = currentFootstepSound.rightFootSounds[rnd]; firstStep = true; } }

audio.volume = volume; audio.Play(); float interval = minInterval * (maxVelocity + bias) / (vel + bias); yield return new WaitForSeconds(interval); } else { yield return 0; } }


} }

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox