/
arti4
/
CourseWork
Обзор
Документация
Войти
/
arti4
/
CourseWork
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Scriptes/CharacterMovement.cs
159 строк
5 KB
arti4
Загрузить файлы в «»
01 июн 2025, 11:56
01 июн 2025, 11:56
6a254d9
Код
Авторство
О чём код?
using UnityEngine; using System.Collections; using UnityEngine.Audio; public class CharacterMovement : MonoBehaviour { [SerializeField] private float _speed; [SerializeField] private float _jumpForce; [SerializeField] private float _slowdownFactor = 2f; private Vector2 _input; private bool _Moving; private bool _Grounded; private Rigidbody2D _rigidbody; private CharacterAnimation _animation; private float _originalSpeed; private bool _isSlowedDown = false; private bool _wasGrounded = true; [SerializeField] private AudioSource _audioSourceWalk; [SerializeField] private AudioClip _walkSound; [SerializeField] private AudioSource _audioSourceJump; [SerializeField] private AudioClip _jumpSound; [SerializeField] private float _basePitch = 0.7f; private bool _isPlayingWalkSound = false; private bool _canJump = true; private float currentVolume; private AudioManager _audioManager; private void Start() { _rigidbody = GetComponent<Rigidbody2D>(); _animation = GetComponentInChildren<CharacterAnimation>(); _originalSpeed = _speed; _audioSourceWalk.pitch = _basePitch; _audioManager = Object.FindFirstObjectByType<AudioManager>(); if (_audioManager != null) { currentVolume = PlayerPrefs.GetFloat("Game", 1f); _audioManager.SetVolume(currentVolume); } } private void Update() { Move(); CheckGround(); if (Input.GetKeyDown(KeyCode.Space) && _Grounded && !_isSlowedDown && _canJump) { Jump(); } if (!_wasGrounded && _Grounded) { _audioSourceJump.PlayOneShot(_jumpSound); } _wasGrounded = _Grounded; UpdateAnimationStates(); } //Ходьба private void Move() { float currentSpeed = _isSlowedDown ? _originalSpeed / _slowdownFactor : _originalSpeed; _input = new Vector2(Input.GetAxis("Horizontal"), 0); _Moving = _input.x != 0; _rigidbody.linearVelocity = new Vector2(_input.x * currentSpeed, _rigidbody.linearVelocity.y); if (_Moving && _Grounded) { if (!_isPlayingWalkSound) { _audioSourceWalk.clip = _walkSound; _audioSourceWalk.loop = true; _audioSourceWalk.Play(); _isPlayingWalkSound = true; } } else { _audioSourceWalk.Stop(); _isPlayingWalkSound = false; } } //Прыжок private void Jump() { _rigidbody.AddForce(Vector2.up * _jumpForce, ForceMode2D.Impulse); _canJump = false; StartCoroutine(JumpCooldown()); } //Кулдаун для прыжка private IEnumerator JumpCooldown() { yield return new WaitForSeconds(1f); _canJump = true; } //Проверка поверхности под персонажем private void CheckGround() { float rayLength = 0.5f; Collider2D collider = GetComponent<Collider2D>(); Vector3 rayStartPosition = collider.bounds.center; rayStartPosition.y = collider.bounds.min.y; RaycastHit2D hit = Physics2D.Raycast(rayStartPosition, Vector2.down, rayLength, LayerMask.GetMask("Ground", "Box")); // Дополнительная проверка с OverlapCircle Collider2D overlapHit = Physics2D.OverlapCircle(rayStartPosition, 0.2f, LayerMask.GetMask("Ground", "Box")); _Grounded = (hit.collider != null && (hit.collider.CompareTag("Ground") || hit.collider.CompareTag("Box"))) || (overlapHit != null && (overlapHit.CompareTag("Ground") || overlapHit.CompareTag("Box"))); } private void UpdateAnimationStates() { _animation.Moving = _Moving; _animation.Flying = !_Grounded; } //Игрок касается колючей проволоки private void OnTriggerEnter2D(Collider2D other) { if (other.CompareTag("Barbwire")) { _isSlowedDown = true; _audioSourceWalk.pitch = _basePitch / _slowdownFactor; } } //Игрок перестал касаться колючей проволоки private void OnTriggerExit2D(Collider2D other) { if (other.CompareTag("Barbwire")) { _isSlowedDown = false; _audioSourceWalk.pitch = _basePitch; } } //Обновления громкости и сохранения её public void UpdateVolume(float volume) { currentVolume = volume; if (_audioManager != null) { _audioManager.SetVolume(currentVolume); PlayerPrefs.SetFloat("Game", currentVolume); PlayerPrefs.Save(); } } }