pokazmi jak mam zrobic by ten skrypt dzialal using System.Collections; using…
- User
pokazmi jak mam zrobic by ten skrypt dzialal using System.Collections; using UnityEngine;
public class Footsteps : MonoBehaviour { public AudioSource footstepsSource; public AudioSource sprintSource; public AudioClip[] footstepClips; public AudioClip[] sprintClips;
private int currentFootstepIndex = 0; private int currentSprintIndex = 0;
void Update() { if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D)) { if (Input.GetKey(KeyCode.LeftShift)) { PlaySprintSound(); StopFootstepSound(); } else { PlayFootstepSound(); StopSprintSound(); } } else { StopFootstepSound(); StopSprintSound(); } }
private void PlayFootstepSound() { if (!footstepsSource.isPlaying) { footstepsSource.clip = footstepClips[currentFootstepIndex]; footstepsSource.enabled = true; footstepsSource.Play(); StartCoroutine(WaitForFootstepClipToEnd()); } }
private void PlaySprintSound() { if (!sprintSource.isPlaying) { sprintSource.clip = sprintClips[currentSprintIndex]; sprintSource.enabled = true; sprintSource.Play(); StartCoroutine(WaitForSprintClipToEnd()); } }
private void StopFootstepSound() { if (footstepsSource.isPlaying) { StartCoroutine(DisableAudioSourceAfterPlayback(footstepsSource)); } }
private void StopSprintSound() { if (sprintSource.isPlaying) { StartCoroutine(DisableAudioSourceAfterPlayback(sprintSource)); } }
private IEnumerator WaitForFootstepClipToEnd() { while (footstepsSource.isPlaying) { yield return null; }
currentFootstepIndex = (currentFootstepIndex + 1) % footstepClips.Length; }
private IEnumerator WaitForSprintClipToEnd() { while (sprintSource.isPlaying) { yield return null; }
currentSprintIndex = (currentSprintIndex + 1) % sprintClips.Length; }
private IEnumerator DisableAudioSourceAfterPlayback(AudioSource audioSource) { // Czekaj aż klip przestanie być odtwarzany while (audioSource.isPlaying) { yield return null; }
// Wyłącz AudioSource audioSource.enabled = false; } }
- Assistant
Twój skrypt wygląda dobrze, ale by go uruchomić prawidłowo w Unity, musisz upewnić się, że komponenty są poprawnie skonfigurowane i odpowiednie AudioClipy są przypisane do skryptu. Oto kilka kroków, które możesz podjąć, aby upewnić się, że wszystko działa poprawnie:
- User
dodaj do tego skyptu by gdy ciagle jest trzymany przycisk czyli ciagle jest chodzenie lub bieganie niech zmienia ciagle klip po kazdym kolejnym (input W itp) nie zmieniaj reszty
- Assistant