fixed player bugs
This commit is contained in:
@@ -6,17 +6,18 @@ using UnityEngine;
|
||||
public class Player : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private float runSpeed = 10f;
|
||||
[SerializeField] private float jumpSpeed = 10f;
|
||||
[SerializeField] private float jumpSpeed = 35f;
|
||||
[SerializeField] private float runFasterFactor = 1.5f;
|
||||
[SerializeField] private float groundErrorThreshold = 0.05f;
|
||||
[SerializeField] private float groundErrorThreshold = 1.5f;
|
||||
[SerializeField] private float wallErrorThreshold = 1.5f;
|
||||
[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 bool isMovementEnabled = true;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
@@ -28,13 +29,39 @@ public class Player : MonoBehaviour
|
||||
private void Update()
|
||||
{
|
||||
MovePlayer();
|
||||
|
||||
//if (IsPlayerOnWall(true))
|
||||
//{
|
||||
// if (collider.sharedMaterial == null || collider.sharedMaterial.friction != 0f)
|
||||
// {
|
||||
// collider.sharedMaterial = new PhysicsMaterial2D();
|
||||
// collider.sharedMaterial.friction = 0f;
|
||||
// collider.enabled = false;
|
||||
// collider.enabled = true;
|
||||
// }
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// if (collider.sharedMaterial == null || collider.sharedMaterial.friction == 0f)
|
||||
// {
|
||||
// collider.sharedMaterial = new PhysicsMaterial2D();
|
||||
// collider.sharedMaterial.friction = 0.5f;
|
||||
// collider.enabled = false;
|
||||
// collider.enabled = true;
|
||||
// }
|
||||
//}
|
||||
|
||||
}
|
||||
|
||||
private void MovePlayer()
|
||||
{
|
||||
Jump();
|
||||
Run();
|
||||
FlipSprite();
|
||||
if (isMovementEnabled)
|
||||
{
|
||||
Jump();
|
||||
Run();
|
||||
FlipSprite();
|
||||
}
|
||||
|
||||
HandleAnimations();
|
||||
}
|
||||
|
||||
@@ -51,9 +78,6 @@ public class Player : MonoBehaviour
|
||||
|
||||
private void Run()
|
||||
{
|
||||
if (IsPlayerOnWall())
|
||||
return;
|
||||
|
||||
var actualRunSpeed = runSpeed;
|
||||
|
||||
// Speed Run Increase
|
||||
@@ -61,6 +85,10 @@ public class Player : MonoBehaviour
|
||||
{
|
||||
actualRunSpeed *= runFasterFactor;
|
||||
}
|
||||
if (IsPlayerOnWall())
|
||||
{
|
||||
actualRunSpeed = 0;
|
||||
}
|
||||
|
||||
// Run
|
||||
if (Input.GetKey(KeyCode.RightArrow))
|
||||
@@ -75,38 +103,45 @@ public class Player : MonoBehaviour
|
||||
|
||||
private bool IsPlayerOnGround()
|
||||
{
|
||||
return IsPointOnGround(transform.position + new Vector3(0.3f, 0, 0)) ||
|
||||
IsPointOnGround(transform.position + new Vector3(-0.3f, 0, 0));
|
||||
return IsPointOnGround(transform.position + new Vector3(collider.bounds.extents.x * 0.97f, 0, 0)) ||
|
||||
IsPointOnGround(transform.position) ||
|
||||
IsPointOnGround(transform.position - new Vector3(collider.bounds.extents.x, 0, 0));
|
||||
}
|
||||
|
||||
private bool IsPointOnGround(Vector2 position)
|
||||
{
|
||||
Debug.DrawLine(position, position - new Vector2(0, collider.bounds.extents.y * groundErrorThreshold));
|
||||
|
||||
RaycastHit2D hit = Physics2D.Raycast(position,
|
||||
-Vector2.up,
|
||||
groundErrorThreshold,
|
||||
collider.bounds.extents.y * groundErrorThreshold,
|
||||
LayerMask.GetMask(LayerNames.Ground));
|
||||
|
||||
return hit.collider != null && hit.collider.IsTouching(collider);
|
||||
}
|
||||
|
||||
private bool IsPlayerOnWall()
|
||||
private bool IsPlayerOnWall(bool ignoreGround = false)
|
||||
{
|
||||
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));
|
||||
return IsPointOnWall(transform.position + new Vector3(0, collider.bounds.extents.y * 0.6f, 0), ignoreGround) ||
|
||||
IsPointOnWall(transform.position - new Vector3(0, collider.bounds.extents.y * 0.25f, 0), ignoreGround) ||
|
||||
IsPointOnWall(transform.position - new Vector3(0, collider.bounds.extents.y * 0.8f, 0), ignoreGround) ||
|
||||
IsPointOnWall(transform.position - new Vector3(0, collider.bounds.extents.y * 1.5f, 0), ignoreGround);
|
||||
}
|
||||
|
||||
private bool IsPointOnWall(Vector2 position)
|
||||
private bool IsPointOnWall(Vector2 position, bool ignoreGround = false)
|
||||
{
|
||||
Debug.DrawLine(position, position + new Vector2(collider.bounds.extents.x * wallErrorThreshold, 0), Color.green);
|
||||
Debug.DrawLine(position, position - new Vector2(collider.bounds.extents.x * wallErrorThreshold, 0), Color.green);
|
||||
|
||||
var rightHit = Physics2D.Linecast(position,
|
||||
position + wallLineCaseDistanceVector,
|
||||
position + new Vector2(collider.bounds.extents.x * wallErrorThreshold, 0),
|
||||
LayerMask.GetMask(LayerNames.Ground));
|
||||
var leftHit = Physics2D.Linecast(position,
|
||||
position - wallLineCaseDistanceVector,
|
||||
position - new Vector2(collider.bounds.extents.x * wallErrorThreshold, 0),
|
||||
LayerMask.GetMask(LayerNames.Ground));
|
||||
|
||||
return ((rightHit.collider != null && rightHit.collider.IsTouching(collider)) ||
|
||||
(leftHit.collider != null && leftHit.collider.IsTouching(collider))) && !IsPlayerOnGround();
|
||||
(leftHit.collider != null && leftHit.collider.IsTouching(collider))) && (!IsPlayerOnGround() || ignoreGround);
|
||||
}
|
||||
|
||||
private bool HasEncounteredEnemy()
|
||||
@@ -128,4 +163,14 @@ public class Player : MonoBehaviour
|
||||
animator.SetBool("IsGround", IsPlayerOnGround());
|
||||
animator.SetFloat("YVelocity", rigidBody.velocity.y);
|
||||
}
|
||||
|
||||
public void DisablePlayerMovement()
|
||||
{
|
||||
isMovementEnabled = false;
|
||||
}
|
||||
|
||||
public void EnablePlayerMovement()
|
||||
{
|
||||
isMovementEnabled = true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user