How to make an Interaction Feature (Unity Tutorial)
Вставка
- Опубліковано 22 кві 2021
- Project Files available now on Patreon for our patrons!
/ jtagamesinc
JTAGames is unfortunately closing it's doors. Jon-Tyrell (The guy who's voice you hear 90% of the time) is moving to "Keahunui Technologies" An unofficial company / branding owned exclusively by himself.
Thank you for your support the tutorials will remain on this channel.
Please wishlist Keahunui technologie's first game on steam!
store.steampowered.com/app/15...
If you're feeling super supportive of Keahunui Technologies consider checking out Kickstarter.
www.kickstarter.com/projects/... - Ігри
Super effective and flexible way to make interaction systems instead of having a lot of different scripts with different rays sending out to different things. Thanks for the tutorial :)
How it efficient bro ? It using update method each frame
@@ruhitrai6015 How is it gonna work? To make the script work, you'll need the update function, sure not the most effective, but still more effective than well making millions of scripts for different things.
Thanks! I'm a beginner and made it first with colliders but that was not good at all. Now it feels good.
I'm beginner and I totally understand, thank you so much for the great tutorial
this is so easy I watched a ton of videos but none explained like that many thanks
Great tutorial! I learned a lot.
Really nice tutorial! straight to the point, i would love more tutorials about Horror Games
Our goal is to waste as little of your time as possible so you can get back to developing. Anything specific in mind for horror game tutorials?
@@JTAGames I would love something like reading notes, flashlight, Keys or codelocks, following Ai, everything would help! 😊
You earn my sub
Awsome sir!
thank you best tutorial i have
Great tutorial man! Clear and straight to the point! Thanks for helping us out :D
What are you doin here?
How would I make a bool Toggleable in this? Like say if I want an object to toggle between two materials every time I click on it?
I'd love to see a tutorial using this system on a typical walking simulator mechanic: a paper that you can click on and then have it pop up on the screen so the player can read it
I might do this a UA-cam short
damn this good
Nice tutorial and very helpful. Question: What would be the best to manage lets 100 interactable objects?
This method works fine for 100+, probably even 1000+ I've already used a less refined version in a commercial game with 100+ interactables a while back.
store.steampowered.com/app/1382180/Escape_Knox_Manor/
What's cool is that each interactable doesn't actually do anything they just store information and the interactor script does all the work, along with Unity's event system.
you'll be limited by having too many game objects with mesh colliders long before interactables slow you down.
Let me know if you have any other questions!
@@JTAGames Thanks for your answer and the example ahah ! Keep on with this yt channel.
Hi, how would I make this work in 2d? What do I put the Interactor script on?
How is the animator controller setup, can someone please tell
How would u write the code if i want to select the game object and rotate it then deselect it and it goes back to place
4:55 leaving a mark
1000th Subscriber
Thanks got it sort of working but am left with the animation as certain stuff is not explained and missing which makes this tutorial frustrating. Text size to resolution is just too low; have to go to full screen to read the text. Code is shown for a really short time and sped up a lot. Somewhere we had to write code and delete it one moment later without any reason why we wrote it in the first place (I dont mean the unity events or the logs). "Just simply put in the animator we made", which animator we made? Nowhere to be found, let alone the sprites which are also referenced to as that 'we' made it earlier in the tutorial, which we didn't... I think I am missing some sort of other part that goes beforehand. Sorry for my rant, not a personal attack as I do appreciate that you took the time to explain this!
For anyone else to keep them from getting frustrated; here is the code. Forgot to mention anyone else reading; you have to make a Canvas and create a new object called 'image' in order to add the image in Interactable (also missed out on explaining that).
Animation was not apart of the tutorial. It was just the interaction. Also what text size?
After adding the first interactor script to my camera to test it, my game just no longer starts unless i remove the script.
Edit: I fixed it, i'm not sure why it didnt work exactly but i edited the code to basically say the same thing but in a different way.
void Update()
{
var ray = new Ray(this.transform.position, this.transform.forward);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 50, interactableLayermask))
{
Debug.Log(hit.collider.name);
}
Can you make tutorial like this but in blueprints Bolt,something like this
Ive got to learn bolt first lol. Yeah I'll look into it. It might be a while though.
Hi, is there any way you can make this project downloadable at all? At times you cover things without actually explaining how it is set up, for example the door frame already having an animator, having different animation states such as Opened and Closed, and also the sprites setup. I managed to get the sprites setup working but animation is something I struggle with and either a clearer breakdown of how this was all set up or access to the project folder would really be helpful.
Projects not for download but when I have time I can make a tutorial tackeling animation and animators with this set up as the example.
For future tutorial videos it would be hugely apreciate if u show not only the code but also the setup of the scene. I got lost a lot when I had to asign images and sprites to the project.
how to open door with a key if you have key
how to make a crosshair pick up items
For the Game Jam can we modify some of the code or does it have to be the same as the tutorial
It can be modified! As long as it's recognizable as being from this tutorial you'll be fine. So pretty much a raycast using unity events. Get creative! I'm excited to see what you come up with.
@@JTAGamesok thx
Nice video but can you give us the interaction hand?
Sure. I'm working on a website and it's no where close to being ready but I can put the UI asset there for free. I'll comment here when it's up.
EDIT: jtagamesinfo.wixsite.com/jtagames
This is the beginning of the website we're working on. If you want the hand Icon just go to "Assets" page and click download you'll get a zip file with the single PNG.
Cool friend. Your channel is excellent. I am still a beginner. Is this system suitable for mobiles?
Yes! Follow this tutorial first then follow our most recent mobile control tutorial and at 8:46 in the mobile control video It shows you how to combine this with the mobile controls.
@@JTAGames thanks👍
How do I do it with tmp text? Instead of images I want to add text.
It’s essentially the same but I would recommend keeping the image function and beneath it have a tmp text ui reference and a public string on on the interactable that changes that ui text and have it turn off and on at the same time as the image. Hope that made sense
instead of doing public Text text, do public TMPro text;
Can I have a link to get the code from
www.patreon.com/JTAGamesInc Project files to this tutorial and eventually all of our tutorials are available to our patrons on Patreon.
Great tutorial, however it does not work in my scene. I saw that you have set up a canvas which includes the eventsystem in it...should we all have a canvas in our scene for the script to work?
Yes
@@JTAGames thank you!! Do you maybe have a tutorial on how to add the canvas etc from the beginning?
@@evangelia941 I do not. However, I can give a brief explination.
Right click in the hierarchy, go to UI, and click on image. That will be your interact image. Adjust it's size to your preference.
You shouldn't need anything else from UI
Nice, btw , can you make a interact with outline?
What is outline?
@@JTAGames Maybe he means highline object?
I love this script and think it works very well, however I've noticed an issue. You can interact with objects through walls, is there any way to fix that?
Put default in the layer mask as well. It shouldn’t break anything since it still checks for the script.
@@JTAGames I had already fixed it but thanks!
Tried to follow the code but it does not help me when it reaches the LayerMasks. I copied the code front and back but it does not do anything for me
Have you made the layermasks in the editor? If so are they spelt the same way?
Hi, I'm back again. How can I call a specific Unity Event using the interaction system?
? By adding it to the interactable? What do you mean?
@@JTAGames I mean like: If you interact with this object, an event is called and other scripts will activate.
Another thing is: Is it possible to create a variations in one object? For instance: tapping a button once to turn it on, and tap it a second time to turn it off without using animations?
If there's another video of yours that answers any of my questions, I'm sorry for the annoyance. I'm trying to make a game with no knowledge using only youtube videos to program specific parts as I go.
@@arandomcookie The script already does exactly what you’re asking. It does the same thing as setting up a button. To have different results I’d recommend using bools or iterating through events.
Also I’d recommend reading documentation on Unity Events because using this as a method for interaction will take forever if you don’t have a firm grasp of Unity Events.
@@JTAGames Ok! Thank you very much!
Is there a way to use this system to transition the player between scenes?
Yup! We have a scene change tutorial and you can call the scene change function via interact as opposed to having it triggered when you enter in the trigger
@@JTAGames thanks!
@@JTAGames The only problem I'm having with this is that I'm exiting the doors at the wrong angle, is there a way to rotate the player either just before or just after using the door?
I made a button with the Interactable script to open a hidden door on the wall
When I press this button, the parameter bool "open" toggles to true and the animation plays, but it doesn't actually play. The animator window says that the animation is playing when "open" is true, but in game, none of that is happening.
I have no idea what is happening
Is the door set to static?
@@JTAGames yes.......
it worked
@@hersonmarco2052 Yeah I had the same problem for like 2 hours a few months before I posted this tutorial before I realized static can’t be turned on.
Can you please explain how you hid the blue sphere? If I turn off mesh render, then the blue sphere doesnt appear after the green sphere has been interacted.
When you click on the green sphere
1. Blue sphere : activate game object
2. Green sphere: deactivate game object.
When you click on the blue sphere
1. Green sphere: activate game object
2. Blue sphere: deactivate game object.
Make sure the events are in that order for the respective spheres
@@JTAGames Sorry i must have not been clear, I meant in the first place how did u hide the blue sphere since in the scene tab you couldn't see the sphere at all.
@@jimking1000 Okay I think I understand your question but maybe not, we’ll see. It was disabled in the hierarchy
@@JTAGames Ohh that makes sense thank you. Last question is it possible to increase the range of when you can interact?
@@jimking1000 @1:13 it says out hit, 2… increase or decrease the number in order to increase or decrease the distance.
Thanks for the tutorial, looks great.
I tried it with the Unity Starter Assets First Person Controller and it does not work. I tried the interactor script on the Main Camera, the PlayerFollowCamera and the Player Camera Root. Did not help.
Guess the problem is here:
if(Physics.Raycast(this.transform.position,this.transform.forward,out hit, 2, interactableLayermask))
{
Debug.Log(hit.collider.name);
}
Can anyone help?
Now it works. The problem was that the distance = 2 was too small. Always check the scale of your world.
thank you for this, mine does not work either, what distance you changed it to?
@@evangelia941 In my case, I think I changed to 10. But it always depends on the scale of everything. In the end, you must play around with the values until it work. You could use this debug tool to draw a line and make the ray visible, so you will see where it hits (sorry, forgot the exact way to do it. It is like "Debug.Log" but something else instead of "Log"
Can we get the script it is to small to see
www.patreon.com/JTAGamesInc Project files to this tutorial and eventually all of our tutorials are available to our patrons on Patreon.
Can you make examine object tutorial?
Like holding the object mid air and rotating it?
@@JTAGames Yes, like that,I tried to make a system inspact but always failed
how can I add a cursor in my scene? As soon as you start the program, you can already see the cursor, how can I add one into my scene?
UI image center of the screen.
@@JTAGames it’s as simple as that? But if my camera moves, the cursor won’t follow it right?
@@vitominiello6561 UI isn’t actually in your game scene it only renders to the spot it’s at on the screen. Even if you don’t have a camera in the scene it’ll render. That being said it won’t work without the camera. But no it won’t move unless you change its settings.
@@JTAGames ok thank you really much!
Nice Tutorial. Im having some issues related to the project structure and how to assign the interact icon. Any chance for a link to the code?
Codes not your problem. Drag and drop the image into the inspector.
NullReferenceException: Object reference not set to an instance of an object
Interaction.Update () (at Assets/scripts/Interaction.cs:18)
if (Physics.Raycast(Camera.main.transform.position,Camera.main.transform.forward, out hit,2,Interaction))
how to repair this?
make sure whatever camera you are using is tagged as MainCamera.
@@Delicious_Crystal Thanks
@@Delicious_Crystal Thanks man
Every time I start the game, the animation immediately loops to open and closed without any interaction. Ive got no idea how to fix it
You didn’t set up parameters for your animator.
@@JTAGames Sorry for bothering you again, but I have set up the Open parameter and have the interactible set correctly, but the animation still goes by itself. I am new to all this, so Im probably missing something obvious
@@ManMr. You need to click on the little line on the animator window between your two animations and then then set the BlendTree Parameter to Open, False or Open, True respectively.
@@JTAGames TYSM!
you type so fast i can follow
i want to interacted object for android like granny games
Don’t know what granny games is but this works for mobile and we have another tutorial for that too! It’s the FP mobile controller video on our channel
I have a problem when I open the door, it gets opened and closes automatically
Make sure you set the opposite animation to go when the bool is false and the other when the bool is true. Likely one of them doesn’t have that set up so it just loops back
@@JTAGames hey, thanks for replying, turns out the problem arises only with input system, I tried it with the old Input System and it works fine. Would u mind helping me with this error?
@@darkdoom907 never used the new input system. If I get around to it I’ll let you know 👍 sorry about that
@@JTAGames Its alright, I already know what's causing the problem, explaining in terms of the old input system, my input when i press down sets the bool to be true, but it also reads input when I release the button which causes the bool to become false, and if I keep holding my input, the door open animation gets triggered for like infinite times
Update, i just went along with .triggered function of input system and it works fine
can I use this for Mobile Phone?
With some alterations yeah. I'm working on a tutorial for mobile games and this gets tacked on the end.
@@JTAGames thank you
@@pixelmusic7273 The tutorial is now posted. Follow that tutorial first and then this one. ua-cam.com/video/UkiXp1VzqQo/v-deo.html&t
@@JTAGames thank you soo muchh
I'm so confused what the f is a raycast and layermasks?!?!
I’d recommend learning a little more about unity before tackling this tutorial. A raycast is essentially a laser pointer and the red dot holds the information the script needs. Layermask is an ID system and in this case tells the script what the laser pointer can and can’t point at.
@@JTAGames oh ok thank you very much!! 😀
ur not explaining its hard to follow
Try setting the playback speed to .75 the audio will be weird but it should be easier to follow
Functioned great until I tried using the Interectable script in multiple objects. It just broke. I can only interact with 1 object that uses the script, the other ones just stop working. @JTAGames, what can I do?
I’ve never had that happen before to be totally honest. Is your interactor script properly switching its current interactable?
@@JTAGames I am a firm believer of "Show rather than tell", so I made a quick video showing what is happening. If you can check it out and see if you can find the error, I'd appreciate it. (Please forgive the very rough video, I just want to get this solved as fast as possible) ua-cam.com/video/jgQhX0RASRs/v-deo.html
@@JTAGames Happy ending to this whole situation: I re-wrote the entire thing and it works now!
InteractIcon & iconSize are bugged, for the interacticon I get this -->
'Interactable' does not contain a definition for 'InteractIcon' and no accessible extension method 'InteractIcon' accepting a first argument of type 'Interactable' could be found (are you missing a using directive or an assembly reference?)
I have a Canvas and an InteractIcon in my hierarchie. Here is the piece of code.
if (interactable.InteractIcon != null)
{
interactImage.sprite = interactable.InteractIcon;
if (interactable.iconSize == Vector2.zero)
{
interactImage.rectTransform.sizeDelta = defaultInteractIconSize;
}
else
{
interactImage.rectTransform.sizeDelta = interactable.iconSize;
}
}
Do you have a public sprite called interactIcon in the interactable script?
@@JTAGames No I didn't, thank you!
I'm getting an error whenever I get within the range to interact with the object, I'm not even interacting with the object just getting close to it.
InvalidOperationException: You are trying to read Input using the UnityEngine.Input class, but you have switched active Input handling to Input System package in Player Settings.
UnityEngine.Input.GetKeyDown (UnityEngine.KeyCode key) (at :0)
You updated your input settings so it’s no longer compatible with with any “Input.get”
I haven’t looked into the new input system so as of right now I cannot help much.
you can allow both unity imput system to work at the same time in the setting, maybe it will help
same
just disable the new one.
go to:
edit>> project settings>> player>> active input handling>> input manager(old)
took me a bit to figure out