dimanche 17 mai 2015

Play random sound clip on collision (Unity)

I have 4 sound files that are landing noises i want the player to make when colliding with the ground, at the moment no sound is playing. New to C# and programming need a bit of help please.

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(AudioSource))]
public class Bhop : MonoBehaviour{

public AudioClip[] list;
AudioSource audio;
CharacterController characterController;
int number;

void Start () 
{
    audio = GetComponent<AudioSource>();
    characterController = GetComponent<CharacterController>();
    //Loading the items into the array
    list =  new AudioClip[]
    {
        (AudioClip)Resources.Load("Sound/jumplanding1.wav"),
        (AudioClip)Resources.Load("Sound/jumplanding2.wav"), 
        (AudioClip)Resources.Load("Sound/jumplanding3.wav"), 
        (AudioClip)Resources.Load("Sound/jumplanding4.wav")
    };
}

void OnCollisionEnter (Collision col)
{
    int number;
    number = Random.Range(0, list.Length);


    if (col.gameObject.name == "Sound") 
    {
        audio.PlayOneShot(list[number], 0.5f);
    }
}
}




Aucun commentaire:

Enregistrer un commentaire