Crossy_Road_VR/Assets/Crossy Road VR/Scripts/CarControllerScript.cs

89 lines
1.8 KiB
C#

using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Скрипт для анимации машины
/// </summary>
public class CarControllerScript : MonoBehaviour
{
/// <summary>
/// to do
/// </summary>
[SerializeField]
private List<GameObject> _Wheels = new List<GameObject>();
/// <summary>
/// to do
/// </summary>
[SerializeField]
private int _Speed;
/// <summary>
/// высота,чтобы колёса стояли на 0
/// </summary>
[SerializeField]
private float _High;
[SerializeField]
private CarBipScript _PlayerDetect;
[SerializeField]
private AudioSource _CarBip;
HitDetectorScript _HitDetector;
public float High
{
get => _High;
set => _High = value;
}
private bool _IsBeep = false;
private void Start()
{
_High = gameObject.transform.position.y;
_PlayerDetect.PlayerEntered.AddListener(StartBeep);
_PlayerDetect.PlayerExited.AddListener(StopBeep);
}
private void StartBeep(HitDetectorScript hit)
{
_HitDetector = hit;
_HitDetector.Dead.AddListener(StopBeep);
if (!_IsBeep && !_HitDetector.IsDead)
{
_CarBip.Play();
_IsBeep = true;
}
}
private void StopBeep()
{
if (_IsBeep)
{
_CarBip.Stop();
_IsBeep = false;
}
}
private void StopBeep(HitDetectorScript hit)
{
_HitDetector = hit;
if (_IsBeep)
{
_CarBip.Stop();
_IsBeep= false;
}
}
private void FixedUpdate()
{
foreach (var wheel in _Wheels)
{
wheel.transform.Rotate(Vector3.right * _Speed);
}
}
}