A Better Way To Manage Collision in Unity (For Beginners)

A Better Way To Manage Collision in Unity (For Beginners)

Lost Relic Games

3 года назад

60,220 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@bradleywood1984
@bradleywood1984 - 21.12.2023 17:49

I've only just recently began my game development journey after 20 years in a technical space concerning metrology. I find your videos to be down to Earth and easy to grasp. I love the simplicity here with centralizing the processing of collisions. Good work. Can't wait for Blood & Mead!

Ответить
@laserbean00001
@laserbean00001 - 26.09.2023 13:54

I don't understand why you would need to write them twice. Most objects are either trigger or not trigger. Not trigger colliders will never call the OnCollisionEnter function. I guess it could be useful for an arcade type game where you don't necessarily want to put scripts on every spike object.

Ответить
@MaxIzrin
@MaxIzrin - 17.04.2023 11:36

The reason you don't see such code used much is because you have a very specific use-case where collision and trigger do the same thing.
It's neat code, but not scalable. Do not do this.
What if a platform (trigger area) heals the player or gives some temporary buff/debuff?
You need to add that logic into player as well... that can get very messy very quickly.
That's why I usually see people place the logic in the object initiating the action, and not the entity being affected by it.
Spikes will damage entities that collide with them, instead of entities having to handle damage when touching spikes.
And instead of using tags, use interfaces to detect something that can be damaged, i.e: collision.GetComponent<IDamagable>() or something like that.

Ответить
@cinfoni8698
@cinfoni8698 - 16.04.2023 06:05

im so shit at codig i cant make this work, the chacater collides and nothing happens why the heck did i chose to study this im screwed

Ответить
@josekarnikowski1711
@josekarnikowski1711 - 08.02.2023 02:03

Really nice video.

Ответить
@draveeeen5475
@draveeeen5475 - 24.11.2022 01:38

what is gameObject reffering to? i assume to the object that has this script as a component, aka the player in this case.

Ответить
@flamingpenny
@flamingpenny - 23.11.2022 21:48

If you do this, and your collider is on a subobject, the OnCollisionEnter2D() will get the gameobject from the parent. The OnTriggerEnter2D() will get the gameobject that the collider is attached too.
To fix this do OnTriggerEnter2D() => ProcessCollision(collision.gameObject) and OnCollisionEnter2D() => ProcessCollision(collision.collider.gameObject)

Ответить
@dukewendel
@dukewendel - 21.10.2022 06:33

The next step would be to move the collision detection to its own script and use events, that way you separate the logic from the callback making it even cleaner :)

Ответить
@pndaedits2364
@pndaedits2364 - 04.10.2022 22:27

Ответить
@aminullah5939
@aminullah5939 - 20.09.2022 09:29

sir you r awsome

Ответить
@Zegamis
@Zegamis - 21.07.2022 15:12

Awesome Video. Thanks for the help.

Ответить
@floppitommi123
@floppitommi123 - 20.07.2022 01:19

THIS IS PERFECTT I didnt know trigger so i didnt know what to doasjlk;dfajsl;dfsafdsa TYYY

Ответить
@francoismentec4260
@francoismentec4260 - 11.07.2022 18:12

Wow, making a function for code that occurs in multiple places... Who would have thought of that...
Seriously when you put beginner and unity in the same title I think that as a unity beginner I am going to learn something.
Stop making Unity tutorial that are actually coding tutorial, I am watching Unity tutorial to learn about the game engine not basic programming stuff. There is nothing wrong about making coding tutorial, but phrasing it as if you were going to teach us anything about collisions in Unity is wrong.

Ответить
@JoaoNeto-up8zi
@JoaoNeto-up8zi - 10.07.2022 04:22

You deserve a prize for this, that's some sharp headspace saving simplification, well done man

Ответить
@TheFRBublitz
@TheFRBublitz - 03.07.2022 20:33

glad to see that zlatan is exploring his talent in game development aswell. He was pretty good playing football

Ответить
@user-ve4qx2fw5d
@user-ve4qx2fw5d - 01.07.2022 16:18

thank you it is arbic

Ответить
@animationsbelike
@animationsbelike - 19.05.2022 21:46

ÓMG thank you! :DDDD

Ответить
@limqueenyt
@limqueenyt - 12.05.2022 17:56

Thank you very muchhh!! This totally solve my questions!

Ответить
@CzechHustler
@CzechHustler - 03.04.2022 16:55

Ty jsi Čech že? :D

Ответить
@gabrielsouza9651
@gabrielsouza9651 - 29.03.2022 07:21

Very interesting concept. This centralizing systems could help a lot in every situation in coding since almost every project needs a serie of collisions methods. Thanks it was a nice video!

Ответить
@Hersonrock12
@Hersonrock12 - 24.03.2022 05:17

Great tip! I saved it on my notes :)

Ответить
@art1cuno925
@art1cuno925 - 13.02.2022 21:42

thx for help

Ответить
@robertjunior5272
@robertjunior5272 - 08.02.2022 05:15

Very helpful and awesome

Ответить
@rotub
@rotub - 05.02.2022 12:08

Nice tip - thank you

Ответить
@marcomarmolejostech
@marcomarmolejostech - 01.02.2022 20:49

Maybe you haven't found it on another collision tutorial because what you are doing is related to a software design principle called DRY (Don't Repeat Yourself) combined with the refactoring method called "Extract Method".

Ответить
@wheresmyskin
@wheresmyskin - 31.12.2021 13:16

Not really "a better way to manage collisions" but like basics of code structuring and programming. "no code duplication" is like one of the most basic rules to programming.
"Yeah, few different things do the same action, let's wrap this action into a function and let each thing call the function" :D

Ответить
@oleksiistetsenko6028
@oleksiistetsenko6028 - 26.12.2021 17:42

i misheard "in this video i wanna shave with you" XD

p.s. Thank you for tutorial o/

Ответить
@izz3878
@izz3878 - 03.12.2021 00:47

Thank you for sharing your knowledge !

Ответить
@vast634
@vast634 - 21.11.2021 23:14

Thats one loud keyboard

Ответить
@xDTHECHEMISTx
@xDTHECHEMISTx - 17.11.2021 09:02

Do you have a video for this where it shows your health bar going down when you get damage? I got your tutorial here to work for me to see the get hit debug code but now im trying to incorporate this code with my health bar code that i currently have set to go down every time i hit my F key. i want that to be for when the enemy collides with my character but i keep getting a weird error

Ответить
@theHaPK
@theHaPK - 03.11.2021 20:31

Amazing! Does this technique work for colliders only or I can use it in other parts of the code?

Ответить
@OpenYoureyes304
@OpenYoureyes304 - 20.10.2021 13:19

so john wick is a game dev now huh?

Ответить
@dcry1003
@dcry1003 - 05.10.2021 10:07

how come your player isnt falling through the floor after triggerign the red spikes? in my project whenever i enter my portal collider (which transfers me to the next scene) no matter how i set the portal trigger i always fall of the ground if i didnt add an extra invisible box collider2D in my ground object with its own box collider2D

btw my portal trigger object is more bigger than my player object if that helps

Ответить
@derlinjesustaverasperalta1724
@derlinjesustaverasperalta1724 - 18.08.2021 21:00

Omg , 😲 knowledge acquired, wrote twice the code. Now i know thanks :)

Ответить
@divyampachkawade850
@divyampachkawade850 - 08.08.2021 08:13

Thank You Very Much You Video Helped Me A Lot

Ответить
@affliction1599
@affliction1599 - 03.08.2021 22:50

Hello I’m working on a 2D platformer I ah e a box collider on a moving sprite that’s lava but I want to register more than one collision how do I do this please PM me thanks

Ответить
@lynai8273
@lynai8273 - 12.07.2021 03:03

Do you have any idea to set a cooldown for collision damage? my player dies toooo fast when he collides with an enemy after collision checks

Ответить
@ManjeetKumar-xk8jf
@ManjeetKumar-xk8jf - 08.07.2021 10:27

I don't know man but you look like Jesus.

Ответить
@pliniomourao
@pliniomourao - 23.06.2021 18:35

This kind of enters in the "Single-responsibility principle" pattern.

Ответить
@moonneko4035
@moonneko4035 - 16.06.2021 09:21

If OnTriggerEnter and OnCollisionEnter have the same function, as showed in this video, why do you have to write both of them in 1 c# script?
Can you tell me the difference between them?

Ответить
@johnx140
@johnx140 - 15.06.2021 00:18

Isnt this just... making a function?

Ответить
@Chexmix6
@Chexmix6 - 09.06.2021 05:23

Thank you! I was having SO much trouble on how to check what collision is being detected, and I couldn't find anything to help me until I found this!

Ответить
@halivudestevez2
@halivudestevez2 - 02.06.2021 14:50

The difference between trigger and no-trigger : thank you for the tip - while it is so obvious, I missed the point so far...

Ответить
@kingdom216
@kingdom216 - 24.05.2021 05:21

I need to set a game object after colliding with a trigger, like a puck to a face off circle after colliding with a goal trigger and different face off circles after colliding with boundary triggers

Ответить
@BikramKha1sa
@BikramKha1sa - 12.05.2021 07:37

thank you brother ...

Ответить
@darkotomic943
@darkotomic943 - 01.04.2021 16:53

Even better, use Actions/Events

Ответить
@THEspindoctor84
@THEspindoctor84 - 15.03.2021 03:50

this is pretty straightforward, but for some reason I also have not implemented it! thanks for the tip!

Ответить
@AetherXIV
@AetherXIV - 11.03.2021 11:28

I'll use this. thanks man

Ответить
@karkeeravindran3649
@karkeeravindran3649 - 06.03.2021 15:48

Hello...
Pl help as soon as possible.

Tried a 2D game,
Find the difference game.

Touching d screen on d object it triggered and result correct, and if touched background image it triggers and results wrong.

Placed d pointer down.
If we touched the object, since the pointer moving towards we touched, it automatically triggers the bg first and then the object.


Results - first msg as wrong, immediately correct.

How to overcome this. Since bg image is 2dboxcollider even if we increase z axis, it collides .
Can we directly switch(jump) to the touched point without passing through the bg image area.

Ответить