first commit
This commit is contained in:
54
Assets/Scripts/RaycastDoor.cs
Normal file
54
Assets/Scripts/RaycastDoor.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class RaycastDoor : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Camera _camera;
|
||||
[SerializeField] private CursorSwitcher _cursorSwitcher;
|
||||
[SerializeField] private float _speed = 3f;
|
||||
[SerializeField] private float _openAngle = 81.087f;
|
||||
[SerializeField] private float _closedAngle = 1.882f;
|
||||
|
||||
private bool _isOpen = false;
|
||||
private Transform _doorPivot;
|
||||
private Quaternion _targetRotation;
|
||||
private bool _hasTarget = false;
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (Input.GetMouseButtonDown(0))
|
||||
{
|
||||
Ray ray = _camera.ScreenPointToRay(Input.mousePosition);
|
||||
RaycastHit[] hits = Physics.RaycastAll(ray, _cursorSwitcher.InteractDistance);
|
||||
foreach (var hit in hits)
|
||||
{
|
||||
Transform current = hit.collider.transform;
|
||||
while (current != null)
|
||||
{
|
||||
if (current.CompareTag("Door"))
|
||||
{
|
||||
_doorPivot = current.parent;
|
||||
_isOpen = !_isOpen;
|
||||
float angle = _isOpen ? _openAngle : _closedAngle;
|
||||
_targetRotation = Quaternion.Euler(0f, angle, 0f);
|
||||
_hasTarget = true;
|
||||
return;
|
||||
}
|
||||
current = current.parent;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (_hasTarget && _doorPivot != null)
|
||||
{
|
||||
_doorPivot.localRotation = Quaternion.Lerp(_doorPivot.localRotation, _targetRotation, Time.deltaTime * _speed);
|
||||
|
||||
if (Quaternion.Angle(_doorPivot.localRotation, _targetRotation) < 0.2f)
|
||||
{
|
||||
_doorPivot.localRotation = _targetRotation;
|
||||
_hasTarget = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user