So, I have a game in which a character slides on walls, but I want to make it so that on some walls he can’t slide and just sticks to them, so I set rb.velocity to 0, but for some reason he still has a very low sliding speed instead of sticking.
public class Player : MonoBehaviour
{
public bool slide = false;
private bool isWallSliding;
private float wallSlidingSpeed = 2f;
// FixedUpdate is called at a fixed interval
public void FixedUpdate()
{
// Check if the player should slide
if (slide)
{
WallSlide();
}
// Always check for wall sliding
WallSlide();
}
// Check if the player is touching a wall
private bool IsWalled()
{
if (!IsGrounded())
{
float radius = 0.2f;
bool onWall = Physics2D.OverlapCircle(wallCheck.position, radius, wallLayer);
if (!onWall)
{
Vector2 extraCheck = wallCheck.position + new Vector3(0, radius + 0.1f);
onWall = Physics2D.OverlapCircle(extraCheck, radius, wallLayer);
}
return onWall;
}
return false;
}
// Perform wall sliding logic
private void WallSlide()
{
if (IsWalled() && !IsGrounded() && slide == true)
{
// Check if touching a specific object
if (IsTouchingLick())
{
isWallSliding = true;
rb.velocity = new Vector2(0, Mathf.Clamp(0, 0, 0));
}
else
{
isWallSliding = true;
rb.velocity = new Vector2(rb.velocity.x, Mathf.Clamp(rb.velocity.y, -wallSlidingSpeed, float.MaxValue));
}
}
else
{
isWallSliding = false;
}
}
// Check if the player is touching a specific object
private bool IsTouchingLick()
{
Collider2D[] colliders = Physics2D.OverlapBoxAll(wallCheck.position, new Vector2(1, 1), 0f);
foreach (Collider2D col in colliders)
{
if (col.CompareTag("Lick"))
{
return true;
}
}
return false;
}
}