0

Trying to create movement through vertical axis, only when objects are overlapping. I basically want my character to be fixed to the wall before they're able to climb up.

Here's my player code, no error message everything compiles, just nothing moves. Sometimes if I place the player too deep within the wall and then hit play it pushes it out and that's about it.

Here's my code:

public class Player : MonoBehaviour
{
    public GameObject wallRef;
    public Rigidbody2D rb;
    public GameObject wall1;
    public GameObject wall1RightBorder;
    private bool touchingWall = false;
    public float speed = 5f;
    
    //||
    void Start()
    {
        wall1 = wallRef.transform.Find("Wall1").gameObject;
        wall1RightBorder = wall1.transform.Find("wall1RightBorder").gameObject;
    }
    
    void Update()
    {
        if (touchingWall == true)
        {
            float verticalInput = Input.GetAxis("Vertical");
            rb.velocity = new Vector2(0f, verticalInput * speed);
        }
    }
    
    void OnCollisionStay2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("wall1RightBorder"))
        {
            touchingWall = true;
        }
    }
    
    void OnCollisionExit2D(Collision2D collision)
    {
         if (collision.gameObject.CompareTag("wall1RightBorder"))
         {
            touchingWall = false;
         } 
    }
}

1 Answer 1

0

Try OnCollisionEnter2D instead of OnCollisionStay2D. But I would use OnTriggerEnter2D, OnTriggerExit2D

Sign up to request clarification or add additional context in comments.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.