Goal is to implement wall jumping mechanic.
public Transform wallCheck;
public LayerMask wallLayer;
void Update()
{
if(WallCheck())
{
Debug.Log("Is wall");
}
}
bool WallCheck()
{
return Physics2D.OverlapCircle(wallCheck.position, 0.2f, wallLayer);
}
private void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(wallCheck.position, .2f);
}
OnDrawGizmos()
to visualize the Physics2D.OverlapCircle()
OverlapCircle()
hits the wall layer, on the console it should say "Is wall".void WallSlide()
{
if (!isGround && WallCheck() && h != 0f)
{
isWallSliding = true;
rigid.velocity = new Vector2(rigid.velocity.x, Mathf.Clamp(rigid.velocity.y, -wallSlidingSpeed, float.MaxValue));
}
}
Mathf.Clamp()
: Constrains a value within a specified range.Mathf.Clamp(float value to be clamped, float min, float max)
The function returns
- The original value if it's within the specified range.
- The minimum value if the original value is less than the minimum.
- The maximum value if the original value is greater than the maximum.
Mathf.Clamp()
is used to prevent accelerating while sliding down the wall.void WallSlide()
{
if (!isGround && WallCheck() && h != 0f)
{
isWallSliding = true;
rigid.velocity = new Vector2(rigid.velocity.x, Mathf.Clamp(rigid.velocity.y, -wallSlidingSpeed, float.MaxValue));
}
else
{
isWallSliding = false;
}
}
else
is isWallSliding = false;
I’ve been hooked on Krunker io for weeks. The fast-paced action and variety of game modes keep me entertained. The pixel graphics are surprisingly effective, and the community content is a nice touch.