Hey.! Just set up a wall grabbing animation and noticed the bool is only triggered when my player is facing right, and not on the left when hes flipped. Any Ideas.?
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private Rigidbody2D rb;
private SpriteRenderer sr;
private Animator ani;
[Header("Movement")]
public float moveSpeed = 5f;
public float jumpPower = 8f;
private bool doubleJumpPossible;
private bool isGrounded;
[Header("Hangtime & Jump Buffer")]
public float hangTime = .1f;
public float jumpBufferLength = .1f;
private float hangCounter;
private float jumpBufferCount;
private bool canDash = true;
private bool isDashing;
private bool isWallJumping;
private float wallJumpingDirection;
private float wallJumpingTime = 0.1f;
private float wallJumpingCounter;
private float wallJumpingDuration = 0.1f;
private Vector2 wallJumpingPower = new Vector2(12f, 14f);
[Header("Dashing")]
public float dashingPower = 25;
public float dashingTime = 0.2f;
public float dashingCoolDown = 1f;
[Header("WallCheck")]
private bool isWallSliding;
private float wallSlidingSpeed = 10f;
private bool turnOnDoubleJump = true;
private bool turnOnWalljump = true;
private bool turnOnDash = true;
[SerializeField] private Transform wallCheck;
[SerializeField] private LayerMask wallLayer;
void Start()
{
rb = GetComponent<Rigidbody2D>();
sr = GetComponent<SpriteRenderer>();
ani = GetComponent<Animator>();
}
void Update()
{
if (isDashing)
{
return;
}
//Horizontal Movement
rb.velocity = new Vector2(Input.GetAxisRaw("Horizontal") * moveSpeed, rb.velocity.y);
ani.SetFloat("xVelocity", Math.Abs(rb.velocity.x));
ani.SetFloat("yVelocity", (rb.velocity.y));
ani.SetBool("isJumping", !isGrounded);
ani.SetBool("isOnWall", isWalled());
//Sprite Flip
if (rb.velocity.x > 0)
{
sr.flipX = false;
}
if (rb.velocity.x < 0)
{
sr.flipX = true;
}
//Hangtime
if (isGrounded)
{
hangCounter = hangTime;
}
else
{
hangCounter -= Time.deltaTime;
}
//Jump Buffer
if (Input.GetButtonDown("Jump"))
{
jumpBufferCount = jumpBufferLength;
}
else
{
jumpBufferCount -= Time.deltaTime;
}
//Jump/Double Jump
if(turnOnDoubleJump == true)
DoubleJump();
else
{
Jump();
}
//Wallslide/Walljump
WallSlide();
if (turnOnDash == true)
if (Input.GetKeyDown(KeyCode.Mouse0) && canDash)
{
ani.SetBool("isDashing", true);
StartCoroutine(Dash());
}
if(turnOnWalljump == true)
{
WallJump();
}
}
private void Jump()
{
if (jumpBufferCount >= 0 && hangCounter > 0f)
{
rb.velocity = new Vector2(rb.velocity.x, jumpPower);
jumpBufferCount = 0;
}
if (Input.GetButtonUp("Jump") && rb.velocity.y > 0)
{
rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
}
}
//Groundcheck
private void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.CompareTag("Ground"))
{
isGrounded = true;
doubleJumpPossible = true;
}
}
private void OnCollisionExit2D(Collision2D other)
{
if (other.gameObject.CompareTag("Ground"))
{
isGrounded = false;
}
}
private bool isWalled()
{
return Physics2D.OverlapCircle(wallCheck.position, 0.2f, wallLayer );
}
private void WallSlide()
{
if (isWalled() && !isGrounded)
{
Debug.Log("OnWall");
isWallSliding = true;
rb.velocity = new Vector2(rb.velocity.x, Mathf.Clamp(rb.velocity.y, -wallSlidingSpeed, float.MaxValue));
}
else
{
isWallSliding = false;
}
}
private void WallJump()
{
if (isWallSliding)
{
isWallJumping = false;
wallJumpingDirection = rb.velocity.y - rb.velocity.x;
wallJumpingCounter = wallJumpingTime;
CancelInvoke(nameof(StopWallJumping));
}
if(Input.GetButtonDown("Jump") && wallJumpingCounter > 0f)
{
isWallJumping = true;
rb.velocity = new Vector2(wallJumpingDirection * wallJumpingPower.x, wallJumpingPower.y);
wallJumpingCounter = 1f;
if (rb.velocity.x != wallJumpingDirection)
{
sr.flipX = !sr.flipY;
Vector3 localScale = rb.velocity;
localScale.x *= -1f;
rb.velocity = localScale;
}
Invoke(nameof(StopWallJumping), wallJumpingDuration);
}
}
private void StopWallJumping()
{
isWallJumping = false;
}
private IEnumerator Dash()
{
canDash = false;
isDashing = true;
float originalGravity = rb.gravityScale;
rb.gravityScale = 0f;
rb.velocity = new Vector2(rb.velocity.x * dashingPower, 0f);
yield return new WaitForSeconds(dashingTime);
rb.gravityScale = originalGravity;
ani.SetBool("isDashing", false);
isDashing = false;
yield return new WaitForSeconds(dashingCoolDown);
canDash = true;
}
private void DoubleJump()
{
if (jumpBufferCount >= 0)
{
if (hangCounter > 0f)
{
rb.velocity = new Vector2(rb.velocity.x, jumpPower);
jumpBufferCount = 0;
}
else if (doubleJumpPossible)
{
rb.velocity = new Vector2(rb.velocity.x, jumpPower);
jumpBufferCount = 0;
doubleJumpPossible = false;
}
}
}
}