Question by
Tikles · Feb 28, 2021 at 06:55 PM ·
raycastif-statements
Raycast "If" statements conflicting with each other.
I'm quite new at programming and I'd like to know how I would be able to solve this problem.
So I made 2 raycasts coming from 2 different sides of my character, and 2 if statements to go along with them, however only one of the if statements actually work, I think one might be overwriting another or something, but I don't know how that's possible since the raycasts are two different things.
Here's my code:
void FixedUpdate()
{
moveInput = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
//-------------------------CODE AT HAND BELOW THIS LINE---------------------------
wallCheckHitRight = Physics2D.Raycast(wallCheckRight.position, wallCheckRight.right, wallCheckDistanceRight, whatIsGround);
wallCheckHitLeft = Physics2D.Raycast(wallCheckLeft.position, wallCheckLeft.right, -wallCheckDistanceLeft, whatIsGround);
if(wallCheckHitRight && rb.velocity.y <= 0 && groundCheck == false){
isWallSliding = true;
}
if(wallCheckHitLeft && rb.velocity.y <= 0 && groundCheck == false){
isWallSliding = true;
}
else{
isWallSliding = false;
}
//-------------------------CODE AT HAND ABOVE THIS LINE---------------------------
if(isWallSliding){
Debug.Log("Bruh");
}
//limit fall velocity while hugging wall
if (isWallSliding == true){
if(rb.velocity.y < -maxWallSlideVelocity){
rb.velocity = new Vector2(rb.velocity.x, -maxWallSlideVelocity);
}
}
}
Sorry if it's some dumb mistake but I've been at this for an hour or two and I really don't know how to solve this.
Comment