public abstract int Health { get; }
public int Health2;
public class Solider : UnitBase
{
[SerializeField] private int _health; // Нужно для настройки солдата в Unity
public override int Health => _health;
}
using System.Collections;
using UnityEngine;
using UnityEngine.Events;
public class Door : MonoBehaviour
{
[SerializeField] private Vector3 _rotationOffset;
[SerializeField] private Vector3 _positionOffset;
[SerializeField] private float _openingTime = 2f;
[Space]
[SerializeField] private AudioSource _openSound;
[SerializeField] private AudioSource _closeSound;
[SerializeField] private Transform _root;
[Space]
[SerializeField] private bool _justOneDiscovery;
[SerializeField] private bool _firstOpen;
[SerializeField] private bool _open;
[Space]
[SerializeField] private UnityEvent _actionEvents;
private Transform _movingPart;
private Vector3 _newRotation;
private Vector3 _newPosition;
private bool _working;
private void Awake()
{
if (_root != false)
{
_movingPart = _root;
}
else
{
_movingPart = transform;
}
}
private void Update()
{
if(_working)
{
_movingPart.position = Vector3.Lerp(_root.position, _newPosition, _openingTime * Time.deltaTime);
_movingPart.eulerAngles = Vector3.Lerp(_root.eulerAngles, _newRotation, _openingTime * Time.deltaTime);
}
}
private void OnMouseDown()
{
if(!_working)
{
if (_justOneDiscovery && _firstOpen || !_justOneDiscovery)
{
if (_open)
{
Close();
}
else
{
Open();
}
StartCoroutine("StopWorking");
}
}
}
private void Open()
{
_newRotation = _movingPart.eulerAngles + _rotationOffset;
_newPosition = _movingPart.position + _positionOffset;
if(_openSound != null)
{
_openSound.Play();
}
if(_actionEvents != null)
{
_actionEvents?.Invoke();
}
_firstOpen = true;
_working = true;
}
private void Close()
{
_newRotation = _movingPart.eulerAngles - _rotationOffset;
_newPosition = _movingPart.position - _positionOffset;
if (_openSound != null)
{
_closeSound.Play();
}
_firstOpen = true;
_working = true;
}
private IEnumerator StopWorking()
{
yield return new WaitForSeconds(_openingTime);
_working = false;
_open = !_open;
_newRotation = Vector3.zero;
_newPosition = Vector3.zero;
}
}
private void Update()
{
if(_working)
{
_movingPart.rotation = Quaternion.Lerp(_currentRotation, _newRotation, 2);
_movingPart.position = Vector3.Lerp(_currentPosition, _newPosition, 2);
}
if(_movingPart.rotation == _newRotation && _movingPart.position == _newPosition)
{
_working = false;
_open = !_open;
_newRotation = Quaternion.Euler(Vector3.zero);
_newPosition = Vector3.zero;
_currentRotation = Quaternion.Euler(Vector3.zero);
_currentPosition = Vector3.zero;
}
_currentRotation = _movingPart.rotation;
_currentPosition = _movingPart.position;
}
_newRotation = _movingPart.eulerAngles + _rotationOffset;
_newPosition = _movingPart.position + _positionOffset;
private void Update()
{
if(_working)
{
_movingPart.eulerAngles = Vector3.Lerp(_movingPart.eulerAngles, _newRotation, 2);
_movingPart.position = Vector3.Lerp(_movingPart.position, _newPosition, 2);
}
if(_movingPart.eulerAngles == _newRotation && _movingPart.position == _newPosition)
{
print("End");
_working = false;
_open = !_open;
_newRotation = Vector3.zero;
_newPosition = Vector3.zero;
}
}