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 BoxCollider2D _boxCollider; private CapsuleCollider2D _boxCollider; protected bool _isOnBridge; protected bool _isOnLadder; protected bool _isFalling; protected bool _facingRight = true; protected bool isAllowVertical = true; protected bool isAllowRight = true; protected bool isAllowLeft = true; float v_movement = 0; private void Start() { _body = GetComponent(); _boxCollider = 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); if (leftCheck == MapElementType.Wall || leftCheck == MapElementType.BreakableWall) { isAllowLeft = false; } else { isAllowLeft = true; } if (rightCheck == MapElementType.Wall || rightCheck== MapElementType.BreakableWall) { isAllowRight = false; } else { isAllowRight = true; } isAllowVertical = false; _isFalling = false; float h_movement = inputHorizontal; if (h_movement > 0 && !_facingRight) { FlipCharacter(); } if (h_movement < 0 && _facingRight) { FlipCharacter(); } SetWalkingAnimation(h_movement != 0); var upperElement = GetMapElement(Vector2.up); _isOnLadder = mapElement == MapElementType.Ladder || upperElement == MapElementType.Ladder; if (_isOnLadder) { isAllowVertical = true; _isOnLadder = true; v_movement = inputVertical; if (v_movement > 0) { if (upperElement != MapElementType.Ladder) { v_movement = 0; } } SetClimbingAnimation(v_movement != 0); } _body.velocity = new Vector2(h_movement * MovementSpeed, v_movement * MovementSpeed); } else { _isFalling = true; } } private MapElementType GetMapElement(Vector2 direction) { var raycastHit = Physics2D.BoxCast(_boxCollider.bounds.center, _boxCollider.bounds.size, 0f, direction, 0.2f, _mapLayer); if (raycastHit) { var mapElement = raycastHit.transform.GetComponent(); if (mapElement == null) { return MapElementType.Empty; } return mapElement.ElementSO.ElementType; } return MapElementType.Empty; } protected abstract void SetWalkingAnimation(bool isWalking); protected abstract void SetClimbingAnimation(bool isClimbing); private void FixedUpdate() { if (_isOnLadder || _isOnBridge) { _body.gravityScale = 0; } else { _body.gravityScale = 1; } if (_isFalling) { _body.velocity = new Vector2(0, _body.velocity.y); SetWalkingAnimation(false); if (GetMapElement(Vector2.down) == MapElementType.Ladder) { _body.velocity = Vector2.zero; _isOnLadder = true; } } } private void FlipCharacter() { Vector3 currentScale = gameObject.transform.localScale; currentScale.x *= -1; gameObject.transform.localScale = currentScale; _facingRight = !_facingRight; } }