我试图写一个脚本,其中音频源播放一个audioclip数组与4个声音在它。当我的角色冲刺时,声音会播放得更快更响,而当我的角色蹲下时,声音会播放得更安静更慢。我的问题是audiosource不播放audioclip。我检查了好几遍,还是找不到问题。
` ` ` ` ` ` player脚步声:MonoBehaviour{private AudioSource footstepSound;
[SerializeField]
private AudioClip[] footstepClip;
private CharacterController charController;
[HideInInspector]
public float VolumeMin, VolumeMax;
private float accumulatedDistance;
[HideInInspector]
public float stepDistance;
void Awake()
{
footstepSound = GetComponent<AudioSource>();
charController = GetComponentInParent<CharacterController>();
}
void Update()
{
CheckToPlayFootstepSounds();
}
void CheckToPlayFootstepSounds()
{
if (!charController.isGrounded)
{
return;
}
if (charController.velocity.magnitude > 0)
{
accumulatedDistance += Time.deltaTime;
if (accumulatedDistance > stepDistance)
{
footstepSound.volume = Random.Range(VolumeMin, VolumeMax);
footstepSound.clip = footstepClip[Random.Range(0, footstepClip.Length)];
footstepSound.Play();
accumulatedDistance = 0f;
}
else
{
accumulatedDistance = 0f;
}
}
}
} '
public class PlayerSprintAndCrouch : MonoBehaviour
{
private PlayerMovement playerMovement;
public float sprintSpeed = 10f, moveSpeed = 5f, crouchSpeed = 2f;
[SerializeField]
private Transform lookRoot;
private float standHeight = 1.6f, crouchHeight = 1f;
private bool isCrouching;
private PlayerFootsteps playerFootsteps;
private float sprintVolume = 1f;
private float crouchVolume = 0.1f;
private float walkVolumeMin = 0.2f, walkVolumeMax = 0.6f;
private float walkStepDistance = 0.4f, sprintStepDistance = 0.25f, crouchStepDistance = 0.5f;
void Awake()
{
playerMovement = GetComponent<PlayerMovement>();
playerFootsteps = GetComponentInChildren<PlayerFootsteps>();
}
void Start()
{
playerFootsteps.VolumeMin = walkVolumeMin;
playerFootsteps.VolumeMax = walkVolumeMax;
playerFootsteps.stepDistance = walkStepDistance;
}
// Update is called once per frame
void Update()
{
Sprint();
Crouch();
}
void Sprint()
{
if (Input.GetKeyDown(KeyCode.LeftShift) && !isCrouching)
{
playerMovement.speed = sprintSpeed;
playerFootsteps.stepDistance = sprintStepDistance;
playerFootsteps.VolumeMin = sprintVolume;
playerFootsteps.VolumeMax = sprintVolume;
}
if (Input.GetKeyUp(KeyCode.LeftShift) && !isCrouching)
{
playerMovement.speed = moveSpeed;
playerFootsteps.stepDistance = walkStepDistance;
playerFootsteps.VolumeMin = walkVolumeMin;
playerFootsteps.VolumeMax = walkVolumeMax;
}
}
void Crouch()
{
if (Input.GetKeyDown(KeyCode.LeftControl))
{
if (isCrouching)
{
lookRoot.localPosition = new Vector3(0f, standHeight, 0f);
playerMovement.speed = moveSpeed;
isCrouching = false;
}
else
{
lookRoot.localPosition = new Vector3(0f, crouchHeight, 0f);
playerMovement.speed = crouchSpeed;
playerFootsteps.stepDistance = crouchStepDistance;
playerFootsteps.VolumeMin = crouchVolume;
playerFootsteps.VolumeMax = crouchVolume;
isCrouching = true;
}
}
}
}
问题是您将accumulatedDistance设置为0,这应该可以解决您的问题:
void CheckToPlayFootstepSounds()
{
if (!charController.isGrounded)
{
return;
}
if (charController.velocity.magnitude > 0)
{
accumulatedDistance += Time.deltaTime;
if (accumulatedDistance > stepDistance)
{
footstepSound.volume = Random.Range(VolumeMin, VolumeMax);
footstepSound.clip = footstepClip[Random.Range(0, footstepClip.Length)];
footstepSound.Play();
accumulatedDistance = 0f;
}
else
{
// accumulatedDistance = 0f; Remove This
}
}
}