My Player Going trough the wall when I dashing
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class MovementPlayer : MonoBehaviour
{
//Component
Rigidbody2D rb;
Animator anim;
//Float
public float WalkForce;
public float JumpForce;
public float WallSlidForce;
public float WallJumpDuration = 0.3f;
public float DashPower = 30;
private float HorizontalMovement;
private float WallSlideDirection;
private float WallJumpTimer = 0.2f;
private float WallJumpTime;
private float DashTime = 0.2f;
private float DashCooldown = 1f;
//Int
private int NumberOfJumps = 1;
private int JumpLeft;
//Bool
private bool IsMoving;
private bool IsGrounded;
private bool IsFacingRight = true;
private bool IsTouchingWall;
private bool IsSliding;
private bool IsWallJumping;
private bool CanMove;
private bool CanDash = true;
private bool IsDashing;
//Vector
public Vector2 GroundCheckSize;
public Vector2 WallCheckSize;
public Vector2 WallJumpForce = new Vector2 (2f,10f);
//Transform
public Transform GroundCheck;
public Transform WallCheck;
//Layer
public LayerMask GroundLayer;
// Start is called before the first frame update
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (!IsWallJumping && CanMove && !IsDashing)
{
rb.velocity = new Vector2(HorizontalMovement * WalkForce, rb.velocity.y);
Flip();
}
GroundChecking();
WallChecking();
Animation();
WallSliding();
WallJump();
MoveChecking();
}
//Player Input
public void OnMove(InputAction.CallbackContext context)
{
HorizontalMovement = context.ReadValue<Vector2>().x;
if (HorizontalMovement != 0)
IsMoving = true;
else
IsMoving = false;
}
public void Jump(InputAction.CallbackContext context)
{
if (context.performed && !IsDashing)
{
if (!IsSliding && JumpLeft > 0)
{
rb.velocity = new Vector2(rb.velocity.x, JumpForce);
JumpLeft--;
}
}
else if (context.canceled)
rb.velocity = new Vector2(rb.velocity.x,rb.velocity.y * 0.5f);
if (context.performed && WallJumpTime > 0f)
{
if (HorizontalMovement != 0)
{
IsWallJumping = true;
rb.velocity = new Vector2(WallSlideDirection * WallJumpForce.x, WallJumpForce.y);
WallJumpTime = 0f;
}
else
{
IsWallJumping= true;
rb.velocity = new Vector2 (WallSlideDirection * WallJumpForce.x , rb.velocity.y);
WallJumpTime = 0f;
}
if (transform.localScale.x != WallSlideDirection)
{
IsFacingRight = !IsFacingRight;
Vector2 ls = transform.localScale;
ls.x *= -1f;
transform.localScale = ls;
}
Invoke(nameof(StopWallJump),WallJumpDuration);
}
}
public void Dashing(InputAction.CallbackContext context)
{
if (context.performed && CanDash && !IsDashing)
{
StartCoroutine(Dash());
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Ground")
{
StopCoroutine(Dash());
}
}
private void StopWallJump()
{
IsWallJumping = false;
}
//Checking
private void GroundChecking()
{
if (Physics2D.OverlapBox(GroundCheck.position, GroundCheckSize,0, GroundLayer))
{
IsGrounded = true;
JumpLeft = NumberOfJumps;
}
else
IsGrounded= false;
}
private void WallChecking()
{
if (Physics2D.OverlapBox(WallCheck.position,WallCheckSize,0,GroundLayer))
{
IsTouchingWall = true;
JumpLeft = NumberOfJumps;
}
else
{
IsTouchingWall= false;
}
}
private void MoveChecking()
{
if (!IsSliding)
CanMove = true;
else CanMove = false;
}
// ********************************************************
private void Flip ()
{
if (IsFacingRight && HorizontalMovement < 0 || !IsFacingRight && HorizontalMovement > 0)
{
IsFacingRight = !IsFacingRight;
Vector2 ls = transform.localScale;
ls.x *= -1f;
transform.localScale = ls;
}
}
private void WallSliding()
{
if (IsTouchingWall && !IsGrounded && !IsDashing)
{
rb.velocity = new Vector2(rb.velocity.x, -WallSlidForce);
IsSliding = true;
}
else IsSliding = false;
}
private void WallJump()
{
if (IsSliding)
{
IsWallJumping = false;
WallSlideDirection = -transform.localScale.x;
WallJumpTime = WallJumpTimer;
CancelInvoke(nameof(StopWallJump));
}
else WallJumpTime -= Time.deltaTime;
}
private IEnumerator Dash()
{
CanDash = false;
CanMove = false;
IsDashing = true;
rb.velocity = new Vector2(transform.localScale.x * DashPower, 0f);
yield return new WaitForSeconds(DashTime);
IsDashing = false;
CanMove = true;
yield return new WaitForSeconds(DashCooldown);
CanDash = true;
}
private void Animation()
{
anim.SetBool("IsMoving", IsMoving);
anim.SetBool("IsGrounded", IsGrounded);
anim.SetFloat("Yvelocity",rb.velocity.y);
anim.SetBool("IsSliding", IsSliding);
}
private void OnDrawGizmos()
{
Gizmos.color = Color.yellow;
Gizmos.DrawWireCube(GroundCheck.position, GroundCheckSize);
Gizmos.color = Color.red;
Gizmos.DrawWireCube(WallCheck.position, WallCheckSize);
}
}