Created
December 5, 2023 18:26
-
-
Save SamatKadyrov/2b7f335ae84aeea573cfd47aa7e2796c to your computer and use it in GitHub Desktop.
Sounds in Games
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class AmbientListener : MonoBehaviour | |
{ | |
// Start is called before the first frame update | |
void Start() | |
{ | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class AmbientSound : MonoBehaviour | |
{ | |
[SerializeField] private AudioSource _audioSource; | |
public void SetAmbient(AudioClip clip) | |
{ | |
_audioSource.clip = clip; | |
_audioSource.Play(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class AmbientSoundZone : MonoBehaviour | |
{ | |
[SerializeField] private AudioClip _clip; | |
[SerializeField] private AmbientSound _ambientSound; | |
private void OnTriggerEnter(Collider other) | |
{ | |
if (other.GetComponent<AmbientListener>() == false) | |
return; | |
_ambientSound.SetAmbient(_clip); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using UnityEngine; | |
public class FootStepsSounds : MonoBehaviour | |
{ | |
[SerializeField] private AudioSource _audioSource; | |
[SerializeField] private SurfaceStepsSound[] _stepsSounds; | |
[SerializeField] private Transform _checkPoint; | |
private SurfaceType _currentSurface; | |
private void FixedUpdate() | |
{ | |
if (Physics.Raycast(_checkPoint.position, Vector3.down, out RaycastHit hitInfo) == false) | |
return; | |
if (hitInfo.transform.TryGetComponent(out Surface surface) == false) | |
return; | |
if (surface.Type == _currentSurface) | |
return; | |
SetSurfaceSteps(surface.Type); | |
} | |
public void Play() | |
{ | |
_audioSource.Play(); | |
} | |
public void Pause() | |
{ | |
_audioSource.Pause(); | |
} | |
private void SetSurfaceSteps(SurfaceType type) | |
{ | |
_currentSurface = type; | |
_audioSource.clip = _stepsSounds.First(sound => sound.Type == _currentSurface).Clip; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class Movement : MonoBehaviour | |
{ | |
[SerializeField] private float _moveSpeed; | |
[SerializeField] private float _rotationSpeed; | |
[SerializeField] private AudioSource _stepsAudioSource; | |
[SerializeField] private float _stepDistance; | |
private float _coveredDistance = 0f; | |
private void Update() | |
{ | |
Rotate(); | |
Move(); | |
} | |
private void Rotate() | |
{ | |
float rotation = Input.GetAxis("Horizontal"); | |
transform.Rotate(_rotationSpeed * rotation * Time.deltaTime * Vector3.up); | |
} | |
private void Move() | |
{ | |
float direction = Input.GetAxis("Vertical"); | |
if(direction == 0f) | |
{ | |
_coveredDistance = 0f; | |
return; | |
} | |
float distance = _moveSpeed * direction * Time.deltaTime; | |
_coveredDistance += Mathf.Abs(distance); | |
transform.Translate(distance * Vector3.forward); | |
if(_coveredDistance >= _stepDistance) | |
{ | |
_coveredDistance -= _stepDistance; | |
_stepsAudioSource.Play(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class Surface : MonoBehaviour | |
{ | |
[SerializeField] private SurfaceType _type; | |
public SurfaceType Type => _type; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using UnityEngine; | |
[Serializable] | |
public struct SurfaceStepsSound | |
{ | |
[SerializeField] private SurfaceType _type; | |
[SerializeField] private AudioClip _clip; | |
public SurfaceType Type => _type; | |
public AudioClip Clip => _clip; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum SurfaceType | |
{ | |
Grass, | |
Snow | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class Weapon : MonoBehaviour | |
{ | |
[SerializeField] private AudioSource _audioSource; | |
private void Update() | |
{ | |
if (Input.GetMouseButtonDown(0)) | |
{ | |
Shoot(); | |
} | |
} | |
private void Shoot() | |
{ | |
Debug.Log("Shoot"); | |
_audioSource.Play(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment