using UnityEngine;
namespace Movement
{
public class Climb : AdditionalMovement
{
[SerializeField, Header("Animation states at which you can jump")]
private WorkingWithAnimation.CharacterAnimation[] _conditionToPlayAnimation;
[SerializeField, Header("Jump settings:")]
private float _climbSpeedByUp;
[SerializeField]
private float _climbSpeedByForward;
[SerializeField]
private float _waitBeforClimb;
[SerializeField]
private float _climbIterations;
public ClimbCheacker ClimbCheacker;
[SerializeField]
private string _animationTrigger;
public override System.Collections.IEnumerator Move()
{
if (ClimbCheacker.CanClimb())
{
if (_playerAnimation.SetTrigger(_animationTrigger, _conditionToPlayAnimation))
{
float normalGravity = _moveLogic.Gravity;
float normalHeight = _controller.height;
_animationCondition.Condition = WorkingWithAnimation.CharacterAnimation.Climb;
_moveLogic.Gravity = 0;
_controller.height = 1;
yield return new WaitForSeconds(_waitBeforClimb);
for (int i = 0; i < _climbIterations; i++)
{
yield return new WaitForSeconds(0.1f);
_moveLogic.MoveDirection = (transform.up * _climbSpeedByUp) + (transform.forward * _climbSpeedByForward);
_controller.height += (normalHeight - 1) / _climbIterations;
}
yield return new WaitForSeconds(0.5f);
_moveLogic.Gravity = normalGravity;
yield return new WaitForSeconds(0.5f);
_controller.height = normalHeight;
}
}
}
}
}
using UnityEngine;
public class ClimbCheacker : MonoBehaviour
{
[SerializeField]
private bool _obstacleForClimbing;
[SerializeField]
private GameObject _objectToClimb;
private RaycastHit _hit;
private void OnTriggerEnter(Collider other)
{
_obstacleForClimbing = true;
}
private void OnTriggerExit(Collider other)
{
_obstacleForClimbing = false;
}
private void Update()
{
if (Physics.Raycast(transform.position, Vector3.down, out _hit, 3))
_objectToClimb = _hit.transform.gameObject;
else
_objectToClimb = null;
}
public bool CanClimb()
{
return !_obstacleForClimbing && _objectToClimb != null;
}
public GameObject ObjectToClimb()
{
return _objectToClimb;
}
}