Collisions and Triggers ("OnTriggerEnter not working!" - Unity Tutorial)

Поділитися
Вставка
  • Опубліковано 20 жов 2024

КОМЕНТАРІ • 427

  • @olotolow
    @olotolow 3 роки тому +46

    After one hour of tears and headsmahes i found this video and the title saved my life... i didnt use 2D... Even tho i used 2D components hundrets of times in the past... Its 4 AM now and i can peacefully go to sleep, tyvm.

    • @data_corrupted
      @data_corrupted 3 роки тому

      aaand you just saved me

    • @foxfirefive
      @foxfirefive 3 роки тому

      @@data_corrupted i think you might need to fix your data.

  • @friedrichhieronymus8786
    @friedrichhieronymus8786 4 роки тому +50

    I almost quit my first game after 3 days of tweaking the code, turns out it only needed a rigidbody, damn, I owe you one a big thanks

    • @junejuly532
      @junejuly532 4 роки тому

      If you learnt Physics and mathematics well in High school, You will know the engine better.

    • @RomainDelmaire
      @RomainDelmaire 4 роки тому +9

      @@junejuly532 Don't shame someone on what they didn't learn. Encourage them for what they are learning.

    • @junejuly532
      @junejuly532 4 роки тому +4

      @@RomainDelmaire I'm sorry if my comment meant shaming. it was not the intention. I soon realized how important Physics and Maths are to Game development.

    • @nathanmcconnell9991
      @nathanmcconnell9991 4 роки тому +6

      @@junejuly532 Knowing physics and knowing you needed a RigidBody in Unity are not the same thing.

    • @junejuly532
      @junejuly532 4 роки тому +1

      @@nathanmcconnell9991 In physics, you learn about Rigidbody. Some of the requirements when you go to uni to learn game programing is Physics and maths.

  • @Kewie1337
    @Kewie1337 2 роки тому +22

    I needed this so badly for a long time. I am so happy to finally discover your channel as it is truly inspiring work :) Thank you for everything you have contributed to the unity community Code Monkey!

  • @hardgames4104
    @hardgames4104 4 роки тому +7

    This is a really good tutorial, because it does not only show one way to do something and is over, but it covers several ways how to do and how not to do the topic the video is about. This makes things easier to get put together inside the brain of someone watching. Good work.

  • @freemoncodes
    @freemoncodes Рік тому +1

    My man, I spent like 45 minutes trying to figure this out. Was missing the all important 2D at the end. Thanks much!

  • @joaocarvalho7189
    @joaocarvalho7189 4 роки тому +1

    I searched on many videos and websites, and now I finally found how to make the player not pass through the walls
    ,Thank you!

  • @croeusjawad9744
    @croeusjawad9744 3 роки тому

    I made a project and the walls were not working, then I permanently deleted the project and made a new one and I felt like to quit on this idea, but then you cam and saved me from rewriting 567 lines of code.
    love u
    Thanks :D

  • @abdessamadcherrat5275
    @abdessamadcherrat5275 2 роки тому +11

    Some call him The Next Brackeys
    Others call him a Legend
    I call him "The man that the saved me from starting a new project by ACTUALLY making a good Tutorial about Collisions "

  • @ZeroIdeas9000
    @ZeroIdeas9000 9 місяців тому +1

    Thank you so much, I have been working on my first game ever. Even though it is a 3d game this finally got a object to detect the thing- THIS SAVED ME SO MANY HOURS.

  • @wiltongarcia4864
    @wiltongarcia4864 4 роки тому +1

    Your channel is very good, your English is very clear and the speed that you speak is very nice, helpful for those non-natives

  • @loklo
    @loklo 3 роки тому

    2 years later and still saving lives, great tutorial saved me a lot of time :)

  • @breadofgodd
    @breadofgodd 4 роки тому +2

    I was going mad trying to get OnTriggerEnter to work, you're awesome!

    • @elbreadroh
      @elbreadroh 3 роки тому +1

      I see a fellow Bread, alliance??

  • @AraragiAriel
    @AraragiAriel 2 роки тому +2

    I was looking to learn more about collisions, but what ended up really helping me on the video was discovering the Rigidboy2D.MovePosition function, which made me found out the solution to my player movement. I was doing it using the transform before, which wasn't working properly when moving towards terrain

  • @spartanpenguin3351
    @spartanpenguin3351 4 роки тому +4

    OHH! THANK YOU!
    my mistake is that I was actually using OnCollisionEnter insted of OnTriggerEnter! never notice this till now =-=

    • @iamyourfather6941
      @iamyourfather6941 4 роки тому +1

      mine is OntriggerEnter instead of OnTriggerEnter... been spending hours for a spelling mistake T _ T

    • @spartanpenguin3351
      @spartanpenguin3351 4 роки тому +1

      @@iamyourfather6941 haha! that's C# for yah

  • @CodesnippetBiz
    @CodesnippetBiz 4 роки тому +5

    very clear explanation, thank you so much, now I can fully understand differences between these kind of collisions!

  • @swedstonetv322
    @swedstonetv322 2 роки тому +1

    Thank you :) After 5 hours of finding the problem I've finally fixed it

  • @diegocarrionportilla8718
    @diegocarrionportilla8718 3 роки тому +2

    I f***ing love you man, you have saved me

  • @canerozdemir4195
    @canerozdemir4195 4 роки тому +15

    I am a simple man, I see Code Monkey and I like it.

  • @illuminith6405
    @illuminith6405 3 роки тому

    Thank you for the help. I was struggling a lot to make the collider work but now it works. I am new to unity and C# and you are of great help.

  • @DARK_AMBIGUOUS
    @DARK_AMBIGUOUS 3 роки тому +2

    Thank you, my game really coming along, I just added split screen multiplayer

  • @abrahamliz2559
    @abrahamliz2559 4 роки тому

    I was going crazy... could not get a state change inside the trigger. Watching the debugs you used solved my problem lol!! Many thanks!!

  • @anirbankarmakar2037
    @anirbankarmakar2037 4 роки тому +15

    Awesome video ,I really understand the concepts of collisions better now.
    Can you also make more on 3d development that would be really helpful
    Thanks,love your videos

  • @noether9447
    @noether9447 4 роки тому +1

    Found you through the ads and really liking your videos. Subscribed

  • @Gurseerat_Singh
    @Gurseerat_Singh 3 роки тому +1

    Hay code monkey bro i frickin love you omg you saved me actully i needed the function on collision exit and i was not knowing that this finction even exists bro i was almost going to quit my game but you saved it bro i am sooo frickin glad i found your vid

  • @subzero4787
    @subzero4787 4 роки тому

    Thanks bro it was helpful... First I thought my script was wrong and deleted it and wrote again and again deleted... But now it's working

  • @thefakejonfischer
    @thefakejonfischer 3 роки тому

    Thank you so much! I have been trying to make a title screen, and the trigger wasn't working. I found this video 30 minutes after midnight, and it allowed me to go to bed.

  • @aminmk697
    @aminmk697 3 роки тому

    bruv I was giving up making a (TOP DOWN DODGER) game because of the rigidbody :)
    I just subbed to the game dev god

  • @pholux5647
    @pholux5647 2 роки тому +1

    Thanks Code Monkey You Really Helped Me A Lot!

  • @redfoxlightning
    @redfoxlightning 3 роки тому

    THANK YOU turns out i just needed 2D at the end. thanks lots

  • @areion819
    @areion819 2 роки тому

    Thank you for this video, I was stuck wondering why my collisions wouldn't work.

  • @namenight7158
    @namenight7158 4 роки тому +1

    Helped me a lot! Looking forward for your game on steam!

  • @bbuddyyeapp3557
    @bbuddyyeapp3557 3 роки тому

    Thank you so much!!! I was finding the way that can solve the problem which is first explained in your video! Thank you!!

  • @swepo5719
    @swepo5719 Рік тому

    o m g, i freaking love you, i have been looking for solution for so long and no one says about rigidbody2d XDDD

  • @utilityy
    @utilityy 3 роки тому +3

    You are actually so cool. Thank you so much for all of these amazing tutorials you are producing. :)

  • @IsaacJairi
    @IsaacJairi 4 роки тому +2

    THANKSSSSSSSSSSSSSS MAN YOU JUST SAVE ME, I WAS 6h ON THIS

  • @alvinmanoj70
    @alvinmanoj70 3 роки тому

    if I had 3 wishes, my first wish will that you will have a million subscribers

  • @ProfessorNova42
    @ProfessorNova42 4 роки тому

    THANK YOU!! It got stuck at this problem for over an hour.

  • @Ranoluq
    @Ranoluq Рік тому

    Bro thank you, i so confused when i making, but you explain this. Thank You

  • @TechGuyDane
    @TechGuyDane 2 роки тому

    This was very helpful. You're the 🐐

  • @DridgeDEU
    @DridgeDEU 5 місяців тому

    Amazing, solved my problem super quickly. Love you!

  • @dhankumari7290
    @dhankumari7290 4 роки тому +1

    Your teaching ability is quite exceptional 👌👌👌👌👌👌👌
    I really love it💖💖💖💖💖
    Thanks as always uploading Such information viedo 🙏🙏🙏🙏🙏🙏

  • @samuellucaslimasoares9010
    @samuellucaslimasoares9010 2 роки тому

    Thanks very much! This tip saved my work, congratulations!

  • @nathaliaperes1392
    @nathaliaperes1392 4 роки тому +2

    This was really really helpful, thank you very much!!

  • @Fiedroz
    @Fiedroz 4 роки тому +1

    Nice! I was using a 3d collider for 2d game, thanks for helping! :)

  • @gameovercom
    @gameovercom 4 роки тому +1

    Man, I had the same problem, I was trying to play an animation trigger with just the collider, when I tried this, I almost make you a coding religion, haha.
    Thanks man.

  • @crazyone3494
    @crazyone3494 Рік тому

    OnVoidStay2D
    That line of code saved me! I was trying to make an attack for my character, it is a sword slash, due the circunstances i had to use the Polygon collider as a trigger, then it was simple, i would press the button, if it detected an object with the enemy tag in range it would play the damage animation while removing a life point. The problem was: i didn´t read the whole Unity manual so the only OnTrigger/OnCollider i knew was OnTriggerEnter, from here you can guess the problem. Really stupid of my part....

  • @James-zp5ly
    @James-zp5ly 3 роки тому +1

    Hello, great video but i didn't manage to solve my problem, was wondering if i could have some of your time.
    Currently, my player has the following components: transform, renderer, animator (custom script), rigid body 2D, box collider 2D as well as a movement script. The tilemap i want the player to interact with has: tilemap, a renderer, tilemap collider 2D. All these components have their default settings (eg: the player is a dynamic object not kinematic), and my problem is very odd, my player can walk across the floor, will 'hit' the ceiling and not go any higher, but for some reason can pass through the tiles left to right; so my problem is that the x axis collisions are working (as i can walk on them), but my y axis doesn't seem to (as my player can pass through them when walking left to right).
    Thank you for any help,
    James

  • @makemoneyonlinewithjannie
    @makemoneyonlinewithjannie 3 роки тому +2

    Thanks for this, I needed this tutorial today, I was doing colliders and triggers were set up perfectly, the only difference was my Player never had a rigidbody, so weird that it needs that.

  • @harneetsingh9420
    @harneetsingh9420 Рік тому

    Life saver always, Thanks Hugo :)

  • @Motyy
    @Motyy 8 місяців тому

    THANK GOD IT WORKS YOU ARE A GENIUS

  • @serialchiller9183
    @serialchiller9183 4 роки тому +4

    Amazing as always.

  • @dwlg
    @dwlg 2 роки тому

    Code Monkey saves the day again! woop thx man!

  • @Miki-gd4rq
    @Miki-gd4rq Рік тому

    Very helpful and great explanation, thanks man

  • @hhhl3555
    @hhhl3555 3 роки тому

    Ты легенда. Спасибо за детство

  • @footballgirl48
    @footballgirl48 Місяць тому

    Thank you so much for this video !! Really helpful!!

  • @itz_ak4085
    @itz_ak4085 2 роки тому

    This is very helpful, I Subscribed :)

  • @GustavoPLima
    @GustavoPLima 3 роки тому

    you save me again! thanks from brazil

  • @rodrigogriner9904
    @rodrigogriner9904 Рік тому

    Nice!
    You saved my team

  • @Excess-qn7qh
    @Excess-qn7qh 2 роки тому

    you make my day ... thank you !!!

  • @neozoid7009
    @neozoid7009 2 роки тому

    Love your Tutorials . Awesome and simple explanations

  • @KhaledHakim
    @KhaledHakim 4 роки тому

    Keep up the great work buddy. I wish you success and more of it.

  • @sasakiluna
    @sasakiluna 4 роки тому +3

    the trigger still doesn't respond in the debug code for me

  • @mynameisDuck
    @mynameisDuck 3 роки тому

    Dude I had everything right except I was using OnTriggerEnter instead of OnTriggerEnter2D! Thanks!

  • @cate01a
    @cate01a 2 роки тому

    cheers! my issue was using OnTriggerEnter instead of OnTriggerEnter2D :)

  • @k_dash99
    @k_dash99 4 роки тому +2

    Great video, thanks . I wasnt getting any output from the OnTriggerExit function due to I was testing collision vs collsion2d

  • @xXC0rn3rXx
    @xXC0rn3rXx 4 роки тому +2

    Really cool video, but I have a problem. If I want one object to collide with something and also to be trigger to something, how can I achieve this? (for example a projectile that bounces off the walls but effects the player as trigger)

  • @stakes_z
    @stakes_z 2 роки тому +1

    Love your videos. They help me a lot.
    I'm having a problem now and I dont know how to find a solution. I need to detect areas in a sphere with mouse click, like a globe. Do you know how I can do that?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  2 роки тому +1

      You can use Physics.OverlapSphere(); or Physics2D.OverlapCircle();
      It will find all objects will colliders within range

    • @stakes_z
      @stakes_z 2 роки тому

      @@CodeMonkeyUnity Oh nice! I'll check that. Thank you! So what I need to do is include a collider for each region I want to detect on the sphere and put all as sphere children?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  2 роки тому +1

      ​@@stakes_z Yes, if you have your globe with separate mesh parts that you want to select, just add a Mesh collider to each part. Then when when you click you can just do a raycast if you want to test a single point ua-cam.com/video/0jTPKz3ga4w/v-deo.html
      Or use Physics.OverlapSphere if you want to test with a bit of range

    • @stakes_z
      @stakes_z 2 роки тому

      @@CodeMonkeyUnity Awesome! Thank you so much! I'll create my globe with multiple meshes and try it. Thank you

    • @stakes_z
      @stakes_z 2 роки тому

      Hi. I would just like to say that it worked. I took meshes for each region of the map, put a mesh collider on each one and used your tutorial on clicking 3d objects as a base. Thank you very much! :D @Code Monkey

  • @sabeelm9
    @sabeelm9 9 місяців тому

    Nice, I found the issue with mine being that I was using the 3d collision and not 2d! Only figured out when I copied your code out by hand lol

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  9 місяців тому

      Yup I've made that mistake myself several times when I was a beginner

  • @christo-j5507
    @christo-j5507 4 роки тому

    Great work and you're so excellent! Helped me a lot.

  • @bananaramaman9031
    @bananaramaman9031 2 роки тому

    Thank you for this video!

  • @Steve-gi2yj
    @Steve-gi2yj 2 роки тому

    ok I answered my question, but the problem still remains, when I put my character from Kinematic to Dynamic on the rigidbody2d, it starts to fall horizontally, and my up button on the joystick doesnt work, like gravity is against it... i set the mass to 1, but still falls, i copied everything i could on your video, but still keeps falling, what else should i check for?

  • @madbitscience6230
    @madbitscience6230 3 роки тому

    Great tutorial! Thank you so much!

  • @KerasinafLega
    @KerasinafLega 4 місяці тому

    Hi! thanks for the vid, its awesome and really helped me on undersatding colissions better! i have a question, if i want a multiple triggers to cause a different events how can i do that?

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 місяці тому +1

      Make multiple separate game objects each with its own trigger and script

  • @baisk7105
    @baisk7105 4 роки тому

    you literlly safed my life

  • @jmiller6224
    @jmiller6224 3 роки тому

    So helpful, thank you as always!

  • @fallingstar_Yas
    @fallingstar_Yas 2 роки тому

    So for some reason, none of the triggers or collision code work. Even when there there a rigidbody. it just doesn't detect that the player and the object is touching. like the player cant go through the item but it not sensing that theyre touching

  • @collosalcake236
    @collosalcake236 2 роки тому

    your typing speed is faster than light omg

  • @well5832
    @well5832 4 роки тому

    increible Thanks CodeMonkey:D

  • @d3zar
    @d3zar 3 роки тому

    uhmm, i have a polygon collider. I'm trying on triggerstay2D event for this. But uh If i wait with dummy mode ( so doing nothing ) in my polygon collider it's working for 2 seconds. But after debug.log is not logging while when i out from dummy mode. Why it's stopping working .? And how can i do like your heal circle in this video.

  • @sleepingcolossus
    @sleepingcolossus 5 місяців тому

    I always forget "2D" at the end of the method name! Thanks!!!

  • @stepaburianek8994
    @stepaburianek8994 3 роки тому

    I WAS SEARCHING FOR THIS FOR HOURS YOU SAVED MY COMPUTER FROM ME XDD

  • @TechnoMichaSDL
    @TechnoMichaSDL 3 роки тому

    i have a 2D Player Object with a Child 2D Object but i want to use the Child Object as Collision detector but how?

  • @omidnajmi6019
    @omidnajmi6019 Рік тому

    thank u man, u solved my problem

  • @jamaass
    @jamaass 4 роки тому

    Thank you! You saved my life!

  • @rollingstonesonroad
    @rollingstonesonroad 4 роки тому

    Hello. I am trying to detect on trigger condition with prefabs. But the problem is it is detecting the collision with only the first prefab out of the spawning prefabs. How to rectify this?

  • @phin-7261
    @phin-7261 Рік тому

    very helpful. just one problem that light bright coding screen is killing my eyes.

  • @burakyigitgulsun3833
    @burakyigitgulsun3833 Рік тому

    I'm using ontriggerenter 2 and ontriggerexit2d to check if my character is inside a cave. but since my character's collider is smaller than the collider of the cave, the oncolliderexit2d command works as soon as I enter the door as if I went out of the cave, how can I fix this?

  • @dansv9778
    @dansv9778 4 роки тому +1

    well i have rigidbody and doesn't work. I made lots of functions in the scene with triggerenter and they work...the collision layer are all checked

    • @BeardaneLope
      @BeardaneLope 4 роки тому

      Dan SV I have the same problem

  • @bearerofcurses
    @bearerofcurses 7 місяців тому

    hey there great video, im having a problem with my player starting to rotate when i collide with my chest

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  7 місяців тому

      On the Rigidbody you can set it to fix rotation

  • @DWIWindsands
    @DWIWindsands 2 роки тому

    Nice I can use that on my bullet & it won't stick to the wall or I can not & make it stick to the wall TY.

  • @RC-hi5sj
    @RC-hi5sj 4 роки тому

    Very informative thanks

  • @DataversePH
    @DataversePH 3 роки тому

    Happy for everyone that makes their game works. Unfortunately, mine is now working. I have a rigidbody2d, boxcollider2d for my player, and a gameobject which has a boxcollider for the trigger. And also in my script, I used OnTriggerEnter2d. But it still doesn't work. Send help please.

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  3 роки тому

      Your gameobject needs a BoxCollider2D, not BoxCollider

  • @sanskarkhunt
    @sanskarkhunt 4 роки тому

    Thanks code monkey for helping us..

  • @wolfenergy4219
    @wolfenergy4219 4 роки тому

    OMG thanks for this video my only problem was the little "2D" you need to add after OnTriggerEnter and the collider

    • @GumbazCulumbaz
      @GumbazCulumbaz 3 роки тому

      Same here

    • @yatogami2159
      @yatogami2159 2 роки тому

      Shitttt i didn't realised there was a 2d trigger tooo thank u mannnn 😭 u saved mee i didn't even noticed in video damnnn thnxxxx

  • @loya5377
    @loya5377 3 роки тому

    Hi! Please help me! How can I determine the coordinates of the vertices of the Box Collider? If the object's rotation is not (0, 0, 0), then collider.bounds doesn't help.

  • @watcher2747
    @watcher2747 4 роки тому +1

    For some reason it's still not working for me, any ideas what could be the possible reasons?

    • @quentinyahia352
      @quentinyahia352 4 роки тому

      Hi, i get the same issue and i add a rigidbody to the two object (put on static on the trigger object and dynamic to the player) and it works, hope it was useful

  • @Steve-gi2yj
    @Steve-gi2yj 2 роки тому

    thank you for the tutorial, do I need to use RigidBody2d to make it stop against walls? right now i am using a joystick prefab, and in the code using: player.transform.position, and I set an object, just a black cube, with only box collider 2d, and the player still goes through it... so again, is it because I am using transform.position instead of Rigidbody2d? I set everything you did at the 5:51 video mark.... but not working.

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  2 роки тому

      The rigidbody by itself has no shape, the shape is on the collider. So just a rigidbody will move through walls
      If you manually set the transform.posiiton then you're overriding the rigidbody logic (unless you make it kinematic but then you have to handle collisions yourself)

  • @GameDevCaptainBlack
    @GameDevCaptainBlack 11 місяців тому

    Thank you King

  • @STFUC
    @STFUC 4 роки тому +1

    Great video on a topic i've been working on; question on something you didn't cover.
    How can i make my pickup/trigger (in your case a health potion) only interact with 'player'? ie if you push a rock into the health potion the rock would activate the trigger.
    I've tried a lot of combinations of playertag = 'player' (not written like that ofc) but still can't get it to only trigger when the player character enters (i have a lot of debris flying around).
    Thanks!

    • @CodeMonkeyUnity
      @CodeMonkeyUnity  4 роки тому +1

      In the script where you're testing for the collision you have a reference to that object and whatever other object collided with it. In terms of identifying there are many ways you can do it, if the player has a Player class attached to it then doing (otherCollider.GetComponent() != null) would be the simplest.

    • @STFUC
      @STFUC 4 роки тому

      @@CodeMonkeyUnity Thanks! I'll give that a shot!

  • @Piyush-x3s
    @Piyush-x3s 3 роки тому +1

    Thanks !