- Home /
Sprite turning around repeatedly against a wall. How to have "Wallslide" flip sprite?
Hello! So I have been messing around with unity, trying to get better at programming. The issue I am currently experiencing is in regards to wall-sliding. Presently, I have my character move left when holding A (and his transform.localscale.x = 1) , and move to the right with D (and his transform.localscale.x = -1). These functions also work when in a jumping or falling state.
However, the issue I have been suffering from is wall-sliding. I am trying to have my character face to the right when clinging on a left wall, and vice versa, however, despite after testing several different methods, I can't seem to get it to work. What ends up happening is that it functions perfectly normal when on the ground/air, but the second I get against a wall, the transform.localscale.x shoots between 1 and -1, rapidly, causing my sprite to spazz out.
Here is my code thus far (This lacks anything in regards to the flip when wall-sliding due to me not being able to figure it out):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerMovement : MonoBehaviour
{
[SerializeField] float jumpHeight;
[SerializeField] float runSpeed;
private PlayerInputScript playerActionControls;
private Rigidbody2D rb;
private Animator ani;
private Transform smTransform;
private CapsuleCollider2D capCollider;
private BoxCollider2D boxCollider;
bool facingRight = true;
bool wallSliding = false;
private void Awake()
{
playerActionControls = new PlayerInputScript();
rb = GetComponent<Rigidbody2D>();
ani = GetComponent<Animator>();
smTransform = GetComponent<Transform>();
capCollider = GetComponent<CapsuleCollider2D>();
boxCollider = GetComponent<BoxCollider2D>();
}
private void OnEnable()
{
playerActionControls.Enable();
}
private void OnDisable()
{
playerActionControls.Disable();
}
void Start()
{
playerActionControls.PlayerMovement.Jumping.performed += _ => Jumping();
}
void Update()
{
Running();
JumpingAni();
FallingAni();
WallSlide();
}
private void Running()
{
float rlRunning = playerActionControls.PlayerMovement.Running.ReadValue<float>();
Vector2 playerVelocity = new Vector2(rlRunning * runSpeed, rb.velocity.y);
rb.velocity = playerVelocity;
//Sets Animation
if (Mathf.Abs(rlRunning) <= 0)
{
ani.SetBool("Running", false);
}
else
{
ani.SetBool("Running", true);
}
//Flips Sprite depending on movement
if (rlRunning > 0 && !facingRight && !wallSliding)
{
FlipSprite();
}
else if (rlRunning < 0 && facingRight && !wallSliding)
{
FlipSprite();
}
}
private void Jumping()
{
if (!boxCollider.IsTouchingLayers(LayerMask.GetMask("Foreground")))
{
return;
}
rb.AddForce(new Vector2(0, jumpHeight), ForceMode2D.Impulse);
}
private void JumpingAni()
{
if(rb.velocity.y >= .1)
{
ani.SetBool("Jumping", true);
}
if (rb.velocity.y <= 0)
{
ani.SetBool("Jumping", false);
}
}
private void FallingAni()
{
if (rb.velocity.y <= -.1 && !capCollider.IsTouchingLayers(LayerMask.GetMask("Walls")))
{
ani.SetBool("Falling", true);
}
if (rb.velocity.y >= 0)
{
ani.SetBool("Falling", false);
}
if (boxCollider.IsTouchingLayers(LayerMask.GetMask("Foreground"))){
ani.SetBool("Falling", false);
}
}
private void WallSlide()
{
if(capCollider.IsTouchingLayers(LayerMask.GetMask("Walls")) && (!boxCollider.IsTouchingLayers(LayerMask.GetMask("Foreground")))){
Vector2 wallslideVelocity = new Vector2(rb.velocity.x, .5f * rb.velocity.y);
rb.velocity = wallslideVelocity;
ani.SetBool("Wallsliding", true);
wallSliding = true;
}
else
{
ani.SetBool("Wallsliding", false);
wallSliding = false;
}
}
private void FlipSprite()
{
facingRight = !facingRight;
Vector2 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
Your answer
Follow this Question
Related Questions
box collider 2d are not touching! please help!,box Collider 2d are not touching 0 Answers
How can I add parallax to background sprites which are being instantiated only in play mode. 1 Answer
How to sync animation if animation should be use with specially object 0 Answers
Instantiate a GameObject with a specific Z rotation 2 Answers
My 2D Player Can't Move (2d Photon Game) :(,My Player Don't Move (2d Character Controller 0 Answers