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
Thank you so much! I haven't used Unity Events much before but they're perfect for things like this. Well explained too X)
Dude you just saved my ass, keep on going you're great, YT need more tutorials like yours
Thanks, I really appreciate hearing that!
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!
Wow, this just ease a lot the script. My codes were loaded of GetComponents because i didn't know how to use this function
THE BEST tutorial on youtube for unity interactions! ty!
This helped me clean up my old door system to something way better and more flexible. Thank you
Came for the title, stayed for the duck.
Thanks for this Awesome Tutorial it helped me immensely with my project that I'm building atm only for learning by doing
I feel like I've struck gold with this video. Amazing tutorial!
Again another dope tutorial, it clarified a lot how Unity Events work, thanks!
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
Great video. Good content.
You deserve alot more subs.
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?
did you have using UnityEngine.Events; at the top of your script?
@@bensmart2829 thanks yes I did I figured it out but thanks for the help :)
@@bensmart2829 Fixed that problem for me, but it still has an issue where it does not recognize "KeyCode"
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?
really good explanation! thank you
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?)"
Thank you so much you saved my game
Thanks, but you put in the work! 🙏 Good luck, would love to see your game!
I watched your dialogue box tutorial as well, is there a way to combine it so that it plays the script with a interaction?
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...
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?
The video was very helpful, thank you.
Glad I could help!
Thank you very much you helped me a lot
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?
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...
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.
I am so happy for finding an awsome channal 😍
The feeling is mutual!
Oh wow thank you so much!!
When I press check in the "Is open" for the chest my chest animation does not work. Any solutions?
Did you solve it?
What did you did in the animator from the chest?
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.
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! :)
@@BMoDev Can you explain more, like i need to know
Awsome video man!
Thank you, friend
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? :(
very good tutorial. thank you
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
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
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.
This is awesome!
This is awesome thank you!!
You're awesome, hope it helps!
My version of Unity 3.6.0 doesn’t recognise UnityEvent as a variable type, has it changed?
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
@@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.
Was that like a giant yoyo?? (Duncan yoyo). Thanks for the info btw :)
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
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.
You misspelled UnityEngine
You put "UnityEnginge"
Very nice! When we want to close the chest? How would we do it :D
This helped so much xD Thank youuuu! Also is it possible to change the Interact keycode within another event?
Awesome! Yeah definitely, you'd just have to change the variable Keycode
Where's the chest controller script video?
What must i do if i want this for 3d project work?
You are a god.
Input.GetKeyDown doesn't work...Unity says Imput doesn't have GetKeyDown
how to close that door?
What about 3d game? do i still use 2dcollider?
amazing tutorial
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?
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.
I don't get how the chest changes to being open when executed, is there something that I missed?
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.
When I used the UnityEvent it gave me an error
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.
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.
@@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..
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 :)
OMG Thank u very much!
Np!
How do you create the ! pop-up when the character is near an interactable objects?
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.
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?
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
@@BMoDev thanks man
Thanks but u missed the BoxCollider2D for the player..
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!
Checkout my gameover or pause menu videos, similar concept!
@@BMoDev Cool, Will do! Thank you!
can you make a video on how did you make the ninja move with animation haha i really need it
Hey BMo sweet vid - Know any open source 3d sprites / libraries to mess around with some simple simulations? I'd love to see that
Hmm, what kind of simulations? There are alot of libraries out there (unity asset store would be my first look)
how i can get animator chest assets?
I made them myself in photoshop, tbh I'm not sure if I even have them anymore
"keeping things simple"
sweet
Tried following this in 2024 but there is no Interact Key (2:10) anymore so i think this guide is outdated.
maybe, just you could use interfaces, i would do it that way
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.
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! :) :) :) :) :) :) :) :)
so I am trying to make it run an animation but it is looping at the very beginning how would I fix this?
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.