79 lines
2.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;
public class HitDetectorScript : MonoBehaviour
{
private bool _IsDead = false;
[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;
private void OnTriggerEnter(Collider other)
{
//Debug.Log(other.gameObject.name);
if (other.gameObject.CompareTag("Car") && !_IsDead)
{
_IsDead = true;
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");
// }
//}
}