我有一个 Unity 脚本,可以在玩家移动时播放音频(脚步声)。该脚本正在运行,但还有一个问题,在播放和暂停之间转换时,音频似乎太粗糙(并且可能会伤害我的扬声器呵呵)。
这是我的脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
public class PlayerController : MonoBehaviour {
Rigidbody playerRigid;
private Vector3 pindah;
public AudioSource audioSource;
public AudioClip audioClip;
public float lowPitchRange = .95f;
public float hiPitchRange = 1.05f;
void Awake () {
audioSource = GetComponent<AudioSource> ();
audioSource.clip = audioClip;
float randomPitch = Random.Range (lowPitchRange, hiPitchRange);
audioSource.pitch = randomPitch;
}
// Use this for initialization
void Start () {
//mengambil komponen rigidbody player
playerRigid = GetComponent<Rigidbody> ();
}
// Update is called once per frame
void Update () {
//untuk mendapatkan input dari virtual joystick button
float h = CrossPlatformInputManager.GetAxisRaw ("Horizontal");
float v = CrossPlatformInputManager.GetAxisRaw ("Vertical");
if ((h != 0f || v != 0f) && !audioSource.isPlaying) {
audioSource.Play ();
} else {
audioSource.Pause ();
}
//memindah posisi player sesuai input yang didapat
pindah = new Vector3 (h, 0.0f, v);
playerRigid.velocity = pindah;
}
}