R
R
Roman Morar2021-06-23 18:38:18
C++ / C#
Roman Morar, 2021-06-23 18:38:18

What to do if onWall doesn't want to be checked?

Faced a problem, when a character comes up to a wall, bool is not checked in the inspector (not checked) .
There are 2 dummies with a collider on the character (is Trigger enabled) here is the script

public class Buttons : MonoBehaviour
{
    public float speed;
    public float jumpForce;

    public Joystick joystick;
    public float dirX, dirY;
    

    private Rigidbody2D rb;

    public bool facingRight = true;

    private bool isGrounded;
    public Transform feetPos;
    public float checkRadius;
    public LayerMask whatIsGround;

    private Animator anim;

    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();

        WallCheckRadiusUp = WallCheckUp.GetComponent<CircleCollider2D>().radius;
        WallCheckRadiusDown = WallCheckDown.GetComponent<CircleCollider2D>().radius;
    }

    private void FixedUpdate()
    { 
        rb.velocity = new Vector2(dirX, dirY);
        
        
        CheckingWall();

    }

 
    
    //Ты Негр?
    

    private void Update()
    {
        isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);

        dirX = joystick.Horizontal * speed;
    

        
        if (dirX == 0)
            anim.SetBool("Walk", false);
        else if (dirX > 0)
            anim.SetBool("Walk", true);
        else if (dirX < 0)
            anim.SetBool("Walk", true);

        if (dirX > 0 && !facingRight)
            Flip();
        else if (dirX < 0 && facingRight)
            Flip();

        CheckingWall();

    }

    private void Jump() 
    {
        rb.velocity = Vector2.up * jumpForce;
        if (isGrounded == false)
        {
            anim.SetBool("Jump", true);
        }
        if (isGrounded == true)
        {
            anim.SetBool("Jump", false);
        }
    }
    private void Flip() 
    {
        facingRight = !facingRight;
        transform.Rotate(0f, 180f, 0f);
    }

    public bool onWall;
    public LayerMask Wall;
    public Transform WallCheckUp;
    public Transform WallCheckDown;
    private float WallCheckRadiusUp;
    private float WallCheckRadiusDown;

    void CheckingWall() 
    {
        onWall = (Physics2D.OverlapCircle(WallCheckUp.position, WallCheckRadiusUp, Wall) && Physics2D.OverlapCircle(WallCheckDown.position, WallCheckRadiusDown, Wall));
        anim.SetBool("onWall", onWall);
    }

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AnanasikDev, 2021-07-14
@AnanasikDev

Physics in Unity is arranged in such a way that to check for collisions, all objects are checked in pairs, according to the collision matrix. So, for OverlapCircle and other methods to work with physics, a rigidbody (or in your case, rigidbody2d) is required on at least one of the objects of the pair. It is also possible that in your collision matrix (Edit->ProjectSettings->Physics2D at the very bottom) the intersection of the layers of these objects is disabled.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question