Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Best way to interact with something...

Discussion in '2D' started by armedpatriots, Jul 14, 2014.

  1. armedpatriots

    armedpatriots

    Joined:
    Jul 29, 2013
    Posts:
    33
    I have been pondering what is the best way to interact with something. I have been using void OnTriggerStay2D() and then an if statement with the input and action I want to perform for about everything. Is this the most effective way to interact with things, climb ladders and such? Or is this using valuable resources and there is a better way? Very curious, thanks for your help guys!
     
  2. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    OnTriggerStay2D has a reputation for being highly inefficient.

    Personally, I just use OnTriggerEnter2D and OnTriggerExit2D to approximate OnTriggerStay2D, and I've never had a fundamental problem, even back when OnTriggerEnter2D could get extra calls.
     
  3. armedpatriots

    armedpatriots

    Joined:
    Jul 29, 2013
    Posts:
    33
    But if I am trying to let's say go up a ladder; if I use either OnTriggerEnter2D() or OnTriggerExit() then wouldn't I have to push up(to go up the ladder) on the exact frame I enter or exit? Would this be a scenario that OnTriggerStay2D() is required?
     
  4. armedpatriots

    armedpatriots

    Joined:
    Jul 29, 2013
    Posts:
    33
    Or is there a better way to tell when two objects are overlapping each other? Are triggers the only way?
     
  5. Pyrian

    Pyrian

    Joined:
    Mar 27, 2014
    Posts:
    301
    Sorry, I should've explained more.

    When you get an OnTriggerEnter2D from your ladder, set a Boolean variable to true on your Player object that says "I'm on a ladder". When you get an OnTriggerExit2D from your ladder, set that same variable to false. Then, when you're checking Inputs (which should be done in Update), you can discard any up/down inputs if you're not on a ladder.
     
  6. armedpatriots

    armedpatriots

    Joined:
    Jul 29, 2013
    Posts:
    33
    Wow, I should have thought of that. Thank you for your help!