How to Make a Flexible Interaction System in 2 Minutes [C#] [Unity3D]

แชร์
ฝัง
  • เผยแพร่เมื่อ 12 ธ.ค. 2024

ความคิดเห็น • 129

  • @Rytech_Dev
    @Rytech_Dev  11 หลายเดือนก่อน +12

    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)

    • @Twisted444
      @Twisted444 9 หลายเดือนก่อน +7

      can you just paste the code as a comment?

    • @novagardenstudios
      @novagardenstudios 9 หลายเดือนก่อน +5

      Why wouldn't you just use pastebin instead of a discord link with a chat that say No Access??

    • @DoompyGames
      @DoompyGames 7 หลายเดือนก่อน

      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?

    • @Dootium
      @Dootium 7 หลายเดือนก่อน

      @@novagardenstudios Cuz he's greedy

    • @kiboslams
      @kiboslams 5 หลายเดือนก่อน

      @@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();
      }
      }
      }
      }
      }

  • @sxsignal
    @sxsignal ปีที่แล้ว +52

    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

    • @FauxFemale
      @FauxFemale ปีที่แล้ว +5

      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

  • @kamcio8586
    @kamcio8586 ปีที่แล้ว +19

    incredible tutorial. Very concise, no waffling, explained everything i wanted to know. 5 star review, will come again

  • @christophersmith1658
    @christophersmith1658 17 วันที่ผ่านมา +1

    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

    • @asphaltspectre338
      @asphaltspectre338 21 ชั่วโมงที่ผ่านมา

      your're a legend, it worked

  • @Astro-Drifter
    @Astro-Drifter 8 หลายเดือนก่อน +2

    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

  • @justawhisp4160
    @justawhisp4160 4 หลายเดือนก่อน +3

    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.

  • @kalpolproductions8558
    @kalpolproductions8558 ปีที่แล้ว +17

    Dayumm, lmfao this was way better than creating an if statement for each object

    • @Playeroth
      @Playeroth 22 วันที่ผ่านมา

      uh sounds painful

  • @JoshuaAguilar-fc6np
    @JoshuaAguilar-fc6np ปีที่แล้ว +3

    Thank you very much! Was struggling handling interactions, and you just solved one of my main problems.

  • @MalicProductions
    @MalicProductions ปีที่แล้ว +1

    Worked perfectly for my purposes. Thanks.

  • @trispycee
    @trispycee 5 หลายเดือนก่อน +1

    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

  • @varcel4625
    @varcel4625 7 หลายเดือนก่อน

    Extremely well made tutorial! Keep up the work!

  • @fieryninja2374
    @fieryninja2374 ปีที่แล้ว +9

    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!

    • @tornadre
      @tornadre ปีที่แล้ว +5

      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

    • @nahuelhemsi
      @nahuelhemsi 9 หลายเดือนก่อน +2

      @@tornadre FixedUpdate

  • @zachariusd
    @zachariusd 2 หลายเดือนก่อน

    fast explanation. excellent

  • @DevilDrinksTea
    @DevilDrinksTea ปีที่แล้ว +1

    Super helpful, thank you

  • @thescribblersdiary
    @thescribblersdiary 4 หลายเดือนก่อน

    Thanks man! Great knowledge

  • @somestuff3872
    @somestuff3872 ปีที่แล้ว +1

    Sir. For this you got my sub

  • @samyplayz261
    @samyplayz261 ปีที่แล้ว +7

    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

    • @kackebaumzwo
      @kackebaumzwo 9 หลายเดือนก่อน

      ty worked

    • @violetdreamweaver
      @violetdreamweaver 7 หลายเดือนก่อน

      I know this comment is like 8 months old but thank you man

    • @samyplayz261
      @samyplayz261 7 หลายเดือนก่อน

      @@violetdreamweaver Welcome bro

  • @emeraldskits4026
    @emeraldskits4026 ปีที่แล้ว +31

    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();
    }
    }
    }
    }
    }

    • @ZaltInVR
      @ZaltInVR ปีที่แล้ว

      TY SO MUCHHHH

    • @Monkefinn2
      @Monkefinn2 ปีที่แล้ว

      JUST YOINK Thank you!!!@

  • @isacsimoes_
    @isacsimoes_ ปีที่แล้ว +1

    Wow great video loved it.

  • @novagardenstudios
    @novagardenstudios 9 หลายเดือนก่อน +2

    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.

    • @paininkabir4760
      @paininkabir4760 8 หลายเดือนก่อน

      same here. did you solve that?

    • @ransomha
      @ransomha 7 หลายเดือนก่อน

      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()
      {
      }
      }

    • @mrwaffl333
      @mrwaffl333 5 หลายเดือนก่อน

      nvm

  • @Sharky.Sharky.Sharky
    @Sharky.Sharky.Sharky 2 หลายเดือนก่อน

    bro i loooove you so much

  • @canadianreviewer4649
    @canadianreviewer4649 ปีที่แล้ว +5

    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

    • @thedude6389
      @thedude6389 ปีที่แล้ว +6

      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.

    • @toiletmangaminghd5714
      @toiletmangaminghd5714 ปีที่แล้ว

      @@thedude6389 Already checked all of that, still not working :/

    • @toiletmangaminghd5714
      @toiletmangaminghd5714 ปีที่แล้ว +1

      Nevermind, had to make my range higher. Works perfectly now

    • @Lambo_swiper5
      @Lambo_swiper5 ปีที่แล้ว

      @@toiletmangaminghd5714 I have the same problem what did you set your range to?

    • @necmi_sonmez
      @necmi_sonmez ปีที่แล้ว

      @@Lambo_swiper5 It will be enough to give 2

  • @bigglasses2625
    @bigglasses2625 6 หลายเดือนก่อน +2

    It says the name space IInteractable could not be found, there are no errors in studio code, only in the unity engine.

  • @AcedGamedev
    @AcedGamedev ปีที่แล้ว +1

    Thanks man, helped out a LOT with my game!

  • @itsvolt69
    @itsvolt69 หลายเดือนก่อน

    You can also use a enum to seperate the different types of objects btw

  • @black_squall
    @black_squall ปีที่แล้ว

    I kinda like these shawty tutorials

  • @tidectrl
    @tidectrl ปีที่แล้ว +5

    what do you put for the Interactor Source

    • @bluejay7071
      @bluejay7071 ปีที่แล้ว +1

      The camera

    • @tidectrl
      @tidectrl ปีที่แล้ว

      @@bluejay7071 thanks pal

    • @ukaszgrabski2811
      @ukaszgrabski2811 10 หลายเดือนก่อน

      @@bluejay7071 if you put the script into the camera then you can refer to this.transform instead.

    • @viniciuspires2629
      @viniciuspires2629 3 หลายเดือนก่อน

      @@bluejay7071 oh, thanks man, I was going to create a new game object hahaha

  • @Milzy_TBC
    @Milzy_TBC ปีที่แล้ว +3

    How do I make it show text when I am in range?

    • @brage2
      @brage2 ปีที่แล้ว +1

      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.

    • @alexkarivelil6350
      @alexkarivelil6350 5 หลายเดือนก่อน

      @@brage2can you please send me the code

  • @shwoomgloom6763
    @shwoomgloom6763 ปีที่แล้ว +1

    The only issue I have with this tutorial is an Unassigned Reference Exception for the InteracterSource. Is there a way to fix this?

    • @wescomvalleninksp
      @wescomvalleninksp ปีที่แล้ว

      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.

    • @delenter3296
      @delenter3296 10 หลายเดือนก่อน

      @@wescomvalleninksphe didn’t show as he assigned it in the unity editor

  • @legosol1081
    @legosol1081 ปีที่แล้ว

    thank you very much!!

  • @Jay_BanZ
    @Jay_BanZ ปีที่แล้ว +1

    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?

    • @tobtob1816
      @tobtob1816 ปีที่แล้ว +1

      try using a bool

  • @mutlugameofhalit
    @mutlugameofhalit หลายเดือนก่อน

    If we have more than one interaction key, what can we do?

  • @luckybeeyt
    @luckybeeyt ปีที่แล้ว +3

    what to put in interactorSource?

    • @Sn3adPlays
      @Sn3adPlays ปีที่แล้ว +2

      Put your camera there !

  • @TheLastEcho1
    @TheLastEcho1 หลายเดือนก่อน

    do u attach the interact script to the player?

  • @Revlos
    @Revlos 2 หลายเดือนก่อน +2

    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!

  • @Just_the_Cube
    @Just_the_Cube 9 หลายเดือนก่อน +1

    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

  • @lucianbodnaresku6568
    @lucianbodnaresku6568 ปีที่แล้ว

    Really helpfull tysm

  • @brage2
    @brage2 ปีที่แล้ว +1

    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.

    • @cheetamcu644
      @cheetamcu644 ปีที่แล้ว +1

      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.

    • @AliAlBrrak
      @AliAlBrrak 10 หลายเดือนก่อน

      I'm 7 months late but Unity has GetComponents() with an s
      It returns a list that you can loop over

  • @XCA7IBUR
    @XCA7IBUR 6 หลายเดือนก่อน +7

    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();
    }
    }
    }
    }
    }

    • @alexkarivelil6350
      @alexkarivelil6350 5 หลายเดือนก่อน +1

      Put this on camera ?

    • @RISHABHKUMAR-zk1fu
      @RISHABHKUMAR-zk1fu 2 หลายเดือนก่อน +1

      @@alexkarivelil6350 put it on anything but remember to drag and drop camera into InteractSource

    • @alexkarivelil6350
      @alexkarivelil6350 2 หลายเดือนก่อน

      @@RISHABHKUMAR-zk1fu thanks I’m new I’m trying to make a game ! Sorry my English btw

  • @Auck130
    @Auck130 4 หลายเดือนก่อน +1

    my code just says error and tells me that the interactorSource is empty

  • @hoangbach5175
    @hoangbach5175 7 หลายเดือนก่อน

    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

  • @Ejim130
    @Ejim130 4 หลายเดือนก่อน +1

    If your script says public is not a valid term
    Just remove it and it'll still work

  • @brage2
    @brage2 ปีที่แล้ว

    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.

    • @hyperDev_
      @hyperDev_ 11 หลายเดือนก่อน

      use stuff like 'return;' or hows called. im not sure but i think it works

  • @Endercraft-kf3tk
    @Endercraft-kf3tk ปีที่แล้ว

    How would you put a countdow timer into this system?

  • @ga7axy
    @ga7axy ปีที่แล้ว +1

    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 😅)

  • @emeraldwas9k
    @emeraldwas9k ปีที่แล้ว

    Thanks :D

  • @DeveloperOfReality
    @DeveloperOfReality ปีที่แล้ว

    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?

    • @bluejay7071
      @bluejay7071 ปีที่แล้ว +1

      What did you declare as the InteractorSource? It should be the camera. Make sure it is set as the camera in the inspector.

    • @thewaterwasfine
      @thewaterwasfine 11 หลายเดือนก่อน

      @@bluejay7071 ty. this was exactly the answer i was looking for as well

  • @imwatchingasalways
    @imwatchingasalways ปีที่แล้ว +2

    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?

    • @theodore6432
      @theodore6432 ปีที่แล้ว +1

      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.

  • @DolphLOL
    @DolphLOL ปีที่แล้ว +1

    Why exactly did we add the interface?

    • @Rytech_Dev
      @Rytech_Dev  ปีที่แล้ว +5

      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.

    • @DolphLOL
      @DolphLOL ปีที่แล้ว

      @@Rytech_Dev ty

  • @ethanBBB
    @ethanBBB ปีที่แล้ว

    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?

    • @Legionope
      @Legionope 6 หลายเดือนก่อน

      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.

  • @Grizzdrop
    @Grizzdrop ปีที่แล้ว

    I am getting errors about script class not being found when I try to add the script. any advice?

    • @yash_dhiman-tw8nh
      @yash_dhiman-tw8nh 9 หลายเดือนก่อน

      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.

  • @sebastianbramborak2756
    @sebastianbramborak2756 ปีที่แล้ว

    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!"

    • @sebastianbramborak2756
      @sebastianbramborak2756 ปีที่แล้ว +1

      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")

  • @PugVRYT_Official
    @PugVRYT_Official ปีที่แล้ว

    I don’t have some of the buttons for the script

  • @maucazalv903
    @maucazalv903 ปีที่แล้ว

    can I use it for a 3d person game?

    • @s-ata-n
      @s-ata-n ปีที่แล้ว +1

      do you mean third person?

    • @maucazalv903
      @maucazalv903 ปีที่แล้ว

      @@s-ata-n yes

    • @s-ata-n
      @s-ata-n ปีที่แล้ว +1

      @@maucazalv903 İ dont know the anwser just wanted to be sure. lol

    • @JackDylanDaniels
      @JackDylanDaniels ปีที่แล้ว +1

      yep im using it right now

  • @OpxGames
    @OpxGames 2 หลายเดือนก่อน

    cant make that work :( error error m8 i copy and pasted, how do i have error

  • @lettuce1355
    @lettuce1355 ปีที่แล้ว

    Can this be used in mobile? Thank you

    • @MysticMaverick0639
      @MysticMaverick0639 ปีที่แล้ว

      instead of Input.GetKeyDown, do GetMouseButtonDown(0)) and it will work as a tap on mobile.

  • @Deleted328
    @Deleted328 ปีที่แล้ว

    wohhhoa

  • @JunaBot
    @JunaBot ปีที่แล้ว

    version mobile?

  • @huseyinyeldan7998
    @huseyinyeldan7998 ปีที่แล้ว +32

    your tutorials never work for me for some reason

    • @der-Dritte
      @der-Dritte 8 หลายเดือนก่อน +4

      Did you do it properly?

    • @Legionope
      @Legionope 6 หลายเดือนก่อน +4

      25 upvotes from Unity noobies.

    • @der-Dritte
      @der-Dritte 6 หลายเดือนก่อน +4

      @@Legionope be nice

    • @anabolix_
      @anabolix_ 4 หลายเดือนก่อน +1

      @@LegionopeLegit you are also noob, so wtf, or why do you have to watch unity tutorial?🤔

    • @Legionope
      @Legionope 4 หลายเดือนก่อน +3

      @@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.

  • @toto3777
    @toto3777 ปีที่แล้ว +1

    Didn't work.

  • @cqrhqr3418
    @cqrhqr3418 6 หลายเดือนก่อน

    sero izlendi

  • @watercat1248
    @watercat1248 2 หลายเดือนก่อน

    i don't get it at all

  • @Maskeowl
    @Maskeowl ปีที่แล้ว +2

    WHATTA HEK BRO İ HATE WACH
    İNG OTHER TTORAL SHT AFTER SEENİNG THİS

  • @thev01d12
    @thev01d12 ปีที่แล้ว

    How did you do that effect where the coin from world position went to the coin ui position