Тёмный
No video :(

First Person Controller - Interaction (EP08) [Unity Tutorial] 

Comp-3 Interactive
Подписаться 16 тыс.
Просмотров 20 тыс.
50% 1

This is the big one! The one that everyone wants! INTERACTION AND INTERACTABLE ITEMS! In this video I'm going to be using raycasting to determine when the player is looking at an object that you can interact with, then they look away from that object and, obviously, interacting with the object it's self!
This one may be a little more advanced than other videos in the series but we'll get through this together!
Join me and learn your way through the Unity Game Engine, the C# language and the Visual Studio editor. Remember, if this video was useful then DROP A LIKE! 👍
💯 Want to help me out and allow me to keep making these tutorials? Consider supporting the channel on Patreon:
/ comp3interactive
🤩 SPONSORED LINKS:
www.gigatank30...
/ gigatank3000
Check out HEXED PERSPECTIVE:
Soundcloud: / hexmane
Spotify: open.spotify.c...
Instagram: www.instagram....
Apple Music: / hexmane
😍 AWESOME high quality 3D game assets available here:
shop.runemarks...
💬 Join the Discord community here:
/ discord
📱 Find us on social media for more Tips and Tricks:
/ comp3interactive
/ comp3interactive
/ comp3int
📱 Play our games for FREE here on Google Play:
bit.ly/2TisAQo
#UnityTutorial #Unity3D #Comp3interactive #GameDev

Опубликовано:

 

5 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 98   
@vishingad
@vishingad 2 года назад
For anyone having trouble triggering OnLoseFocus() when not looking at the intended object, you can try adding "&& hit.collider.gameObject.layer == 9" to first if statement of the HandleInteractionCheck() function. Seems like it has done the trick for me. Hope it helps. Have a nice day
@viniregoli
@viniregoli 2 года назад
Thanks dude...
@Slort_
@Slort_ Год назад
This was the exact solution I was looking for! Thanks!
@xyn6704
@xyn6704 Год назад
bless your soul that little thing was annoying me for such a long time, thank you
@DevKintara
@DevKintara 2 месяца назад
how to add that on the script? what part line /text
@JoaoPMFerreira
@JoaoPMFerreira 2 года назад
idk how no one has noticed this but the OnFocus OnLoseFocus events are firing multiple times, not only when youi first look at an object. The fix is really simple just replace currentInteractible.GetInstanceID() with currentInteractible.gameObject.GetInstanceID(). it was comparing the instance id's of 2 different objects, therefore never detecting you were always looking at the same one.
@WowNemy
@WowNemy Год назад
I knew something was wrong, but couldn't figure out what it was. Thank you!
@ovter
@ovter Год назад
Thank you dude, I waste 20 minutes to fix it❤
@phyg0544
@phyg0544 Год назад
Thank you very much. 🙂
@dremovremen1764
@dremovremen1764 Год назад
Thanks, that cleared the console a lot. Add. question: so in case with "currentInteractable.GetInstanceID()" the ray was getting the ID of the script, right? (Do scrpits even have an ID?) As the script is the same, it's ID is also the same, and thus the console clutter.
@blaqkstar
@blaqkstar Год назад
Awesome, thanks for that
@TSUKILORD
@TSUKILORD 2 года назад
2022 and I really wish this series to be appreciated by future generations much much much more than it was when it was created. It deserves thousands of likes for the quality of code and for the genuinely practical aspects of the code on FPS/FP Adventure games! I really wish that there were more videos in the series! Edited: Is there a place where we can find the whole code? I am not sure why I keep on receiving a NullReferenceException ... it has something to do with that interactionLayer at HandleInteractionInput method. I am not sure where is the problem though (...)
@natasilva2978
@natasilva2978 2 года назад
Many doubts I had about how to make various types of systems were resolved and I'm learning even more things here on your channel! Thank you very much for the tutorials my friend!!
@washynator
@washynator 3 года назад
Dude, you can't make too long videos! I'd watch you for hours on end!
@khurum
@khurum 11 месяцев назад
Why cant he??like code monkey did.
@LunarBulletDev
@LunarBulletDev 2 года назад
Another way to do it is having a long box collider from camera that turns on when you press key, and to detect if it collided with something of specific tag! The plus on this one is that you arent checking all the time with raycasts! (a lil tini wit more performant)
@monkeyrobotsinc.9875
@monkeyrobotsinc.9875 Год назад
actually thats really good but i dont think that would work with on focus stuff.
@pepethegood
@pepethegood 21 день назад
best guides out there
@theangrypotato2.031
@theangrypotato2.031 Год назад
I had a problem here that i thought was complex, that wound up being misplacing a "(", now this comment is only here to thank Comp-3 for their incredible tutorials, so far my only issue has been human error on my end, which is surprising for 8 tutorials in a row.
@monkeyrobotsinc.9875
@monkeyrobotsinc.9875 Год назад
thats F*cking amazing!
@elevenrus
@elevenrus 7 месяцев назад
Thank you, sir! Highly appreciate your work and the way you explain things. Your videos are never long.
@rxc-legamelab8134
@rxc-legamelab8134 3 года назад
You deserve way more views ! Thanks for your tutorials !
@comp3interactive
@comp3interactive 3 года назад
Kind word my man ♥ we'll get there soon enough!
@BbVortexMortinghan
@BbVortexMortinghan 3 года назад
These tutorials are super helpful man, keep it up. Thank you!
@alup4rk
@alup4rk 2 года назад
How can i prevent the console to update the interaction things like 999 times
@birnchengroup
@birnchengroup 2 года назад
You can say collaps :) the every "same Message" is only one line
@Shivyyy
@Shivyyy 3 года назад
Dude thank you so much for this! Seriously!
@angryparkranger
@angryparkranger 2 года назад
I seriously love your videos man, thank you so much!
@casd96
@casd96 2 года назад
Thanks for the tutorials! I've learned a lot from you! Can you please make a tutorial about footsteps?
@stefanbartolo
@stefanbartolo 3 года назад
This Channel is gold. For sure deserves a 100,000 subs plus
@comp3interactive
@comp3interactive 3 года назад
One day 🤞
@Abracadaniels
@Abracadaniels Год назад
Everything works except the interact part, nothing happens when i press my designated button.
@NewYorkNixDa
@NewYorkNixDa Год назад
Same here 😞
@andrewfrolov5144
@andrewfrolov5144 Год назад
@@NewYorkNixDa check your layers guys in code
@BrainSuspendedInFluid
@BrainSuspendedInFluid Год назад
Your videos are great Mike I'm learning a lot. Thank you
@kyleme9697
@kyleme9697 3 года назад
Interesting, the code I didn't know I needed. I can think of a couple uses for that Raycast Viewport thingy. Thanks !!
@nutsandy7183
@nutsandy7183 3 года назад
Looking forward to the next tutorials on this, at the end of doing these tuts im like Jhonny5 - "Need more input!" :)
@shang_psycho7414
@shang_psycho7414 Год назад
I added a circle in the center so that the player knows where to look and interact.
@schooliedee
@schooliedee 3 года назад
Nice video! For the part around 10:50, this check won't fire an OnLoseFocus call if you looked from one interactable object to another btw.
@Shivyyy
@Shivyyy 3 года назад
yea if you find a way to fix that do let me know please thanks
@lucasancelmo5191
@lucasancelmo5191 2 года назад
I think I managed to fix this, in the line 211 I added a layermask to the Raycast method. Because before it was looking at everything and then checking if the layer of the collided object was the layer 9, but if it's looking for everything, it will never reach the else statement.
@IlIlIIIIlIlIIlIll
@IlIlIIIIlIlIIlIll 2 года назад
here's the code: if(Physics.Raycast(playerCamera.ViewportPointToRay(interactionRayPoint), out RaycastHit hit, interactionDistance, LayerMask.GetMask("Interactable")))
@BrunoHiltune
@BrunoHiltune 2 года назад
Hi, did you ever manage to fix this? Ran into the same problem and it's pretty annoying EDIT: If anyone is still stuck at this, I've probably found a solution: I ended up redoing the script but used the same elements, so this should work: Right before this videos line 215, add the following check: if (currentInteractable != null) { currentInteractable.OnLoseFocus(); } This makes sure that the code runs OnLoseFocus if the previous conditions were met and a new object is being focused on. To be clear, I'm running a slightly modified script, but this should work for this as well. Test it out.
@CreticDev
@CreticDev 2 месяца назад
@@IlIlIIIIlIlIIlIll Im sorry but this code makes you interact with objects through the walls
@bmcg3712
@bmcg3712 Год назад
These tutorials have been incredibly helpful! But I've just now encountered an issue with this interactable thing. The ray seems to detect the interactable object just fine and everything goes as expected, provided the object is not close to another object. Need to set everything else to be on the layer "ignore raycast". Which, in retrospect, seems obvious.
@libberator5891
@libberator5891 3 года назад
Now that you made an Interactable class, you need to make a Comp3 class
@AlejandroArch22
@AlejandroArch22 2 года назад
I’m guessing for doors you just interact with the click or some other button and have it idle at no animation then play the rotation animation from pivot on click then on the second click have it close by playing the rotation starting from the end of the first animation. Have it check through a bool if it’s open or closed
@deadbroadcastpc
@deadbroadcastpc 2 года назад
Excellent explanations, look forward to more videos man
@lomlak6390
@lomlak6390 Год назад
what should i do if the message "LOOKING AT" pops up 600 times per second instead of popping up once
@andrewfrolov5144
@andrewfrolov5144 Год назад
where you have GetInstance after != simply add gameObject in between. Like so: currentInteractable.gameObject.GetInstanceID()
@hi-id2kn
@hi-id2kn 2 года назад
I wonder what's next after this 👑Different footsteps sound :-D👑
@stefanwuttig
@stefanwuttig 2 года назад
I have some issues with the OnLOseFocus() routine. It works fine if the player isn't to close to the object. but if he is to close to OnLoseFocus doesn't work. The OnFocus stops properly, but the OnLoseFocus doesn't start. AnyOne a Idea to fix this?
@roguetaver579
@roguetaver579 2 года назад
Great series! Thanks a lot
@PaulMartin-zc9oj
@PaulMartin-zc9oj Год назад
Love the series, alway learning something new. However, one should obide by the SOLID principles. None the less, cheers mate.
@Slort_
@Slort_ 2 года назад
So the LoseFocus function doesn't quite work or get called.. I kind of fixed it by adding the layermask to the first raycast, but it doesn't work if you look at an object and that object is destroyed, only if you look away from it.. Does anyone know a proper fix?
@user-uw7ys1vs7x
@user-uw7ys1vs7x Год назад
im pretty late to the party but im having trouble getting my cube to the interaction layer... everything works amazing but the script doesnt assign the layer for my object with the script... only after i assign the layer on the object manually does the script activate instead of having the script itself assign the layer automatically
@omeryyavuz
@omeryyavuz Год назад
same thing...
@ragnagna4276
@ragnagna4276 4 месяца назад
How are we suppose to attach the interactable script to a gameobject if it is abstract?
@fishgethirsty6377
@fishgethirsty6377 2 года назад
Awesome videos keep up the great work but it would be super cool for a video on first person animations
@Glados44
@Glados44 2 года назад
Mike can you make a double jump and crouch dash movements? . I been looking for a while but no one makes a good tutorial of this actions :/
@InvisaPotion
@InvisaPotion 2 года назад
Could you make a vid on how to slide/wall run?
@NeroBlanka
@NeroBlanka 2 года назад
Could you also, instead of using layers, use tags to denote objects that the player is able to interact with? Or is it better to just use layers?
@TerrorGerbleDoom
@TerrorGerbleDoom Год назад
Hello! I'm some one with very little coding experience and I'm trying to get better at recognizing issues but I'm kind of at loss after quadruple checking everything. I don't have any errors appearing in my project, but it doesn't seem to be performing correctly. 1. Looking at the cube sets off both the "LOOKING AT" and "STOPPED LOOKING AT" print in unison and they'll continually update as long as I look at the cube. They both stop when I look away. Interaction 2. My crouch is now glitching, if I uncrouch in just the right spot while partially under a collider, my character will glitch through the ground and fall endlessly to their doom. It's particular, but I'm able to recreate the glitch reliably. I didn't have this issue before implementing the interactions (though maybe I just didn't test well enough). EDIT: I also seem to get the crouch glitch from just spamming the crouch button. I assume the issue has to do with raycasting, though I'm not sure what or how to fix it. Any help would be appreciated!
@TirianMassot
@TirianMassot Год назад
Hey, have you found a solution for your first issue? I have the same one.
@akarsh9407
@akarsh9407 2 года назад
Have you planned anything like Running Slide its cool...
@SubjektDelta
@SubjektDelta 3 года назад
Why no interfaces?!
@comp3interactive
@comp3interactive 3 года назад
Because you can add default behaviour so abstract classes, like swapping out the cursor to show an object is interactable an vice versa
@renzellelaurenceapolinario7925
I have an issue, it seems that i can see through object that has a layer of Interactable or Has a Script of TestInteractable, anyone can help?
@Fosterushka
@Fosterushka 2 года назад
its my second week i was rewatch video 3 times and finally understand where is mistake. Its a hardcoded layer ? instead of number layer , we should use smth like name i mean we should compare with InrecationLayer the name of the object insted of layer number ? ama right ?
@pinhoug
@pinhoug Год назад
I have the Problem that if i look at any Object i cant walk forward to that object an the other problem is that if i look at an object that isnt interactable i get an error that the object is not set to a reference. I mean the last one doesnt realy impact the game realy but it would be nice if its not there. Can anyone help?
@reginaarbizu6017
@reginaarbizu6017 2 года назад
I can't link the public abstract class Interactable to the new TestInteractable script, it seem Visual Studio takes them as separate instance even they are save on the same folder. Is there a way to change that on Settings? Edited: I found the problem, in Unity - Preference I hadn't check Visual Studio as the External Script Editor.
@stevethepirate
@stevethepirate Год назад
it's hard to tell when a method/function is part of the unity Library or if it's a new method. any advice on that? i don't think people comment often in code whether the method is a Unity Method or if it's something you've made from scratch and doesn't exist anywhere else in code. does unity indicate that at all? like if you hover over the method or right click it or anything?
@jud.su.5developer895
@jud.su.5developer895 2 года назад
This work for pickup weapons?
@skymeyzz8431
@skymeyzz8431 2 года назад
You can make footsteps please !
@leoskinnertm
@leoskinnertm 3 года назад
is there any way you could upload this project to github so i don't have to go from episode 1?
@gustavlofqvist5507
@gustavlofqvist5507 2 года назад
Hello I'm getting a few errors. Mainly surrounding ViewportPointToRay. It says: "Transform does not contain a definition for ViewportPointToRay". OnFocus and OnInteract has the same error but with interactable not containing a definition. Please help.
@gustavlofqvist5507
@gustavlofqvist5507 2 года назад
Error code example: "Assets\Player\FirstPersonController.cs(166,41): error CS1061: 'Transform' does not contain a definition for 'ViewportPointToRay' and no accessible extension method 'ViewportPointToRay' accepting a first argument of type 'Transform' could be found (are you missing a using directive or an assembly reference?)"
@gustavlofqvist5507
@gustavlofqvist5507 2 года назад
Solved OnFocus and OnInteract but ViewportPointToRay is still giving me errors.
@comp3interactive
@comp3interactive 2 года назад
Because ViewportPointToRay is a camera class method, you're using the transform and not the Camera
@gustavlofqvist5507
@gustavlofqvist5507 2 года назад
Thank you so much for the answer! Much appreciated.
@comp3interactive
@comp3interactive 2 года назад
The object you're calling ViewportPointToRay on should be of type Camera, your variable is either marked as a Transform or you're trying to call ViewportPointToRay on the transform its self. Rewatch the tutorial to see where you made a mistake
@jud.su.5developer895
@jud.su.5developer895 2 года назад
14:16 🤫😂
@stefanwuttig
@stefanwuttig 2 года назад
I Love your Videos and tutorials. But this is much too fast. Ich havr to click every frame too see where you have set new Layer. It takes me 5 times to understand its in the directionel light
@marcus_vdm
@marcus_vdm Год назад
Hi Mike!! Hope you're doing well. I have the following code if (Physics.Raycast(playerCamera.ViewportPointToRay(interactionRaypoint), out RaycastHit hit, detectionDistance)) { if (hit.collider.gameObject.layer == 6 && (currentInteractable == null) || hit.collider.gameObject.GetInstanceID() != currentInteractable.GetInstanceID()) { hit.collider.TryGetComponent(out currentInteractable); if (currentInteractable != null) { currentInteractable.OnFocus(); } } } and occasionally I get the error NullReferenceException: Object reference not set to an instance of an object. It's pointing to if (hit.collider.gameObject.layer == 6 && (currentInteractable == null) || hit.collider.gameObject.GetInstanceID() != currentInteractable.GetInstanceID()) , any idea why that could be?
@Juss_Chillin
@Juss_Chillin 2 года назад
Hey, I really love your tutorials, for me all is working fine except for this tutorial... My Problem (not a big problrm I guess) : I've done everything correctly but the message that I'm looking at the Object is spamming in the console a thousand times per second. Well, mabye not but incredible fast.. What have I done wrong? Please Help! Thanks ^-^
@defiantparsnip
@defiantparsnip 2 года назад
This is happening because the HandleInteractionCheck method is being called during Update, which, as the Unity Documentation shows, is called every frame. So if you're running at 30 frames per second, that's 30 calls per second. To fix this (in 2020.3.3f1) click "Collapse" in the Console tab. This will collapse all identical errors into one.
@Juss_Chillin
@Juss_Chillin 2 года назад
@@defiantparsnip Oooooooh okayyy. THANK YOUUUU!! :D ^·^
@hamidmousa
@hamidmousa 2 года назад
Hiya! I have the same problem. I debug.logged both the the instanceIDs in the != same instanceID check, and they don't ever seem to be the same even if i'm staring at the same interactable object. This then spams the console since it thinks i'm constantly looking at a new object so it keeps invoking onFocus() each frame. To fix this, I just added a public (or serializable) int of ID to the Interaction class. Then set different objects near eachother to different IDs in the inspector and then use this to check: hit.collider.GetComponent().ID != currentInteractable.ID This checks what the raycast is hitting against the current ID, which seems to actually work correctly. Hope this helps! :D
@tinvandulm5765
@tinvandulm5765 2 года назад
da fak is giggatank?
@arbuzxx
@arbuzxx Год назад
Hello I just wanted to make a correction to the code at HandleInteractionCheck()
@arbuzxx
@arbuzxx Год назад
In private void HandleInteractionCheck() { if (Physics.Raycast(playerCamera.ViewportPointToRay(interactionRayPoint), out RaycastHit hit, interactionDistance)) { if (hit.collider.gameObject.layer == 9 && (currentInteractable == null || hit.collider.gameObject.GetInstanceID() != currentInteractable.GetInstanceID())) { hit.collider.TryGetComponent(out currentInteractable); if (currentInteractable) { currentInteractable.OnFocus(); } } // x1 position } else if (currentInteractable) //this should be also in the x1 position otherwise the OnLoseFocus() would only apply when you look at nothing { currentInteractable.OnLoseFocus(); currentInteractable = null; } }
@arbuzxx
@arbuzxx Год назад
so it should actually look like that private void HandleInteractionCheck() { if (Physics.Raycast(playerCamera.ViewportPointToRay(interactionRayPoint), out RaycastHit hit, interactionDistance)) { if (hit.collider.gameObject.layer == 9 && (currentInteractable == null || hit.collider.gameObject.GetInstanceID() != currentInteractable.GetInstanceID())) { hit.collider.TryGetComponent(out currentInteractable); if (currentInteractable) { currentInteractable.OnFocus(); } } else if (currentInteractable) { currentInteractable.OnLoseFocus(); currentInteractable = null; } else if (currentInteractable) //now it dissapears when you look look into nothing (by moving back when aiming at the object), and when you aim somewhere else { currentInteractable.OnLoseFocus(); currentInteractable = null; } }
@arbuzxx
@arbuzxx Год назад
// The way I learned this was making a script that would show the name of the object when you look at it, but I noticed that the only way to stop showing the name of the object was to aim at the sky. So i checked it by doing print("xx") when i looked somewhere and learned that the only way to lose focus is to actually look at nothing (which was the sky in this example). And so I checked everything and learned that this should be a fix. Hope I helped!
Далее
First Person Interact
7:42
Просмотров 9 тыс.
Паук
01:01
Просмотров 2,8 млн
НЕДОВОЛЬНА УСЛУГОЙ #shorts
00:27
Просмотров 20 тыс.
How to add subtitles to your game [Unity Tutorial]
10:11
Animate like a Programmer
1:53
Просмотров 221 тыс.
How To Build An Event System in Unity
8:01
Просмотров 410 тыс.
Unity 3D Animation Blend Tree's (In 3 Minutes!!!)
3:02
How to play random animations [Unity Tutorial]
7:36
Просмотров 21 тыс.
Паук
01:01
Просмотров 2,8 млн