Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Problem with OnTriggerStay2D

Discussion in 'Scripting' started by misterG420, Jan 29, 2021.

  1. misterG420

    misterG420

    Joined:
    Jan 23, 2020
    Posts:
    31
    Hi all,

    I don't know what I am missing - it must be something really obvious, but I cannot get my head around why this would not work:

    The idea of the script is that you can ONLY move left and right, unless you run into an item that lets you climb (ladder, tagged as "climbable"). Only then can you move upwards.

    Problem is, once you collide with "climbable" you can move upwards even if you left the box collider of "climbable"...

    Any tipps are much appreciated <3

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Movement : MonoBehaviour
    6. {
    7.  
    8.     public float moveSpeed = 5f;
    9.     public Rigidbody2D rb;
    10.  
    11.     private bool canClimb = false;
    12.  
    13.     Vector2 movement;
    14.  
    15.     void Update()
    16.     {
    17.         movement.x = Input.GetAxisRaw("Horizontal");
    18.      
    19.         if (canClimb)
    20.         {
    21.             movement.y = Input.GetAxisRaw("Vertical");
    22.         }
    23.         canClimb = false;
    24.                    
    25.     }
    26.  
    27.     private void FixedUpdate()
    28.     {
    29.         rb.MovePosition(rb.position + movement * moveSpeed * Time.deltaTime);
    30.     }
    31.  
    32.     private void OnTriggerStay2D(Collider2D other)
    33.     {
    34.         if (other.gameObject.tag == "Cilmbable")
    35.         {
    36.             canClimb = true;
    37.         }
    38.      
    39.     }
    40.  
    41. }
    42.  
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    Shouldn't you do something like:
    Code (CSharp):
    1.         if (canClimb)
    2.         {
    3.             movement.y = Input.GetAxisRaw("Vertical");
    4.         }
    5.         else {
    6.             movement.y = 0;
    7.         }
    That way the y value in the movement vector gets reset to 0.
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    Also rather than using OnTriggerStay you should use OnTriggerEnter and Exit:
    Code (CSharp):
    1.     private void OnTriggerEnter2D(Collider2D other)
    2.     {
    3.         if (other.gameObject.tag == "Cilmbable")
    4.         {
    5.             canClimb = true;
    6.         }
    7.     }
    8.  
    9.     private void OnTriggerExit2D(Collider2D other)
    10.     {
    11.         if (other.gameObject.tag == "Cilmbable")
    12.         {
    13.             canClimb = false;
    14.         }
    15.  
    16.     }
    And remove canClimb = false from Update
     
  4. misterG420

    misterG420

    Joined:
    Jan 23, 2020
    Posts:
    31
    Thanks! Works like a charm now :) But shouldn't OnTriggerStay keep the bool true for only the period that I "stay" in the trigger? I thought that was the whole purpose of OnTriggerStay?
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    Kinda? The thing is OnTriggerStay doesn't run every frame. It only runs when the Physics update happens. Update can potentially happen many times in between each OnTriggerStay, because update runs every frame.