Refactor player, create base character class
This commit is contained in:
@@ -0,0 +1,112 @@
|
|||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
public class Character : MonoBehaviour
|
||||||
|
{
|
||||||
|
[SerializeField]
|
||||||
|
private Animator animator;
|
||||||
|
[SerializeField]
|
||||||
|
private float MovementSpeed = 1.5f;
|
||||||
|
[SerializeField]
|
||||||
|
private LayerMask ladderLayer;
|
||||||
|
[SerializeField]
|
||||||
|
private LayerMask groundLayer;
|
||||||
|
[SerializeField]
|
||||||
|
private float distance;
|
||||||
|
private bool _isOnLadder;
|
||||||
|
private Rigidbody2D _body;
|
||||||
|
private BoxCollider2D _boxCollider;
|
||||||
|
private bool _isFall;
|
||||||
|
private bool _facingRight = true;
|
||||||
|
|
||||||
|
private void Start()
|
||||||
|
{
|
||||||
|
_body = GetComponent<Rigidbody2D>();
|
||||||
|
_boxCollider = GetComponent<BoxCollider2D>();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void Update()
|
||||||
|
{
|
||||||
|
float inputVertical = 0;
|
||||||
|
|
||||||
|
var groundCheck = Physics2D.BoxCast(_boxCollider.bounds.center, _boxCollider.bounds.size, 0f, Vector2.down, .1f, groundLayer);
|
||||||
|
|
||||||
|
if (groundCheck || _isOnLadder)
|
||||||
|
{
|
||||||
|
_isFall = false;
|
||||||
|
float inputHorizontal = Input.GetAxisRaw("Horizontal");
|
||||||
|
if (inputHorizontal > 0 && !_facingRight)
|
||||||
|
{
|
||||||
|
FlipCharacter();
|
||||||
|
}
|
||||||
|
if (inputHorizontal < 0 && _facingRight)
|
||||||
|
{
|
||||||
|
FlipCharacter();
|
||||||
|
}
|
||||||
|
|
||||||
|
animator.SetBool("Walk", inputHorizontal != 0);
|
||||||
|
|
||||||
|
if (IsLadder(Vector2.down))
|
||||||
|
{
|
||||||
|
_isOnLadder = true;
|
||||||
|
inputVertical = Input.GetAxisRaw("Vertical");
|
||||||
|
|
||||||
|
if (inputVertical > 0)
|
||||||
|
{
|
||||||
|
if (!IsLadder(Vector2.up))
|
||||||
|
{
|
||||||
|
inputVertical = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_isOnLadder = false;
|
||||||
|
}
|
||||||
|
_body.velocity = new Vector2(inputHorizontal * MovementSpeed, inputVertical * MovementSpeed);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_isFall = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private RaycastHit2D IsLadder(Vector2 direction)
|
||||||
|
{
|
||||||
|
return Physics2D.BoxCast(_boxCollider.bounds.center, _boxCollider.bounds.size, 0f, direction, .1f, ladderLayer);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void FixedUpdate()
|
||||||
|
{
|
||||||
|
if (_isOnLadder)
|
||||||
|
{
|
||||||
|
|
||||||
|
_body.gravityScale = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_body.gravityScale = 1;
|
||||||
|
}
|
||||||
|
if (_isFall)
|
||||||
|
{
|
||||||
|
if (IsLadder(Vector2.down))
|
||||||
|
{
|
||||||
|
_isOnLadder = true;
|
||||||
|
}
|
||||||
|
_body.velocity = new Vector2(0, _body.velocity.y);
|
||||||
|
animator.SetBool("Walk", false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void FlipCharacter()
|
||||||
|
{
|
||||||
|
Vector3 currentScale = gameObject.transform.localScale;
|
||||||
|
currentScale.x *= -1;
|
||||||
|
gameObject.transform.localScale = currentScale;
|
||||||
|
|
||||||
|
_facingRight = !_facingRight;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 02b165448a794b74992cf202965d79e4
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
+2
-108
@@ -1,32 +1,12 @@
|
|||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
public class Player : MonoBehaviour
|
public class Player : Character
|
||||||
{
|
{
|
||||||
[SerializeField]
|
|
||||||
private Animator animator;
|
|
||||||
[SerializeField]
|
|
||||||
private float MovementSpeed = 1.5f;
|
|
||||||
[SerializeField]
|
|
||||||
private LayerMask ladderLayer;
|
|
||||||
[SerializeField]
|
|
||||||
private LayerMask groundLayer;
|
|
||||||
[SerializeField]
|
|
||||||
private float distance;
|
|
||||||
private bool _isOnLadder;
|
|
||||||
private Rigidbody2D _body;
|
|
||||||
private BoxCollider2D _boxCollider;
|
|
||||||
private bool _isFall;
|
|
||||||
private bool _facingRight = true;
|
|
||||||
|
|
||||||
private int _totalCoins = 0;
|
private int _totalCoins = 0;
|
||||||
private bool _hasKey = false;
|
private bool _hasKey = false;
|
||||||
|
|
||||||
private void Start()
|
|
||||||
{
|
|
||||||
_body = GetComponent<Rigidbody2D>();
|
|
||||||
_boxCollider = GetComponent<BoxCollider2D>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void AddCoin()
|
public void AddCoin()
|
||||||
{
|
{
|
||||||
_totalCoins++;
|
_totalCoins++;
|
||||||
@@ -48,90 +28,4 @@ public class Player : MonoBehaviour
|
|||||||
{
|
{
|
||||||
return _hasKey;
|
return _hasKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void Update()
|
|
||||||
{
|
|
||||||
float inputVertical = 0;
|
|
||||||
|
|
||||||
var groundCheck = Physics2D.BoxCast(_boxCollider.bounds.center, _boxCollider.bounds.size, 0f, Vector2.down, .1f, groundLayer);
|
|
||||||
|
|
||||||
if (groundCheck || _isOnLadder)
|
|
||||||
{
|
|
||||||
_isFall = false;
|
|
||||||
float inputHorizontal = Input.GetAxisRaw("Horizontal");
|
|
||||||
if (inputHorizontal > 0 && !_facingRight)
|
|
||||||
{
|
|
||||||
FlipCharacter();
|
|
||||||
}
|
|
||||||
if (inputHorizontal < 0 && _facingRight)
|
|
||||||
{
|
|
||||||
FlipCharacter();
|
|
||||||
}
|
|
||||||
|
|
||||||
animator.SetBool("PlayerWalk", inputHorizontal != 0);
|
|
||||||
|
|
||||||
if (IsLadder(Vector2.down))
|
|
||||||
{
|
|
||||||
_isOnLadder = true;
|
|
||||||
inputVertical = Input.GetAxisRaw("Vertical");
|
|
||||||
|
|
||||||
if (inputVertical > 0)
|
|
||||||
{
|
|
||||||
if (!IsLadder(Vector2.up))
|
|
||||||
{
|
|
||||||
inputVertical = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_isOnLadder = false;
|
|
||||||
}
|
|
||||||
_body.velocity = new Vector2(inputHorizontal * MovementSpeed, inputVertical * MovementSpeed);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_isFall = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private RaycastHit2D IsLadder(Vector2 direction)
|
|
||||||
{
|
|
||||||
return Physics2D.BoxCast(_boxCollider.bounds.center, _boxCollider.bounds.size, 0f, direction, .1f, ladderLayer);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private void FixedUpdate()
|
|
||||||
{
|
|
||||||
if (_isOnLadder)
|
|
||||||
{
|
|
||||||
|
|
||||||
_body.gravityScale = 0;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_body.gravityScale = 1;
|
|
||||||
}
|
|
||||||
if (_isFall)
|
|
||||||
{
|
|
||||||
if (IsLadder(Vector2.down))
|
|
||||||
{
|
|
||||||
_isOnLadder = true;
|
|
||||||
}
|
|
||||||
_body.velocity = new Vector2(0, _body.velocity.y);
|
|
||||||
animator.SetBool("PlayerWalk", false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void FlipCharacter()
|
|
||||||
{
|
|
||||||
Vector3 currentScale = gameObject.transform.localScale;
|
|
||||||
currentScale.x *= -1;
|
|
||||||
gameObject.transform.localScale = currentScale;
|
|
||||||
|
|
||||||
_facingRight = !_facingRight;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user