added bridges
This commit is contained in:
+36
-30
@@ -7,15 +7,15 @@ public class Character : MonoBehaviour
|
||||
[SerializeField]
|
||||
private float MovementSpeed = 1.5f;
|
||||
[SerializeField]
|
||||
private LayerMask ladderLayer;
|
||||
[SerializeField]
|
||||
private LayerMask groundLayer;
|
||||
[SerializeField]
|
||||
private float distance;
|
||||
private bool _isOnLadder;
|
||||
private LayerMask _mapLayer;
|
||||
|
||||
|
||||
private Rigidbody2D _body;
|
||||
private BoxCollider2D _boxCollider;
|
||||
private bool _isFall;
|
||||
|
||||
private bool _isOnBridge;
|
||||
private bool _isOnLadder;
|
||||
private bool _isFalling;
|
||||
private bool _facingRight = true;
|
||||
|
||||
|
||||
@@ -31,20 +31,22 @@ public class Character : MonoBehaviour
|
||||
|
||||
protected void MoveTo(float inputHorizontal,float inputVertical)
|
||||
{
|
||||
var groundCheck = CheckBounds(Vector2.down, groundLayer);
|
||||
|
||||
float v_movement=0;
|
||||
if (groundCheck || _isOnLadder)
|
||||
var mapElement = GetMapElement(Vector2.down);
|
||||
|
||||
_isOnBridge = mapElement == MapElementType.Bridge && !_isFalling;
|
||||
|
||||
float v_movement=0;
|
||||
if (mapElement==MapElementType.Wall || _isOnLadder || _isOnBridge)
|
||||
{
|
||||
var leftCheck = CheckBounds(Vector2.left, groundLayer);
|
||||
var rightCheck = CheckBounds(Vector2.right, groundLayer);
|
||||
if(leftCheck.collider!=null) {
|
||||
var leftCheck = GetMapElement(Vector2.left);
|
||||
var rightCheck = GetMapElement(Vector2.right);
|
||||
if(leftCheck == MapElementType.Wall) {
|
||||
isAllowLeft = false;
|
||||
}else
|
||||
{
|
||||
isAllowLeft = true;
|
||||
}
|
||||
if (rightCheck.collider != null) {
|
||||
if (rightCheck ==MapElementType.Wall) {
|
||||
isAllowRight = false;
|
||||
}else
|
||||
{
|
||||
@@ -52,7 +54,7 @@ public class Character : MonoBehaviour
|
||||
}
|
||||
|
||||
isAllowVertical = false;
|
||||
_isFall = false;
|
||||
_isFalling = false;
|
||||
float h_movement = inputHorizontal;
|
||||
if (h_movement > 0 && !_facingRight)
|
||||
{
|
||||
@@ -65,52 +67,56 @@ public class Character : MonoBehaviour
|
||||
|
||||
animator.SetBool("Walk", h_movement != 0);
|
||||
|
||||
if (CheckBounds(Vector2.down, ladderLayer))
|
||||
_isOnLadder = mapElement == MapElementType.Ladder || GetMapElement(Vector2.up) == MapElementType.Ladder;
|
||||
if (_isOnLadder)
|
||||
{
|
||||
isAllowVertical = true;
|
||||
_isOnLadder = true;
|
||||
v_movement=inputVertical;
|
||||
v_movement = inputVertical;
|
||||
|
||||
if (v_movement > 0)
|
||||
{
|
||||
if (!CheckBounds(Vector2.up,ladderLayer))
|
||||
if (GetMapElement(Vector2.up) != MapElementType.Ladder)
|
||||
{
|
||||
v_movement = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_isOnLadder = false;
|
||||
}
|
||||
_body.velocity = new Vector2(h_movement * MovementSpeed, v_movement * MovementSpeed);
|
||||
}
|
||||
else
|
||||
{
|
||||
_isFall = true;
|
||||
_isFalling = true;
|
||||
}
|
||||
}
|
||||
|
||||
private RaycastHit2D CheckBounds(Vector2 direction,LayerMask layer)
|
||||
private MapElementType GetMapElement(Vector2 direction)
|
||||
{
|
||||
return Physics2D.BoxCast(_boxCollider.bounds.center, _boxCollider.bounds.size, 0f, direction, .1f, layer);
|
||||
var raycastHit=Physics2D.BoxCast(_boxCollider.bounds.center, _boxCollider.bounds.size, 0f, direction, .1f, _mapLayer);
|
||||
if (raycastHit)
|
||||
{
|
||||
var mapElement = raycastHit.transform.GetComponent<IMapElement>();
|
||||
|
||||
return mapElement == null ? MapElementType.Empty : mapElement.ElementType;
|
||||
}
|
||||
else
|
||||
return MapElementType.Empty;
|
||||
}
|
||||
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
if (_isOnLadder)
|
||||
if (_isOnLadder|| _isOnBridge)
|
||||
{
|
||||
|
||||
_body.gravityScale = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
_body.gravityScale = 1;
|
||||
}
|
||||
if (_isFall)
|
||||
if (_isFalling)
|
||||
{
|
||||
if (CheckBounds(Vector2.down,ladderLayer))
|
||||
if (GetMapElement(Vector2.down)==MapElementType.Ladder)
|
||||
{
|
||||
_isOnLadder = true;
|
||||
}
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
public enum TreasureType { Coin, Key }
|
||||
public enum MapElementType {Empty,Wall,Ladder,Bridge }
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f7c871dd492d04149b39713e47b77dc6
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,6 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class Bridge : MonoBehaviour, IMapElement
|
||||
{
|
||||
public MapElementType ElementType => MapElementType.Bridge;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7710dbd5e4ddd064198582aed7ee50c0
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,4 @@
|
||||
public interface IMapElement
|
||||
{
|
||||
MapElementType ElementType { get; }
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: edf40d39eadb17a4ebb0368b3ac5399a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,6 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class Ladder : MonoBehaviour, IMapElement
|
||||
{
|
||||
public MapElementType ElementType => MapElementType.Ladder;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8e88df5680ec8e64ea4939c37720b363
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,6 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class Wall : MonoBehaviour, IMapElement
|
||||
{
|
||||
public MapElementType ElementType => MapElementType.Wall;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3f939712c17722545b2724151dac3d9d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user