Wall Detection will work only when facing right.

Hey.! Just set up a wall grabbing animation and noticed the bool is only triggered when my player is facing right, and not on the left when hes flipped. Any Ideas.?

using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEditor.Experimental.GraphView;
using UnityEngine;

public class PlayerController : MonoBehaviour
{

    private Rigidbody2D rb;
    private SpriteRenderer sr;
    private Animator ani;
    [Header("Movement")]
    public float moveSpeed = 5f;
    public float jumpPower = 8f;
  
    private bool doubleJumpPossible;
         
    private bool isGrounded;
    [Header("Hangtime & Jump Buffer")]
    public float hangTime  = .1f;
    public float jumpBufferLength = .1f;
 
    private float hangCounter;
    private float jumpBufferCount;

    private bool canDash = true;
    private bool isDashing;

    private bool isWallJumping;
    private float wallJumpingDirection;
    private float wallJumpingTime = 0.1f;
    private float wallJumpingCounter;
    private float wallJumpingDuration = 0.1f;
    private Vector2 wallJumpingPower = new Vector2(12f, 14f);
 

    [Header("Dashing")]
    public float dashingPower = 25;
    public float dashingTime = 0.2f;
    public float dashingCoolDown = 1f;
  
    [Header("WallCheck")]
    private bool isWallSliding;
    private float wallSlidingSpeed = 10f;


    private bool turnOnDoubleJump = true;
    private bool turnOnWalljump = true;
    private bool turnOnDash = true;




    [SerializeField] private Transform wallCheck;
    [SerializeField] private LayerMask wallLayer;
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        sr = GetComponent<SpriteRenderer>();
        ani = GetComponent<Animator>();
 
    }

  
    void Update()
    {
        if (isDashing)
        {
            return;
        }
     


        //Horizontal Movement
     
        rb.velocity = new Vector2(Input.GetAxisRaw("Horizontal") * moveSpeed, rb.velocity.y);
     
        ani.SetFloat("xVelocity", Math.Abs(rb.velocity.x));
        ani.SetFloat("yVelocity", (rb.velocity.y));
        ani.SetBool("isJumping", !isGrounded);
        ani.SetBool("isOnWall", isWalled());
      


        //Sprite Flip

        if (rb.velocity.x > 0)
        {
            sr.flipX = false;
        }
        if (rb.velocity.x < 0)
        {
            sr.flipX = true;
        }

        //Hangtime
        if (isGrounded)
        {
            hangCounter = hangTime;
        }
        else
        {
            hangCounter -= Time.deltaTime;
        }

        //Jump Buffer
        if (Input.GetButtonDown("Jump"))
        {
            jumpBufferCount = jumpBufferLength;

        }
        else
        {
            jumpBufferCount -= Time.deltaTime;
        }
        //Jump/Double Jump
        if(turnOnDoubleJump == true)
        DoubleJump();
        else
        {
            Jump();
        }
      
        //Wallslide/Walljump
        WallSlide();
  


        if (turnOnDash == true)
        if (Input.GetKeyDown(KeyCode.Mouse0) && canDash)
        {

            ani.SetBool("isDashing", true);
          
            StartCoroutine(Dash());

            }
   
     if(turnOnWalljump == true)
        {
            WallJump();          
        }

   
    }
    private void Jump()
    {
        if (jumpBufferCount >= 0 && hangCounter > 0f)
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpPower);
            jumpBufferCount = 0;

        }


        if (Input.GetButtonUp("Jump") && rb.velocity.y > 0)
        {
            rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
        }
     

      
      
    }

 

    //Groundcheck
    private void OnCollisionEnter2D(Collision2D other)
    {
        if (other.gameObject.CompareTag("Ground"))
        {
            isGrounded = true;
            doubleJumpPossible = true;
        }
    }
    private void OnCollisionExit2D(Collision2D other)
    {
        if (other.gameObject.CompareTag("Ground"))
        {
            isGrounded = false;        
          
        }
    }

    private bool isWalled()
    {
        return Physics2D.OverlapCircle(wallCheck.position, 0.2f, wallLayer );
      
    }  
    private void WallSlide()
    {
        if (isWalled() && !isGrounded)      
        {
            Debug.Log("OnWall");
            isWallSliding = true;
            rb.velocity = new Vector2(rb.velocity.x, Mathf.Clamp(rb.velocity.y, -wallSlidingSpeed, float.MaxValue));

        }
        else
        {
            isWallSliding = false;
        }
    }
    private void WallJump()
    {
        if (isWallSliding)
        {
          
            isWallJumping = false;
            wallJumpingDirection = rb.velocity.y - rb.velocity.x;
          
            wallJumpingCounter = wallJumpingTime;

         
            CancelInvoke(nameof(StopWallJumping));
          

        }
     
        if(Input.GetButtonDown("Jump") && wallJumpingCounter > 0f)
        {
          
            isWallJumping = true;
            rb.velocity = new Vector2(wallJumpingDirection * wallJumpingPower.x, wallJumpingPower.y);
            wallJumpingCounter = 1f;

            if (rb.velocity.x != wallJumpingDirection)
            {
                sr.flipX = !sr.flipY;              
                Vector3 localScale = rb.velocity;
                localScale.x *= -1f;
                rb.velocity = localScale;

            }
          
            Invoke(nameof(StopWallJumping), wallJumpingDuration);
        }
    }

    private void StopWallJumping()
    {
        isWallJumping = false;
    }

    private IEnumerator Dash()
    {
     
        canDash = false;
        isDashing = true;      
        float originalGravity = rb.gravityScale;
        rb.gravityScale = 0f;
        rb.velocity = new Vector2(rb.velocity.x * dashingPower, 0f);     
        yield return new WaitForSeconds(dashingTime);
        rb.gravityScale = originalGravity;
        ani.SetBool("isDashing", false);
        isDashing = false;
        yield return new WaitForSeconds(dashingCoolDown);
        canDash = true;
      


    }

    private void DoubleJump()
    {
        if (jumpBufferCount >= 0)
        {
            if (hangCounter > 0f)
            {
                rb.velocity = new Vector2(rb.velocity.x, jumpPower);
                jumpBufferCount = 0;
            }
            else if (doubleJumpPossible)
            {
                rb.velocity = new Vector2(rb.velocity.x, jumpPower);
                jumpBufferCount = 0;
                doubleJumpPossible = false;
            }
        }
     

    }
  







}

Yes! It is time for you to start debugging.

By debugging you can find out exactly what your program is doing so you can fix it.

First place I would look is the logic that does the check (however you are doing this) to find the wall. Are all the values going into it reasonable? Is it even executing? etc.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log() to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

When you find out a little bit more, if you still have questions…

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, log output, variable values, and especially any errors you see
  • links to actual Unity3D documentation you used to cross-check your work (CRITICAL!!!)

The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven’t put effort into finding the documentation, why should we bother putting effort into replying?

You need to flip the wallCheck.position when your character changes direction. You could do it along side the code that flips the sprite when the x velocity changes.

1 Like

Oh snap is it really that easy.? I honestly though it would flip with the sprite being its a child of it. Ill write it out and see it thats the issue.

I cant seem to get it moving, maybe im just throwing the wrong code at it. Im trying to flip it using a transform.localscale and -1 off the x. wallCheck.transform.localScale = new Vector3 (-1f, 0f, 0f);

Is this the proper way to do this or am i just blanking here.? hah.

You can do something like this:

        if (rb.velocity.x > 0)
        {
            wallCheck.position = Vector3.right*0.5f;
            sr.flipX = false;
        }
        else if (rb.velocity.x < 0)
        {
            wallCheck.position = -Vector3.right*0.5f;
            sr.flipX = true;
        }

Or you may be able to get away with flipping the sprite’s localScale instead of the sprite renderer:

        if (rb.velocity.x > 0)
            transform.localScale=new Vector3(1,1,1);
        else if (rb.velocity.x < 0)
            transform.localScale=new Vector3(-1,1,1);

Doing this will also flip wallCheck.position because it’s a child of the sprite.

Thanks for the reply, unfortunately, either seem to work. Such a small issue that cant seem to be found. Coding at its finest. Even got frustrated and made a whole separate WallCheck, but had it originate from the left side, and still wont register the trigger. Very confusing.

flipX in the sprite renderer only affects how the sprite is drawn. It is not going to affect anything else in the object or its children.

If you set the x parameter of localScale in the parent object, then it will effectively flip all of the children as well (including any x offset they have set in their localPosition.

You can take a look for yourself in the editor. Change the localScale of the parent object, then click on the wallCheck object to see where it is in the world. Similarly, you can toggle flipX in the sprite renderer and see if the position of wallCheck changes.

When something is not working, it can help to pause the play mode and look at the state of the objects in the scene to see if they match your expectations. If this is not enough information to determine what is happening, then you may need to add more Debug statements or use a debugger.

Thank you, after lots of trial and error today. I finally got it working. Always feels nice as a beginner to figure something out and feel a win. On to the next problems.! hah. Thanks to everyone who tried to help.

1 Like