I want to attach a „grabbed“ object the the interactor. How would you pass the interactor to the interactable, so it can attach itself to the hierarchy of the interactor?
@@Twisted444 interface IInteractable { Public void Interact(); } public class Interactor : MonoBehaviour { public Transform InteractorSource; public float InteractRange;
void Update() { if(Input.GetKeyDown(KeyCode.E)) { Ray r = new Ray(InteractorSource.position, InteractorSource.forward); if(Physics.Raycast(r, out RaycastHit hitInfo, InteractRange)) { if(hitInfo.collider.gameObject.TryGetComponent(out IInteractable interactObj)) { interactObj.Interact(); } } } } }
Great job. One key difference between your videos and many others (not all but most): You explain how things are being used and why. This is great for knuckle draggers like myself
I agree, a lot of videos don't explain the code. When in this video they faded in a bunch of the code on screen at once I was worried they were gonna go "okay just write this don't worry about it" but they really explain how it works here and I'm thankful
I got confused when this didn't work, then I realized you had to assign the camera to the *Interactor Source* on the camera from the script. Works like a charm now. Basically, you have to -Create the script (Interactor) and write the code -Attach the script to the camera -Make the *Interactor Source* on the camera, the camera itself -Make the "NumberGenerator" (or whatever interaction you want) script and write the code -Attach the the script to the object you're interacting with -???? -Profit Hope this helps someone else that made similar silly mistakes to what I did
tysm why aren't all tutorials like this also, to anyone stuck, the problem I had before I got it working was the interactor source should be the camera the script is on and the interactable object's collider needs to have "is trigger" checked.
This is awesome. I had some kind of Interacting sphere being generated to try to collide with objects, and it was not working consistently. I have a feeling this will work much better
Great video, but how would I implement hover? Like it would say click e to interact if I am hovering over the object. Also how do I set up the InteractSource? Do I use the transform of the Camera? Finally what is a good number for the interactRange? Any answers will be greatly appreciated!
You would just need to have the raycast every frame in the Update method and have that sign pop up whenever it hits an interactable object Put all those objects in their own layer and use a layer mask so the ray can only hit objects on that layer
For those of You, If this didn`t Work, Try Like After writing full interaction code, then put the code in Main Camera and then, Reference Main Camera, Like By Dragging it in Hierachy, and after that, the function is all good to run, Sometimes, There should be function which would need you to enable text like You can interact or Pickup or something, For that case this is code, Directly in update, and then follow the same steps like creating interface and then reference it to the object script you want and that`s it Ray rayEverytime = new Ray(interactionSource.position, interactionSource.forward); if(Physics.Raycast(rayEverytime,out RaycastHit hitInfoEverytime, interactionRange)) { if (hitInfoEverytime.collider.gameObject.TryGetComponent(out IInteractableEverytime internObj)) { internObj.InteractEverytime(); } } Thank you for the awesome Tutorial
For anyone wondering, here's the code: interface Interactable { public void Interact(); } public class IntertheAct : MonoBehaviour { public Transform InteracterSource; public float InteractRange; void Update() { if (Input.GetKeyDown(KeyCode.E)) { Ray r = new Ray(InteracterSource.position, InteracterSource.forward); if (Physics.Raycast(r, out RaycastHit hitInfo, InteractRange)) { if (hitInfo.collider.gameObject.TryGetComponent(out Interactable interactObj)) { interactObj.Interact(); } } } } }
SO, I've done everything here. Dragged script to camera, made a cube, dragged randgenerator script to it. Set the InteractorSource as Main Camera, set the range to 5. Script's are word for word, character for character. No dice, no output. Nothing happens.
Did you remember the add the interface to the top of the script? (My bad if that isn't what it's called, i started unity less than a week ago.) like this: public class KeyGiverScript : MonoBehaviour, IInteractable //THIS { public void Interact() { } }
When I try to interact in game it’s not doing anything and I believe it has something to do the raycast as interact with the camera does not show the raycast
I think I had the same issue and figured it out, not sure if you have either. When you've applied the interactor script to your main camera and also set the "interactor source" as your camera too make sure the interact range isn't 0.
use the same way you check for interactable objects with a ray, with a second ray that is always casting, to see if you still look at the interactable object and as soon as you don´t disable or empty you text. Probably not the best way but works for my use-case.
I have a same problem, I've looked to the code and this video many times to check what was wrong, but both are the same, I've typed all of the code right.
okay so im trynna do like an open locker door thing but when i make the animator do the open animation it goes okay, but when i try to close it, it still tries to open it even if its already open (i know this bc i also but a debug.log statment telling me if the door is opened and it always comes back as opened) so is there any solution to this?
My second if statement doesn't seem to be triggering: void Update() { if (Input.GetKeyDown(KeyCode.E)) { Ray r = new Ray(InteractorSource.position, InteractorSource.forward); if (Physics.Raycast(r, out RaycastHit hitInfo, InteractRange)) { Debug.Log("Interactor p1"); if (hitInfo.collider.gameObject.TryGetComponent(out IInteractable interactObj)) { interactObj.Interact(); Debug.Log("Interactor p2"); } }
} } I've attached the interactor to the main camera, set the interactor source to be the main camera and set the range to 100 (just to be sure) And then I've attached the example script to a default cube. When testing, the Interactor p1 message gets pushed to the console, but not the Interactor p2 message, and I don't understand why. Any help would be great!
SIMPLE ERROR FIX: people that have abstract script error can make a new script of IInteractable and interface to public interface if you have any no use error then check for capital letters
If I add multiple Interactions to my Object it only executes the first in the component hirachie. Is there a way to execute all, cause I want to reuse these scripts on other objects say a sign and a note can both use text being shown on interaction.
Would need more context to this, but I'm pretty sure you would just merge the interactions to one script? (I don't fully understand what your saying). I'm just throwing out a guess by the way I am still pretty new to C#, but understand programming logic so I could figure it out if I saw the actual code and was given time to tinker.
using System.Collections; using System.Collections.Generic; using UnityEngine; interface IInteractable { public void Interact(); } public class Interactor : MonoBehaviour { public Transform InteractorSource; public float InteractRange; void Update() { if (Input.GetKeyDown(KeyCode.E)) { Ray r = new Ray(InteractorSource.position, InteractorSource.forward); if (Physics.Raycast(r, out RaycastHit hitInfo, InteractRange)) { if (hitInfo.collider.gameObject.TryGetComponent(out IInteractable interactObj)) { interactObj.Interact(); } } } } }
Can i ask how to make it work in one script, i has disable the first script then when i use the second one, it active the first one even the first one is disable, when i private the Interact function it become error, thanks
Suppose I use a coroutine with a WaitUntil that waits until F is pressed in my Interact. In that case, it will still execute when I am no longer looking at the object [which makes sense], any ideas on how I "cancel" the Interaction or the Coroutine after it´s started? Because I don´t want to do the whole ray-checking thing in my Interact scripts, as that would kinda defeat the purpose of the interface and would bloat scripts.
I was trying to interact with a door but nothing was happening, then I realized the door didn't have a collider... 😳 (shared this incase any beginner struggled like me 😅)
I seem to be having an issue where Unity is telling me that InteractorSource.Position doesn't exist (at what should be line 22), could someone give me some pointers?
i just got back into coding and i used ur tutorial for picking up items, but I cant figure out how to make it to where the object faces the camera. Can anyone help?
Ik this is late, but maybe you could have the item, upon using the interact function, will find the camera object and copy it's rotation? Maybe you could also lerp the rotation from what it was to what you want it to be to make it smoother.
So you can have multiple different classes be unified under one reference. For instance, you can have two classes like "door" and "chair" and you wouldn't have to check for two components if you can just get the interface as a component.
I'm having an issue and I cant figure out why, I have the raycast script and everything and it was working and then I made a new object and tested the number generator on it and it wouldnt work but the original objects did anyone know why?
Two problems that are frequently re-occurring are: 1. The raycast is being blocked by text or an image on your canvas. To solve it, you either need to put those on a different layer or uncheck their raycast option. (This also counts for textmeshPro) 2. You use cursor lock mode and by default, Unity thought it was a good idea to lock your actual invisible cursor to a position of 0,0 (Upper left of your screen) instead of in the middle. In later versions of Unity's Input System they've added a dropdown option where you can lock the cursor and have it be in the middle.
Verify that the script name in Unity matches the one in Visual Studio; a discrepancy here could be the issue. The script names must be identical in both Unity and Visual Studio.
Fixed! In case someone have same error as me, check "public class Interactor : MonoBehaviour", that you have same name of script as in script (in my case "Interactor")
@@anabolix_ How exactly does looking around on tutorials, essentially looking at various/multiple ways to get a system done make someone a noob? Especially when you have said system already implemented into your own project and just look at these to see if you can expand on it.
For those who want the code itself without the explaining, I have an archive that is in the works in my discord (link in description)
can you just paste the code as a comment?
Why wouldn't you just use pastebin instead of a discord link with a chat that say No Access??
I want to attach a „grabbed“ object the the interactor. How would you pass the interactor to the interactable, so it can attach itself to the hierarchy of the interactor?
@@novagardenstudios Cuz he's greedy
@@Twisted444 interface IInteractable {
Public void Interact();
}
public class Interactor : MonoBehaviour
{
public Transform InteractorSource;
public float InteractRange;
void Update() {
if(Input.GetKeyDown(KeyCode.E)) {
Ray r = new Ray(InteractorSource.position, InteractorSource.forward);
if(Physics.Raycast(r, out RaycastHit hitInfo, InteractRange)) {
if(hitInfo.collider.gameObject.TryGetComponent(out IInteractable interactObj)) {
interactObj.Interact();
}
}
}
}
}
Great job. One key difference between your videos and many others (not all but most): You explain how things are being used and why. This is great for knuckle draggers like myself
I agree, a lot of videos don't explain the code. When in this video they faded in a bunch of the code on screen at once I was worried they were gonna go "okay just write this don't worry about it" but they really explain how it works here and I'm thankful
incredible tutorial. Very concise, no waffling, explained everything i wanted to know. 5 star review, will come again
I got confused when this didn't work, then I realized you had to assign the camera to the *Interactor Source* on the camera from the script. Works like a charm now.
Basically, you have to
-Create the script (Interactor) and write the code
-Attach the script to the camera
-Make the *Interactor Source* on the camera, the camera itself
-Make the "NumberGenerator" (or whatever interaction you want) script and write the code
-Attach the the script to the object you're interacting with
-????
-Profit
Hope this helps someone else that made similar silly mistakes to what I did
your're a legend, it worked
Your vid just solved a problem I've been trying to solve for a week. .....You're getting a shout-out in my next dev log
tysm why aren't all tutorials like this
also, to anyone stuck, the problem I had before I got it working was the interactor source should be the camera the script is on and the interactable object's collider needs to have "is trigger" checked.
Dayumm, lmfao this was way better than creating an if statement for each object
uh sounds painful
Thank you very much! Was struggling handling interactions, and you just solved one of my main problems.
Worked perfectly for my purposes. Thanks.
This is awesome. I had some kind of Interacting sphere being generated to try to collide with objects, and it was not working consistently. I have a feeling this will work much better
Extremely well made tutorial! Keep up the work!
Great video, but how would I implement hover? Like it would say click e to interact if I am hovering over the object. Also how do I set up the InteractSource? Do I use the transform of the Camera? Finally what is a good number for the interactRange? Any answers will be greatly appreciated!
You would just need to have the raycast every frame in the Update method and have that sign pop up whenever it hits an interactable object
Put all those objects in their own layer and use a layer mask so the ray can only hit objects on that layer
@@tornadre FixedUpdate
fast explanation. excellent
Super helpful, thank you
Thanks man! Great knowledge
Sir. For this you got my sub
For those of You, If this didn`t Work, Try Like After writing full interaction code, then put the code in Main Camera and then, Reference Main Camera, Like By Dragging it in Hierachy, and after that, the function is all good to run,
Sometimes, There should be function which would need you to enable text like You can interact or Pickup or something,
For that case this is code, Directly in update, and then follow the same steps like creating interface and then reference it to the object script you want and that`s it
Ray rayEverytime = new Ray(interactionSource.position, interactionSource.forward);
if(Physics.Raycast(rayEverytime,out RaycastHit hitInfoEverytime, interactionRange))
{
if (hitInfoEverytime.collider.gameObject.TryGetComponent(out IInteractableEverytime internObj))
{
internObj.InteractEverytime();
}
}
Thank you for the awesome Tutorial
ty worked
I know this comment is like 8 months old but thank you man
@@violetdreamweaver Welcome bro
For anyone wondering, here's the code:
interface Interactable
{
public void Interact();
}
public class IntertheAct : MonoBehaviour
{
public Transform InteracterSource;
public float InteractRange;
void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
Ray r = new Ray(InteracterSource.position, InteracterSource.forward);
if (Physics.Raycast(r, out RaycastHit hitInfo, InteractRange)) {
if (hitInfo.collider.gameObject.TryGetComponent(out Interactable interactObj))
{
interactObj.Interact();
}
}
}
}
}
TY SO MUCHHHH
JUST YOINK Thank you!!!@
Wow great video loved it.
SO, I've done everything here. Dragged script to camera, made a cube, dragged randgenerator script to it. Set the InteractorSource as Main Camera, set the range to 5. Script's are word for word, character for character. No dice, no output. Nothing happens.
same here. did you solve that?
Did you remember the add the interface to the top of the script? (My bad if that isn't what it's called, i started unity less than a week ago.)
like this:
public class KeyGiverScript : MonoBehaviour, IInteractable //THIS
{
public void Interact()
{
}
}
nvm
bro i loooove you so much
When I try to interact in game it’s not doing anything and I believe it has something to do the raycast as interact with the camera does not show the raycast
I think I had the same issue and figured it out, not sure if you have either. When you've applied the interactor script to your main camera and also set the "interactor source" as your camera too make sure the interact range isn't 0.
@@thedude6389 Already checked all of that, still not working :/
Nevermind, had to make my range higher. Works perfectly now
@@toiletmangaminghd5714 I have the same problem what did you set your range to?
@@Lambo_swiper5 It will be enough to give 2
It says the name space IInteractable could not be found, there are no errors in studio code, only in the unity engine.
Thanks man, helped out a LOT with my game!
You can also use a enum to seperate the different types of objects btw
I kinda like these shawty tutorials
what do you put for the Interactor Source
The camera
@@bluejay7071 thanks pal
@@bluejay7071 if you put the script into the camera then you can refer to this.transform instead.
@@bluejay7071 oh, thanks man, I was going to create a new game object hahaha
How do I make it show text when I am in range?
use the same way you check for interactable objects with a ray, with a second ray that is always casting, to see if you still look at the interactable object and as soon as you don´t disable or empty you text.
Probably not the best way but works for my use-case.
@@brage2can you please send me the code
The only issue I have with this tutorial is an Unassigned Reference Exception for the InteracterSource. Is there a way to fix this?
I have a same problem, I've looked to the code and this video many times to check what was wrong, but both are the same, I've typed all of the code right.
@@wescomvalleninksphe didn’t show as he assigned it in the unity editor
thank you very much!!
okay so im trynna do like an open locker door thing but when i make the animator do the open animation it goes okay, but when i try to close it, it still tries to open it even if its already open (i know this bc i also but a debug.log statment telling me if the door is opened and it always comes back as opened) so is there any solution to this?
try using a bool
If we have more than one interaction key, what can we do?
what to put in interactorSource?
Put your camera there !
do u attach the interact script to the player?
My second if statement doesn't seem to be triggering:
void Update() {
if (Input.GetKeyDown(KeyCode.E)) {
Ray r = new Ray(InteractorSource.position, InteractorSource.forward);
if (Physics.Raycast(r, out RaycastHit hitInfo, InteractRange)) {
Debug.Log("Interactor p1");
if (hitInfo.collider.gameObject.TryGetComponent(out IInteractable interactObj)) {
interactObj.Interact();
Debug.Log("Interactor p2");
}
}
}
}
I've attached the interactor to the main camera, set the interactor source to be the main camera and set the range to 100 (just to be sure) And then I've attached the example script to a default cube. When testing, the Interactor p1 message gets pushed to the console, but not the Interactor p2 message, and I don't understand why. Any help would be great!
SIMPLE ERROR FIX: people that have abstract script error can make a new script of IInteractable and interface to public interface if you have any no use error then check for capital letters
Really helpfull tysm
If I add multiple Interactions to my Object it only executes the first in the component hirachie. Is there a way to execute all, cause I want to reuse these scripts on other objects say a sign and a note can both use text being shown on interaction.
Would need more context to this, but I'm pretty sure you would just merge the interactions to one script? (I don't fully understand what your saying). I'm just throwing out a guess by the way I am still pretty new to C#, but understand programming logic so I could figure it out if I saw the actual code and was given time to tinker.
I'm 7 months late but Unity has GetComponents() with an s
It returns a list that you can loop over
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
interface IInteractable {
public void Interact();
}
public class Interactor : MonoBehaviour
{
public Transform InteractorSource;
public float InteractRange;
void Update()
{
if (Input.GetKeyDown(KeyCode.E)) {
Ray r = new Ray(InteractorSource.position, InteractorSource.forward);
if (Physics.Raycast(r, out RaycastHit hitInfo, InteractRange)) {
if (hitInfo.collider.gameObject.TryGetComponent(out IInteractable interactObj)) {
interactObj.Interact();
}
}
}
}
}
Put this on camera ?
@@alexkarivelil6350 put it on anything but remember to drag and drop camera into InteractSource
@@RISHABHKUMAR-zk1fu thanks I’m new I’m trying to make a game ! Sorry my English btw
my code just says error and tells me that the interactorSource is empty
same
Assign your camera there
Can i ask how to make it work in one script, i has disable the first script then when i use the second one, it active the first one even the first one is disable, when i private the Interact function it become error, thanks
If your script says public is not a valid term
Just remove it and it'll still work
Suppose I use a coroutine with a WaitUntil that waits until F is pressed in my Interact. In that case, it will still execute when I am no longer looking at the object [which makes sense], any ideas on how I "cancel" the Interaction or the Coroutine after it´s started? Because I don´t want to do the whole ray-checking thing in my Interact scripts, as that would kinda defeat the purpose of the interface and would bloat scripts.
use stuff like 'return;' or hows called. im not sure but i think it works
How would you put a countdow timer into this system?
I was trying to interact with a door but nothing was happening, then I realized the door didn't have a collider... 😳 (shared this incase any beginner struggled like me 😅)
Thanks :D
I seem to be having an issue where Unity is telling me that InteractorSource.Position doesn't exist (at what should be line 22), could someone give me some pointers?
What did you declare as the InteractorSource? It should be the camera. Make sure it is set as the camera in the inspector.
@@bluejay7071 ty. this was exactly the answer i was looking for as well
i just got back into coding and i used ur tutorial for picking up items, but I cant figure out how to make it to where the object faces the camera. Can anyone help?
Ik this is late, but maybe you could have the item, upon using the interact function, will find the camera object and copy it's rotation? Maybe you could also lerp the rotation from what it was to what you want it to be to make it smoother.
Why exactly did we add the interface?
So you can have multiple different classes be unified under one reference. For instance, you can have two classes like "door" and "chair" and you wouldn't have to check for two components if you can just get the interface as a component.
@@Rytech_Dev ty
I'm having an issue and I cant figure out why, I have the raycast script and everything and it was working and then I made a new object and tested the number generator on it and it wouldnt work but the original objects did anyone know why?
Two problems that are frequently re-occurring are:
1. The raycast is being blocked by text or an image on your canvas. To solve it, you either need to put those on a different layer or uncheck their raycast option. (This also counts for textmeshPro)
2. You use cursor lock mode and by default, Unity thought it was a good idea to lock your actual invisible cursor to a position of 0,0 (Upper left of your screen) instead of in the middle. In later versions of Unity's Input System they've added a dropdown option where you can lock the cursor and have it be in the middle.
I am getting errors about script class not being found when I try to add the script. any advice?
Verify that the script name in Unity matches the one in Visual Studio; a discrepancy here could be the issue. The script names must be identical in both Unity and Visual Studio.
Trying to add script to MainCamera and it says this, any ideas?
"Can't add script behaviour 'Interactor'. The script class can't be abstract!"
Fixed! In case someone have same error as me, check "public class Interactor : MonoBehaviour", that you have same name of script as in script (in my case "Interactor")
I don’t have some of the buttons for the script
can I use it for a 3d person game?
do you mean third person?
@@s-ata-n yes
@@maucazalv903 İ dont know the anwser just wanted to be sure. lol
yep im using it right now
cant make that work :( error error m8 i copy and pasted, how do i have error
Can this be used in mobile? Thank you
instead of Input.GetKeyDown, do GetMouseButtonDown(0)) and it will work as a tap on mobile.
wohhhoa
version mobile?
your tutorials never work for me for some reason
Did you do it properly?
25 upvotes from Unity noobies.
@@Legionope be nice
@@LegionopeLegit you are also noob, so wtf, or why do you have to watch unity tutorial?🤔
@@anabolix_ How exactly does looking around on tutorials, essentially looking at various/multiple ways to get a system done make someone a noob? Especially when you have said system already implemented into your own project and just look at these to see if you can expand on it.
Didn't work.
sero izlendi
i don't get it at all
WHATTA HEK BRO İ HATE WACH
İNG OTHER TTORAL SHT AFTER SEENİNG THİS
How did you do that effect where the coin from world position went to the coin ui position