How To INTERACT with Game Objects using UNITY EVENTS Tutorial

แชร์
ฝัง
  • เผยแพร่เมื่อ 10 ก.ย. 2024
  • Interacting with GameObjects within your scenes is a core tenet to game development within the Unity Engine. I wanted to engineer a system that would show off Unity Events, while also being generic enough to slap on to any of your objects and wire up quickly.
    Admittedly there are many ways to improve upon the basics I show in the video (as well as lots of error checking and handling!), but I wanted to demonstrate the concept in a concise and timely manner.
    I'm confident this can be a useful launching point, and I hope you'll find it useful!
    ✨Want to support the channel?
    Buy me a coffee ☕: ko-fi.com/bmoli
    LIKE the video if this helped you out at all, it helps the channel's visibility and lets me know what I got right!
    COMMENT if you have any suggestions or critiques! (or just want to say hi)
    SUBSCRIBE for more tutorials and game development content!
    Thanks for watching!!
    MORE UNITY TUTORIALS (PLAYLIST): • Epic Unity Tutorials
    SOUND EFFECT TUTORIAL MENTIONED: • Improve every SOUND EF...
    NEW to game development? Start with my 2D Movement Tutorial: • 2D Side Scroller MOVEM...
    ~~~~~~~~~~~
    ASSET LIST
    ~~~~~~~~~~~
    Ninja Character: assetstore.uni...
    ~~~~~~~~~~~
    #bmo #unity #tutorial

ความคิดเห็น • 103

  • @Daniel-qt5ib
    @Daniel-qt5ib 4 ปีที่แล้ว +20

    Thank you so much! I haven't used Unity Events much before but they're perfect for things like this. Well explained too X)

  • @joaquin.galvan
    @joaquin.galvan 4 ปีที่แล้ว +26

    Dude you just saved my ass, keep on going you're great, YT need more tutorials like yours

    • @BMoDev
      @BMoDev  4 ปีที่แล้ว +5

      Thanks, I really appreciate hearing that!

  • @olivertom1
    @olivertom1 3 ปีที่แล้ว +4

    Me: Hmmm does video have the info I want?
    Intro: Yes exactly what you want!
    Thank you for showing what you are doing at the start so I didn't have to watch half the video to find out!

  • @Johnuao
    @Johnuao 2 ปีที่แล้ว +1

    Wow, this just ease a lot the script. My codes were loaded of GetComponents because i didn't know how to use this function

  • @MasterAkiDraw
    @MasterAkiDraw 3 ปีที่แล้ว +2

    THE BEST tutorial on youtube for unity interactions! ty!

    • @BMoDev
      @BMoDev  3 ปีที่แล้ว +1

  • @MC-yo6kz
    @MC-yo6kz ปีที่แล้ว

    This helped me clean up my old door system to something way better and more flexible. Thank you

  • @scratchy996
    @scratchy996 4 ปีที่แล้ว +6

    Came for the title, stayed for the duck.

  • @B4NTO
    @B4NTO 3 ปีที่แล้ว +2

    Thanks for this Awesome Tutorial it helped me immensely with my project that I'm building atm only for learning by doing

  • @ilyasformuli.2779
    @ilyasformuli.2779 ปีที่แล้ว

    I feel like I've struck gold with this video. Amazing tutorial!

  • @luckyknot
    @luckyknot ปีที่แล้ว

    Again another dope tutorial, it clarified a lot how Unity Events work, thanks!

  • @tomasbeno1593
    @tomasbeno1593 3 ปีที่แล้ว +1

    i have a prolem with eventsystem, i have 2 scenes (MainMenu , Game) . Event system as an object is in MainMenu scene (like in hierarchy) , but when i laod scene "Game" , buttons in the "Game" are not working. If i swap EventSystem to the scene Game , it works , but then when i go back to "MainMenu" , the buttons do not work there. Unity says i can have only one EventSystem, so how does it work? How can i "share" eventSystem for whole project , or all scenes ? thank you

  • @eileeng2492
    @eileeng2492 3 ปีที่แล้ว +2

    Great video. Good content.
    You deserve alot more subs.

  • @fungusguy
    @fungusguy 4 ปีที่แล้ว +2

    Hey when I saved the first bit of script you did it says it doesnt recognize "UnityEvent" I'm sure it's something on my end but any help?

    • @bensmart2829
      @bensmart2829 4 ปีที่แล้ว +2

      did you have using UnityEngine.Events; at the top of your script?

    • @fungusguy
      @fungusguy 4 ปีที่แล้ว +1

      @@bensmart2829 thanks yes I did I figured it out but thanks for the help :)

    • @cabbagemontage6999
      @cabbagemontage6999 3 ปีที่แล้ว

      @@bensmart2829 Fixed that problem for me, but it still has an issue where it does not recognize "KeyCode"

  • @endy7562
    @endy7562 2 ปีที่แล้ว

    im trying to make a player "cursor" to be able to interact with the objects, i followed every step to the near exact dot, exept names of objects and a few variables and it wont interact, what do i do?

  • @baby-sheesh
    @baby-sheesh ปีที่แล้ว

    really good explanation! thank you

  • @khoichau8088
    @khoichau8088 2 ปีที่แล้ว

    i bad at code so i look at your and try to learn something. WHenever i copy Unity Event as same as your. It appear this.
    "The type or namespace name 'UnityEvent' could not be found (are you missing a using directive or an assembly reference?)"

  • @gi6624
    @gi6624 4 ปีที่แล้ว +1

    Thank you so much you saved my game

    • @BMoDev
      @BMoDev  4 ปีที่แล้ว +1

      Thanks, but you put in the work! 🙏 Good luck, would love to see your game!

  • @hannahmclaughlin5587
    @hannahmclaughlin5587 ปีที่แล้ว +4

    I watched your dialogue box tutorial as well, is there a way to combine it so that it plays the script with a interaction?

    • @Its_Noctis
      @Its_Noctis 7 หลายเดือนก่อน

      i know you left the comment a year ago, but have you found a way to make it work? i am a bit stuck myself...

  • @HeyImGlen
    @HeyImGlen ปีที่แล้ว

    What if you have two chests close by both in range? How do you make sure only one opens if you click the interact key?

  • @xmahopabuse8501
    @xmahopabuse8501 4 ปีที่แล้ว +1

    The video was very helpful, thank you.

    • @BMoDev
      @BMoDev  4 ปีที่แล้ว

      Glad I could help!

  • @cinemacat4108
    @cinemacat4108 3 ปีที่แล้ว

    Thank you very much you helped me a lot

  • @cabbagemontage6999
    @cabbagemontage6999 3 ปีที่แล้ว +1

    I have a question - What if two interactables are too close to each other, and I just want the player to activate one of them, and not both at the same time?
    Like, if the player enters both the collider of a box and a door at the same time, and for gameplay reasons, the things are close to each other... How can I make him only interact with one?

    • @evikingg5262
      @evikingg5262 3 ปีที่แล้ว +1

      Maybe make some sort of rotating a character in the game, and when player is rotated right, you(for ex.) open right chest, but if he's rotated left open the left chest. But I don't know how to code it...

    • @cabbagemontage6999
      @cabbagemontage6999 3 ปีที่แล้ว

      I ended up doing something different :
      Since I'm doing a top-down game, I can attach qualities to the pointer...
      I still needed the Collider for the animation though.
      So, my solution was to create a separate collider that is activatable when the pointer hovers over it - The pointer itself is another object attached to the mouse cursor... So, I gave it a rigidbody, the tag "pointer", and when it hovers over the collider, it makes the colider for the animation/action activatable - only when the pointer hovers over it.
      When the "take" animation starts, it deactivates the other collider, so that it won't bother with the animation process.
      If I were doing a side scroller... I think the rotation could work, but, what if someone wants to first take something specific?
      without the mouse cursor... Perhaps it could bring up a wheel that holds every interactable item in a small radious, and you can select from it which one to take.
      No idea as for the code, but, selection through a small menu if there are >1 colliders in radious sounds reasonable.

  • @yousifsuleiman7613
    @yousifsuleiman7613 4 ปีที่แล้ว

    I am so happy for finding an awsome channal 😍

    • @BMoDev
      @BMoDev  4 ปีที่แล้ว

      The feeling is mutual!

  • @cheesecake906
    @cheesecake906 3 ปีที่แล้ว

    Oh wow thank you so much!!

  • @jainyash900
    @jainyash900 3 ปีที่แล้ว +1

    When I press check in the "Is open" for the chest my chest animation does not work. Any solutions?

    • @rari9059
      @rari9059 ปีที่แล้ว

      Did you solve it?

  • @Fabian-bx2ci
    @Fabian-bx2ci 2 ปีที่แล้ว +1

    What did you did in the animator from the chest?

  • @keeleon
    @keeleon 4 ปีที่แล้ว +1

    How do you toggle the sprite between open and closed? I created a single sprite animation in the animator and it kind of works, but it doesnt toggle with the checkbox like yours does.

    • @BMoDev
      @BMoDev  4 ปีที่แล้ว +2

      I'd look up how to change Animation state in Unity. I'll eventually make a video on this.
      Basically you need to make a bool variable in the animator (not in script). Setup transitions between open/close animations and use the animation bool to determine how it transitions. Then in a script, when you interact, change the objects animation bool.
      It's a lot to cover in a comment, but that is the general idea. There are many resources out there that cover animation transitions. You can do it, good luck! :)

    • @mrzack4299
      @mrzack4299 2 ปีที่แล้ว

      @@BMoDev Can you explain more, like i need to know

  • @robbercool
    @robbercool 4 ปีที่แล้ว +1

    Awsome video man!

    • @BMoDev
      @BMoDev  4 ปีที่แล้ว

      Thank you, friend

  • @18vlsnp
    @18vlsnp 3 ปีที่แล้ว

    This is exactly what iv been looking BUT unfortunately my unity is an older version, gna have to use normal events instead of Unity Events? :(

  • @kurtthorsten4463
    @kurtthorsten4463 2 ปีที่แล้ว

    very good tutorial. thank you

  • @bladeinferioryt
    @bladeinferioryt 3 ปีที่แล้ว

    hi i was wondering how you referenced your notification in vs i tried obj = GetComponent(); (ring is the name of the image) and i have already defined public GameObject obj but it says the type or namespace ‘Ring’ couldn’t be found

    • @jamespope5668
      @jamespope5668 3 ปีที่แล้ว

      You put the class in the carrot brackets. So for instance if you wanted to find the script he made in this video you'd type obj = GetComponent();
      Are you trying to get a specific image file? Because GetComponent(); can only be used on scripts that inherit from Monobehaviour

  • @neisanland2503
    @neisanland2503 2 ปีที่แล้ว

    finally i figure out the the closing.
    here if u want to open and close.
    public bool isOpen;
    public Animator animator;
    public void LeverControl()
    {
    if (!isOpen)
    {
    isOpen = true;
    Debug.Log("Lever is now Down");
    animator.SetBool("isOpen", true);
    }
    else if (isOpen)
    {
    isOpen = false;
    Debug.Log("Lever is now Up");
    animator.SetBool("isOpen", false);
    }
    }
    *on Unity Animator when creating animation u have to add bool onto the parameters. isOpen. when checked means true and when unchecked means false.
    i hope i helped somehow, Im also a beginner.

  • @Stompin40
    @Stompin40 2 ปีที่แล้ว

    This is awesome!

  • @cognusjohn1269
    @cognusjohn1269 4 ปีที่แล้ว

    This is awesome thank you!!

    • @BMoDev
      @BMoDev  4 ปีที่แล้ว

      You're awesome, hope it helps!

  • @teebot5501
    @teebot5501 10 หลายเดือนก่อน

    My version of Unity 3.6.0 doesn’t recognise UnityEvent as a variable type, has it changed?

    • @josipbroztito8005
      @josipbroztito8005 10 หลายเดือนก่อน

      Hi I think Unity 3.6.0 is your Unity Hub version, I think you should be looking at the editor version (e.g. 2022.3.11f1) and something is not working for me too can you let me know if you made it work. For me it only detects when im NOT in range. tnx

    • @teebot5501
      @teebot5501 10 หลายเดือนก่อน

      @@josipbroztito8005 I managed to figure it out like a few minutes after I posted that comment lol. Mak sure you include unityevents in your libraries, then the variable type should be okay.

  • @galahad4514
    @galahad4514 2 ปีที่แล้ว

    Was that like a giant yoyo?? (Duncan yoyo). Thanks for the info btw :)

  • @tbprod5
    @tbprod5 3 ปีที่แล้ว

    If I’m making a 3D game do I change the trigger code to enter 3D and collider2d to 3D? Thanks, showbiz pizza fan 101

  • @TheAstronomyGame
    @TheAstronomyGame 4 ปีที่แล้ว +1

    This is a very good tutorial, but I am encountering a problem. For my interactAction, I have a change scene script. However, whenever I try to use it, I get the error “ArgumentException; object of type ‘UnityEnginge.object’ cannot be converted to ‘UnityEnginge.Collider2D’. I have ‘Collider2D’ in the parenthesis of my interactAction. Any advice would be really appreciated.

    • @ThatsCrazi
      @ThatsCrazi 2 ปีที่แล้ว

      You misspelled UnityEngine

    • @ThatsCrazi
      @ThatsCrazi 2 ปีที่แล้ว

      You put "UnityEnginge"

  • @durrium
    @durrium 3 ปีที่แล้ว +1

    Very nice! When we want to close the chest? How would we do it :D

  • @BoomBunny
    @BoomBunny 4 ปีที่แล้ว +1

    This helped so much xD Thank youuuu! Also is it possible to change the Interact keycode within another event?

    • @BMoDev
      @BMoDev  4 ปีที่แล้ว +3

      Awesome! Yeah definitely, you'd just have to change the variable Keycode

  • @jillianjones5116
    @jillianjones5116 25 วันที่ผ่านมา

    Where's the chest controller script video?

  • @tonenfich4582
    @tonenfich4582 2 ปีที่แล้ว

    What must i do if i want this for 3d project work?

  • @gistypical
    @gistypical 3 ปีที่แล้ว

    You are a god.

  • @_abhirup_3942
    @_abhirup_3942 5 หลายเดือนก่อน

    Input.GetKeyDown doesn't work...Unity says Imput doesn't have GetKeyDown

  • @darkdoom907
    @darkdoom907 2 ปีที่แล้ว +1

    how to close that door?

  • @markoviik3324
    @markoviik3324 3 ปีที่แล้ว

    What about 3d game? do i still use 2dcollider?

  • @UnisRapier
    @UnisRapier 2 ปีที่แล้ว

    amazing tutorial

  • @revmatch6r
    @revmatch6r 4 ปีที่แล้ว +1

    Awesome vid!! Quick question tho - what happens if there's 2 intractable circles that overlap? Is there any way to say that one should be interacted with over the other?

    • @BMoDev
      @BMoDev  4 ปีที่แล้ว +4

      Right now, in the current state of this video it would trigger both of them. There is absolutely a way to prioritize (conceptually, you could queue them and execute one by one, or by some other factor like distance to the player), but in terms of implementation - that would be a larger discussion. This is more just a feature starter to understand the concept.

  • @kcmdanks999
    @kcmdanks999 2 ปีที่แล้ว

    I don't get how the chest changes to being open when executed, is there something that I missed?

    • @BMoDev
      @BMoDev  2 ปีที่แล้ว

      No, I don't explicitly cover it, but I do quickly show the script for what its doing. Basically the chest just has 2 animation states, closed and open, and interacting with the chest is toggling that animation change.

  • @GreyDawg13
    @GreyDawg13 ปีที่แล้ว

    When I used the UnityEvent it gave me an error

  • @molyporph1045
    @molyporph1045 4 ปีที่แล้ว

    Super dumb question, but how did u make running left animation for this character model, not sure if I'm using wrong version of it or there is simple way to implement that.

    • @BMoDev
      @BMoDev  4 ปีที่แล้ว +1

      You can checkout my 2D Sidescroller Movement tutorial where I show in code how to do it, but basically you just flip your player sprites. You re-use the same animation in one direction it just mirrors in the opposite.
      Very quick to do.

    • @molyporph1045
      @molyporph1045 4 ปีที่แล้ว

      @@BMoDev Thank you for your answer, yeh that was super quick.
      Got another question, for which I cannot find answer somehow... I'm trying to make some kind of Among Us copy.. i cannot find an answer how to reload my MiniGame Scene (when u die in it or w.e) without reloading my MainGame scene(i dont want position of my character or task bar to change) which is working in background additively..

    • @molyporph1045
      @molyporph1045 4 ปีที่แล้ว

      Nvm found one i think, finally :) how to make singletone of class, which i will apply to player and make new object for score and do it the same i guess..
      Now the only problem i have is how to open a scene in pop up window in front of main game, i'm thinking it has to do Camera views probably :)

  • @yoko2579
    @yoko2579 4 ปีที่แล้ว

    OMG Thank u very much!

    • @BMoDev
      @BMoDev  4 ปีที่แล้ว

      Np!

  • @williamallen6382
    @williamallen6382 3 ปีที่แล้ว

    How do you create the ! pop-up when the character is near an interactable objects?

    • @BMoDev
      @BMoDev  3 ปีที่แล้ว

      In this video my setup was I had the "!" sprite set as a child GameObject to my player (defaulted to inactive). I gave my interactables a CircleCollider2D trigger, and when the player enters this it sets the "!" GameObject to active.

  • @Jourlik
    @Jourlik 2 ปีที่แล้ว

    Thanks a lot man. Btw i have a question, do these colliders have an effect on the game's performance? Would it be better to have every object to detect the player and an input, or is it better for the player to have a collider that looks for objects?

    • @BMoDev
      @BMoDev  2 ปีที่แล้ว +1

      Well even if your player drives collision detection the objects still need some collider as well for the player to detect if they're close enough to interact with...
      To answer you from a more general standpoint, collision can get pretty computationally expensive, but it really depends... If you're targeting mobile or something where performance is stricter than say PC or if you have thousands of objects with colliders then it'd be worth it to take the approach to minimize that... but if you have like 20-30 colliders in a scene its really not something to get hung up on

    • @Jourlik
      @Jourlik 2 ปีที่แล้ว

      @@BMoDev thanks man

  • @shiwu5466
    @shiwu5466 2 ปีที่แล้ว

    Thanks but u missed the BoxCollider2D for the player..

  • @lunaris356
    @lunaris356 2 ปีที่แล้ว

    Hey BMo Thanks so much for the tutorial! I have a shop UI that pops up when I interact with an npc but I want it to close when I either press E again, or when I leave the range. I'm a little lost on this and was hoping for some help. Thanks so much in advance!

    • @BMoDev
      @BMoDev  2 ปีที่แล้ว

      Checkout my gameover or pause menu videos, similar concept!

    • @lunaris356
      @lunaris356 2 ปีที่แล้ว

      @@BMoDev Cool, Will do! Thank you!

  • @Asdzxc120
    @Asdzxc120 3 ปีที่แล้ว

    can you make a video on how did you make the ninja move with animation haha i really need it

  • @jakeduncan2721
    @jakeduncan2721 4 ปีที่แล้ว

    Hey BMo sweet vid - Know any open source 3d sprites / libraries to mess around with some simple simulations? I'd love to see that

    • @BMoDev
      @BMoDev  4 ปีที่แล้ว +1

      Hmm, what kind of simulations? There are alot of libraries out there (unity asset store would be my first look)

  • @alivgalihpratama5126
    @alivgalihpratama5126 2 ปีที่แล้ว

    how i can get animator chest assets?

    • @BMoDev
      @BMoDev  2 ปีที่แล้ว

      I made them myself in photoshop, tbh I'm not sure if I even have them anymore

  • @drioko
    @drioko 3 ปีที่แล้ว +1

    "keeping things simple"

  • @kiddowhy1105
    @kiddowhy1105 4 ปีที่แล้ว

    sweet

  • @plasteelgaming
    @plasteelgaming 5 หลายเดือนก่อน

    Tried following this in 2024 but there is no Interact Key (2:10) anymore so i think this guide is outdated.

  • @scolate_
    @scolate_ ปีที่แล้ว

    maybe, just you could use interfaces, i would do it that way

  • @neisanland2503
    @neisanland2503 2 ปีที่แล้ว

    it would've been nice if you also explained how to close the chest box.
    ever single time we ask how to open boxes we also want to know how to close.
    or how to drive a car forward we would definitely want to learn how to make it go backwards.
    or when i pick something up and carry it around, would also want to know how to drop it.
    common sense dude.
    pisses me off that i have to go around and figure out how to make isOpen false.

  • @ReyBanYAHUAH
    @ReyBanYAHUAH 3 ปีที่แล้ว

    Always remember to repent of your sins (sin is transgression of YAHUAH’S LAW: Genesis, Exodus, Leviticus, Numbers, & Deuteronomy) And Have Belief On YAHUSHA HAMASHYACH. HE Died and Rose Again On The Third Day So that you can be forgiven of your sins!
    HE Loves you! Come to HIM! :) :) :) :) :) :) :) :)

  • @Tide_Wolfia
    @Tide_Wolfia 4 ปีที่แล้ว

    so I am trying to make it run an animation but it is looping at the very beginning how would I fix this?

    • @BMoDev
      @BMoDev  4 ปีที่แล้ว

      I think its tough to say without knowing your project, but my guess is you are repeatedly triggering the animation to play, so it just keeps restarting.