95 lines
2.8 KiB
C#
95 lines
2.8 KiB
C#
using UnityEngine;
|
||
using UnityEngine.Events;
|
||
using UnityEngine.XR.Interaction.Toolkit;
|
||
|
||
public class HitDetectorScript : MonoBehaviour
|
||
{
|
||
private bool _IsDead = false;
|
||
public bool IsDead
|
||
{
|
||
get { return _IsDead; }
|
||
}
|
||
|
||
[SerializeField] private GameObject _Menu;
|
||
[SerializeField] private GameObject _ForwardSource;
|
||
[SerializeField] private ActionBasedContinuousMoveProvider move;
|
||
[SerializeField] private CharacterController _CharacterController;
|
||
|
||
[Range(0f, 1f)]
|
||
[SerializeField] private float _Intensity;
|
||
[SerializeField] private float _Duration;
|
||
|
||
[SerializeField] XRBaseController _LeftController;
|
||
[SerializeField] XRBaseController _RightController;
|
||
|
||
[HideInInspector]
|
||
public UnityEvent Dead = new UnityEvent();
|
||
|
||
[SerializeField] private ScoreManagerScript _ScoreManager;
|
||
|
||
private void OnTriggerEnter(Collider other)
|
||
{
|
||
//Debug.Log(other.gameObject.name);
|
||
if (other.gameObject.CompareTag("Car") && !_IsDead)
|
||
{
|
||
if (PlayerPrefs.GetInt("Best score") < _ScoreManager.CurrentScore)
|
||
{
|
||
PlayerPrefs.SetInt("Best score", _ScoreManager.CurrentScore);
|
||
}
|
||
_IsDead = true;
|
||
|
||
Dead.Invoke();
|
||
|
||
move.enabled = false;
|
||
_CharacterController.enabled = false;
|
||
|
||
UISpawn();
|
||
|
||
TriggerHaptic(_LeftController);
|
||
TriggerHaptic(_RightController);
|
||
|
||
Debug.Log("Player was hitted car");
|
||
//SceneManager.LoadSceneAsync(SceneManager.GetActiveScene().name);
|
||
}
|
||
}
|
||
|
||
private void UISpawn()
|
||
{
|
||
Vector3 pos = _ForwardSource.transform.position;
|
||
Vector3 forward = _ForwardSource.transform.forward;
|
||
|
||
// Определите расстояние перед объектом
|
||
float distance = 1.0f;
|
||
|
||
// Вычислите новую позицию с учетом расстояния
|
||
Vector3 newPos = pos + forward.normalized * distance;
|
||
|
||
// Меню смотрит в том же направлении, что и источник, но фиксируется по вертикали
|
||
Quaternion newRot = Quaternion.LookRotation(new Vector3(forward.x, 0, forward.z));
|
||
|
||
// Создание объекта
|
||
Instantiate(_Menu, newPos, newRot);
|
||
}
|
||
|
||
public void TriggerHaptic(XRBaseController controller)
|
||
{
|
||
if (_Intensity > 0)
|
||
{
|
||
controller.SendHapticImpulse(_Intensity, _Duration);
|
||
}
|
||
}
|
||
//private void OnCollisionEnter(Collision collision)
|
||
//{
|
||
// Debug.Log(collision.gameObject.name);
|
||
//}
|
||
|
||
//private void OnControllerColliderHit(ControllerColliderHit hit)
|
||
//{
|
||
// Debug.Log(hit.gameObject.name);
|
||
// if (hit.gameObject.CompareTag("Car"))
|
||
// {
|
||
// Debug.Log("Car hitted");
|
||
// }
|
||
//}
|
||
}
|