using UnityEngine; public abstract class Character : MonoBehaviour { [SerializeField] protected Animator _animator; [SerializeField] private float MovementSpeed = 1.5f; [SerializeField] protected GameObject _bonesSide; [SerializeField] protected GameObject _bonesBack; [SerializeField] private LayerMask _mapLayer; private Rigidbody2D _body; private CapsuleCollider2D _capsuleCollider; protected bool _isOnBridge; protected bool _isOnLadder = false; protected bool _isFalling; protected bool _facingRight = true; protected bool isAllowVertical = true; protected bool isAllowRight = true; protected bool isAllowLeft = true; private void Start() { _body = GetComponent(); _capsuleCollider = GetComponent(); } protected void MoveTo(float inputHorizontal, float inputVertical) { var mapElement = GetMapElement(Vector2.down); _isOnBridge = mapElement == MapElementType.Bridge && !_isFalling; if (mapElement == MapElementType.Wall) //|| _isOnLadder || _isOnBridge || mapElement == MapElementType.BreakableWall) { var leftCheck = GetMapElement(Vector2.left); var rightCheck = GetMapElement(Vector2.right); isAllowLeft = !(leftCheck == MapElementType.Wall || leftCheck == MapElementType.BreakableWall); isAllowRight = !(rightCheck == MapElementType.Wall || rightCheck == MapElementType.BreakableWall); isAllowVertical = false; _isFalling = false; float h_movement = inputHorizontal; if (h_movement > 0 && !_facingRight || h_movement < 0 && _facingRight) { FlipCharacter(); } SetWalkingAnimation(h_movement != 0); _isOnLadder =leftCheck == MapElementType.Ladder && rightCheck== MapElementType.Ladder; if (leftCheck == MapElementType.Ladder && rightCheck == MapElementType.Ladder) { float v_movement = inputVertical; print($"On ladder v_speed {v_movement}"); isAllowVertical = true; if (v_movement != 0) { //if (v_movement > 0 && mapElement != MapElementType.Ladder) //{ // v_movement = 0; //} SetClimbingAnimation(v_movement != 0); } _body.velocity = new Vector2(h_movement * MovementSpeed, v_movement * MovementSpeed); } else { print("On ground"); _body.velocity = new Vector2(h_movement * MovementSpeed, _body.velocity.y); } } else { _isFalling = true; } if (_isOnLadder || _isOnBridge) { _body.gravityScale = 0; } else { _body.gravityScale = 1; } if (_isFalling) { _body.velocity = new Vector2(0, _body.velocity.y); SetWalkingAnimation(false); if (mapElement == MapElementType.Ladder) { _body.velocity = Vector2.zero; _isOnLadder = true; } } } private MapElementType GetMapElement(Vector2 direction) { Vector2 rayStartPoint; if (direction==Vector2.down) rayStartPoint = new Vector2(_capsuleCollider.bounds.center.x + direction.x / 2, _capsuleCollider.bounds.min.y -0.1f); else rayStartPoint = new Vector2(_capsuleCollider.bounds.center.x + direction.x / 2, _capsuleCollider.bounds.min.y+0.1f ); var boundsSize = new Vector2(_capsuleCollider.bounds.size.x - 0.05f, 0.1f); var raycastHit = Physics2D.BoxCast(rayStartPoint, boundsSize, 0f, direction, 0.9f, _mapLayer); MapElementType returnValue; Color color = Color.red; if (raycastHit) { var mapElement = raycastHit.transform.GetComponent(); if (!mapElement.IsEnabled) { color = Color.white; returnValue = MapElementType.Empty; } else { color = Color.green; returnValue = mapElement.ElementSO.ElementType; } } else { returnValue = MapElementType.Empty; } Debug.DrawRay(rayStartPoint, direction, color); return returnValue; } protected abstract void SetWalkingAnimation(bool isWalking); protected abstract void SetClimbingAnimation(bool isClimbing); private void FlipCharacter() { Vector3 currentScale = gameObject.transform.localScale; currentScale.x *= -1; gameObject.transform.localScale = currentScale; _facingRight = !_facingRight; } }