olewis9
olewis9

Reputation: 1

Possible collision not working? no Movement

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;
         } 
    }
}

Upvotes: 0

Views: 21

Answers (1)

Maksim
Maksim

Reputation: 1

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

Upvotes: 0

Related Questions