colliders

This commit is contained in:
2023-06-29 23:24:22 +03:00
parent f69a74c941
commit d7d0f04865
8 changed files with 667 additions and 848 deletions
+42 -28
View File
@@ -12,13 +12,13 @@ public abstract class Character : MonoBehaviour
protected GameObject _bonesBack;
[SerializeField]
private LayerMask _mapLayer;
private LayerMask _mapLayer;
private Rigidbody2D _body;
private BoxCollider2D _boxCollider;
//private BoxCollider2D _boxCollider;
private CapsuleCollider2D _boxCollider;
protected bool _isOnBridge;
protected bool _isOnLadder;
protected bool _isFalling;
@@ -26,35 +26,39 @@ public abstract class Character : MonoBehaviour
protected bool isAllowVertical = true;
protected bool isAllowRight=true;
protected bool isAllowLeft= true;
protected bool isAllowRight = true;
protected bool isAllowLeft = true;
private void Start()
{
_body = GetComponent<Rigidbody2D>();
_boxCollider = GetComponent<BoxCollider2D>();
_boxCollider = GetComponent<CapsuleCollider2D>();
}
protected void MoveTo(float inputHorizontal,float inputVertical)
protected void MoveTo(float inputHorizontal, float inputVertical)
{
var mapElement = GetMapElement(Vector2.down);
_isOnBridge = mapElement == MapElementType.Bridge && !_isFalling;
float v_movement=0;
if (mapElement==MapElementType.Wall || _isOnLadder || _isOnBridge|| mapElement == MapElementType.BreakableWall)
float v_movement = 0;
if (mapElement == MapElementType.Wall || _isOnLadder || _isOnBridge || mapElement == MapElementType.BreakableWall)
{
var leftCheck = GetMapElement(Vector2.left);
var rightCheck = GetMapElement(Vector2.right);
if(leftCheck == MapElementType.Wall) {
isAllowLeft = false;
}else
if (leftCheck == MapElementType.Wall)
{
isAllowLeft = false;
}
else
{
isAllowLeft = true;
}
if (rightCheck ==MapElementType.Wall) {
if (rightCheck == MapElementType.Wall)
{
isAllowRight = false;
}else
}
else
{
isAllowRight = true;
}
@@ -73,16 +77,18 @@ public abstract class Character : MonoBehaviour
SetWalkingAnimation(h_movement != 0);
_isOnLadder = mapElement == MapElementType.Ladder || GetMapElement(Vector2.up) == MapElementType.Ladder;
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 (GetMapElement(Vector2.up) != MapElementType.Ladder)
if (upperElement != MapElementType.Ladder)
{
v_movement = 0;
}
@@ -99,16 +105,24 @@ public abstract class Character : MonoBehaviour
private MapElementType GetMapElement(Vector2 direction)
{
var raycastHit=Physics2D.BoxCast(_boxCollider.bounds.center, _boxCollider.bounds.size, 0f, direction, 0.1f, _mapLayer);
var raycastHit = Physics2D.BoxCast(_boxCollider.bounds.center, _boxCollider.bounds.size, 0f, direction, 0.1f, _mapLayer);
if (raycastHit)
{
var mapElement = raycastHit.transform.GetComponent<MapElement>();
return mapElement == null ? MapElementType.Empty : mapElement.ElementSO.ElementType;
if (mapElement == null)
{
return MapElementType.Empty;
}
if (mapElement.ElementSO.ElementType == MapElementType.BreakableWall)
{
if (mapElement.GetComponent<BoxCollider2D>().isTrigger)
{
return MapElementType.Empty;
}
}
return mapElement.ElementSO.ElementType;
}
else
return MapElementType.Empty;
return MapElementType.Empty;
}
protected abstract void SetWalkingAnimation(bool isWalking);
@@ -116,7 +130,7 @@ public abstract class Character : MonoBehaviour
private void FixedUpdate()
{
if (_isOnLadder|| _isOnBridge)
if (_isOnLadder || _isOnBridge)
{
_body.gravityScale = 0;
}
@@ -129,7 +143,7 @@ public abstract class Character : MonoBehaviour
_body.velocity = new Vector2(0, _body.velocity.y);
SetWalkingAnimation(false);
if (GetMapElement(Vector2.down)==MapElementType.Ladder)
if (GetMapElement(Vector2.down) == MapElementType.Ladder)
{
_body.velocity = Vector2.zero;
_isOnLadder = true;
@@ -146,5 +160,5 @@ public abstract class Character : MonoBehaviour
_facingRight = !_facingRight;
}
}