fixed issues with player
This commit is contained in:
@@ -9,12 +9,15 @@ public class Player : MonoBehaviour
|
||||
[SerializeField] private float jumpSpeed = 10f;
|
||||
[SerializeField] private float runFasterFactor = 1.5f;
|
||||
[SerializeField] private float groundErrorThreshold = 0.05f;
|
||||
[SerializeField] private float wallLineCaseDistance = 0.5f;
|
||||
[SerializeField] private float runErrorThreshold = 0.05f;
|
||||
|
||||
private Rigidbody2D rigidBody;
|
||||
new private BoxCollider2D collider;
|
||||
private Animator animator;
|
||||
|
||||
private Vector2 wallLineCaseDistanceVector => new Vector2(wallLineCaseDistance, 0);
|
||||
|
||||
private void Start()
|
||||
{
|
||||
rigidBody = GetComponent<Rigidbody2D>();
|
||||
@@ -32,7 +35,6 @@ public class Player : MonoBehaviour
|
||||
Jump();
|
||||
Run();
|
||||
FlipSprite();
|
||||
//MarkAsRunning();
|
||||
HandleAnimations();
|
||||
}
|
||||
|
||||
@@ -49,6 +51,9 @@ public class Player : MonoBehaviour
|
||||
|
||||
private void Run()
|
||||
{
|
||||
if (IsPlayerOnWall())
|
||||
return;
|
||||
|
||||
var actualRunSpeed = runSpeed;
|
||||
|
||||
// Speed Run Increase
|
||||
@@ -70,14 +75,40 @@ public class Player : MonoBehaviour
|
||||
|
||||
private bool IsPlayerOnGround()
|
||||
{
|
||||
RaycastHit2D hit = Physics2D.Raycast(transform.position,
|
||||
-Vector2.up,
|
||||
1f + groundErrorThreshold,
|
||||
return IsPointOnGround(transform.position + new Vector3(0.3f, 0, 0)) ||
|
||||
IsPointOnGround(transform.position + new Vector3(-0.3f, 0, 0));
|
||||
}
|
||||
|
||||
private bool IsPointOnGround(Vector2 position)
|
||||
{
|
||||
RaycastHit2D hit = Physics2D.Raycast(position,
|
||||
-Vector2.up,
|
||||
groundErrorThreshold,
|
||||
LayerMask.GetMask(LayerNames.Ground));
|
||||
|
||||
return hit.collider != null && hit.collider.IsTouching(collider);
|
||||
}
|
||||
|
||||
private bool IsPlayerOnWall()
|
||||
{
|
||||
return IsPointOnWall(transform.position + new Vector3(0, 1, 0)) ||
|
||||
IsPointOnWall(transform.position + new Vector3(0, -1, 0)) ||
|
||||
IsPointOnWall(transform.position + new Vector3(0, -2, 0));
|
||||
}
|
||||
|
||||
private bool IsPointOnWall(Vector2 position)
|
||||
{
|
||||
var rightHit = Physics2D.Linecast(position,
|
||||
position + wallLineCaseDistanceVector,
|
||||
LayerMask.GetMask(LayerNames.Ground));
|
||||
var leftHit = Physics2D.Linecast(position,
|
||||
position - wallLineCaseDistanceVector,
|
||||
LayerMask.GetMask(LayerNames.Ground));
|
||||
|
||||
return ((rightHit.collider != null && rightHit.collider.IsTouching(collider)) ||
|
||||
(leftHit.collider != null && leftHit.collider.IsTouching(collider))) && !IsPlayerOnGround();
|
||||
}
|
||||
|
||||
private bool HasEncounteredEnemy()
|
||||
{
|
||||
return rigidBody.IsTouchingLayers(LayerMask.GetMask(LayerNames.Enemies));
|
||||
@@ -91,18 +122,6 @@ public class Player : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
private void MarkAsRunning()
|
||||
{
|
||||
if (Mathf.Abs(rigidBody.velocity.x) > Mathf.Epsilon)
|
||||
{
|
||||
animator.SetBool("IsRunning", true);
|
||||
}
|
||||
else
|
||||
{
|
||||
animator.SetBool("IsRunning", false);
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleAnimations()
|
||||
{
|
||||
animator.SetBool("IsRunning", Mathf.Abs(rigidBody.velocity.x) > runErrorThreshold);
|
||||
|
||||
Reference in New Issue
Block a user