- Home /
Having trouble with my wall slide animation
I set up animations for running, idle, and jumping which all works fine. Now I'm working on setting up a wall slide animation which isn't working for me too well. I have the slide down the wall working when the player is against the wall and you're holding the key to move towards the wall just can't seem to get the animation working.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private Rigidbody2D rb;
private Animator anim;
private CapsuleCollider2D capcol;
[SerializeField] float playerSpeed;
[SerializeField] float jumpForce;
[SerializeField] float slideFactor;
//ground check
public Transform groundCheck;
public float groundCheckRadius;
[SerializeField] private LayerMask groundLayer;
private bool isGrounded;
//wall check for wall slide
public Transform wallCheckCollider;
[SerializeField] private LayerMask wallLayer;
public float wallCheckRadius;
private bool wallSlide;
private bool canDoubleJump;
float dirX;
// Start is called before the first frame update
void Awake()
{
rb = GetComponent<Rigidbody2D>();
capcol = GetComponent<CapsuleCollider2D>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
dirX = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(dirX * playerSpeed, rb.velocity.y);
// flips player on x axis
if (dirX > 0.01f)
transform.localScale = Vector3.one;
else if (dirX < -0.01f)
transform.localScale = new Vector3(-1, 1, 1);
if (Input.GetButtonDown("Jump"))
{
if (isGrounded)
{
Jump();
canDoubleJump = true;
}
else if (canDoubleJump)
{
jumpForce = jumpForce / 1.5f;
Jump();
canDoubleJump = false;
jumpForce = jumpForce * 1.5f;
}
}
//animator parameters
anim.SetBool("wallSlide", wallSlide);
anim.SetBool("Running", dirX != 0);
anim.SetBool("isGrounded", isGrounded);
WallCheck();
}
private void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayer);
}
void Jump()
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
if (Input.GetButtonUp("Jump") && rb.velocity.y > 0)
{
rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y / 2);
}
}
void WallCheck()
{
if (Physics2D.OverlapCircle(wallCheckCollider.position, wallCheckRadius, wallLayer)
&& Mathf.Abs(dirX) > 0
&& rb.velocity.y < 0
&& !isGrounded)
{
Vector2 v = rb.velocity;
v.y = -slideFactor;
rb.velocity = v;
}
}
}
Comment