2014年8月5日 星期二

Unity WalkSound

in the unity , if you want some sound when you walk, you can use this script (C#):
Create Sound.cs Script File, and copy code:

using UnityEngine;
using System.Collections;

public class Sound : MonoBehaviour {
 private AudioSource audios;
 public AudioClip AudioFile;
 // Use this for initialization
 void Awake(){
  audios = gameObject.AddComponent("AudioSource")as AudioSource;
  audios.clip = AudioFile;
  audios.playOnAwake = false;
  audios.loop = true;
 }
 void Start () {
  
 }
 
 // Update is called once per frame
 void Update () {
  if(Input.GetKeyDown(KeyCode.W)){
   audio.clip = AudioFile;
   audio.Play ();
   
  }
  if(Input.GetKeyUp (KeyCode.W)){
   audio.clip = AudioFile;
   audio.Stop();
  }
 }
}
and put your sound in AudioFile, Like this picture:

Download Sound File: 

Walk Sound:

Reference: 

ps:
in short, we should RTFM <- (wiki)

沒有留言:

張貼留言

© Mac Taylor, 歡迎自由轉貼。
Background Email Pattern by Toby Elliott
Since 2014