Get Object from Mouse Click and Call Functions through Interface 2D & 3D - Unity Tutorial

Поделиться
HTML-код
  • Опубликовано: 20 янв 2025

Комментарии • 84

  • @cjc813
    @cjc813 2 года назад +9

    Just wow. You (a) showed the code, (b) explained the ideas/functionality behind the code, (c) referenced the relevant documentation, and (d) were clear in your explanation.
    Most coding tutorials *might* do like 2 out of 4.
    Thanks. :)

  • @gurfotron7
    @gurfotron7 4 года назад +7

    These videos are really great thank you. The focuses are all very practical and your explanations/breakdown are easy to follow.

  • @patnev9296
    @patnev9296 2 года назад +3

    Awesome video, love the editing, explanation and pacing of it. Clear and easy to follow :)

    • @samyam
      @samyam  2 года назад +1

      Glad to hear! 😄

  • @Caster_TechMo
    @Caster_TechMo 3 года назад

    all I did was skim your video to see if it was what I needed and I can already tell this is a better tutorial than 90% of anything else iv found on youtube. I really appreciate that you took the time to show how to not only make specific responses based on what you click but also how to do it in both 2D and 3D. YOU GOT A NEW SUB!!!

    • @samyam
      @samyam  3 года назад

      Yay! Awesome 😄

  • @quinzelbeck
    @quinzelbeck 2 года назад

    Wow, this is an amazing tutorial, im searching for a fast answer because im doing something for a jam, but the explenation makes me to watch all

  • @usercontent2112
    @usercontent2112 7 дней назад

    Perfect tutorial. My project is already completed :))

  • @fateshow5303
    @fateshow5303 2 года назад +1

    super clear and simple to follow, really great!

    • @samyam
      @samyam  2 года назад

      thanks!!

  • @_olives
    @_olives 4 года назад +1

    Your tutorials are all super helpful!!

  • @danniton9831
    @danniton9831 3 года назад +3

    3:01 - 3:49 For those who wants to do this in a much easier and simpler way. Just write "Input.mousePosition" instead of "controls.Mouse.Position.Readval.........." this way, you can skip all that setup in the time stamp.

    • @samyam
      @samyam  3 года назад +4

      Or for the new input system you can use
      using UnityEngine.InputSystem;
      ...
      Vector2 mousePosition = Mouse.current.position.ReadValue();

    • @itookaonabookanyway7385
      @itookaonabookanyway7385 2 года назад

      @@samyam I'm too lazy but does it have a performance improvement over Input.mousePosition?

  • @aslightlyovergrowncactus3315
    @aslightlyovergrowncactus3315 2 года назад

    Thanks! I'm a beginner at Unity and this was really helpful!

    • @samyam
      @samyam  2 года назад

      glad to hear!

  • @HadiLePanda
    @HadiLePanda 4 года назад +8

    Great tutorial as usual, really like the format with the recap ^^
    That keyboard's clicky sounds made the video go from 10/10 to 11/10 haha, which keyboard is it?

    • @samyam
      @samyam  4 года назад +2

      Thanks 😂 The Logitech G815 Clicky keyboard

  • @kuumaaa
    @kuumaaa 3 года назад

    thanks very helpful, had some trouble with raycasts and how to correctly interact with the interface. exactly what i was looking for

  • @g.nath0
    @g.nath0 2 года назад

    11:26 I got a problem with "hit" in lines 106,107 says CS0103: The name 'hit' does not exist in the current context. What I missed

  • @gejohans
    @gejohans 5 месяцев назад

    I have tried using this code to click on things with raycast. When I take "reset level" or click "back to choose level scene" and choose another level that has the same scenemanager, my camera breaks and it refers to "started" in the code. Why does the camera break?

  • @kaloshabuthapa3284
    @kaloshabuthapa3284 2 года назад +1

    I have a third person and first person camera. What changes do I need to make to the code?

  • @Satscape
    @Satscape 4 года назад

    Congrats on 1K subscribers! They may have fixed this by now but, RaycastAll() may not return colliders in the order that they were hit, as I found out fairly recently... D'OH!

    • @samyam
      @samyam  4 года назад +1

      Thanks! And nope it's still unordered 😂

  • @TheKr0ckeR
    @TheKr0ckeR 3 года назад +1

    I mean I checked everywhere for clear 2d raycasting tutorial and this explains it in a best way! Plus, I have a nice example with interfaces now. Thank you. I think you can get more attractions if you add "Raycasting 2d/3d" with the title. :)
    Here is a question: After we select our object, I want to click some object and want it to go there. What way should i follow for that? I mean I can get the info of clicking now, but only clicked object should go there. :)

    • @samyam
      @samyam  3 года назад +1

      Thanks! I've added 2d in the title. Do you mean clicking 2 objects in a row? Well you'd need a manager or some script to store the currently clicked object, and then on the second click check if there's already a clicked object, and if there is, move to the location.

    • @TheKr0ckeR
      @TheKr0ckeR 3 года назад

      @@samyam Great! I'll do that with the new input system. So maybe i can create a temporary gameObject variable, and i can assign the "clicked" object to that. then i can do whatever i want? Is that true way to go or are there any better ways, I dont know how to check second click

    • @samyam
      @samyam  3 года назад

      That’s one way! Or you can use an interface and implement it on your gameobject, then use the position that was clicked and move there (you can pass the position through the interface or just read the mouse value separately)

    • @TheKr0ckeR
      @TheKr0ckeR 3 года назад

      @@samyam Like we did in the video? My game is cafe game that we a waitress, so we first click a waitress, then if it is selected, i click on table and waitress go there, for that scenario ,I should implement the interface on "table"? I couldn't understand the second logic, because i have already IClickable interface like we did in video! Thanks btw'

  • @fabioecco
    @fabioecco 2 года назад

    Great video!👏

  • @random_precision_software
    @random_precision_software 5 месяцев назад

    Hi, Im in the middle of converting my PC game to Xbox. I've got the mouse pointer moving with the Xbox controller sorted. BUT I've got a level selector which did use buttons on PC , but I can't get the raycast to work for the canvas, have you any ideas or do a quick tutorial? Thanks
    PS I'm using old input system.

  • @mrvladimirlenin
    @mrvladimirlenin 2 года назад

    ohh thanks! you helped me so much

  • @chris.davidoff
    @chris.davidoff 4 года назад +2

    Sweet video! The only thing I'd criticize is the example of non alloc. You're still allocating and destroying your raycast hits in your example. To make use of non-alloc, you should allocate the hits array as a private or public variable, in order to avoid the garbage collection

    • @samyam
      @samyam  4 года назад +2

      Great point! Slipped my mind for the tutorial 🙂

    • @chris.davidoff
      @chris.davidoff 4 года назад

      @@samyam Woah! Thanks for the pin :) and no worries, I'm trying to make my own tutorial and it's hard!!

  • @paganotommaso
    @paganotommaso 2 года назад

    Is there a method to make it on mobile? Because I don't want to create a custom cursor

    • @samyam
      @samyam  2 года назад +1

      This should work without the custom cursor, and just replace the mouse coordinates with touch coordinates

    • @paganotommaso
      @paganotommaso 2 года назад

      @@samyam thanks!

  • @drawfulanimations
    @drawfulanimations 3 года назад

    I'm going through this tutorial and everything works for me until I try to use the interface. Unity says this: "error CS8701: Target runtime doesn't support default interface implementation." I've tried to figure out what's wrong and how to fix it but It doesn't make sense.

    • @samyam
      @samyam  3 года назад

      What .Net core version does your project use? Try using the latest version

  • @rajveermishra8713
    @rajveermishra8713 3 года назад

    For me I have an error that controls at line 66 is being used wrong and idk how to fix it pls hel

    • @samyam
      @samyam  3 года назад

      Can you post the exact error code and that line of code here?

    • @rajveermishra8713
      @rajveermishra8713 3 года назад

      I found a different video but I'll still do it

  • @gejohans
    @gejohans 6 месяцев назад

    I am an elderly lady who wants to create a game. And this is the best guide I've seen for Unity. I hope they pay you!!! But I have a question. Suddenly in the film I see two new scripts. "Coins" and "Tower". I assume they are included from the Asset package. But I create my own "package".
    Should I have a script for each and every thing I click on that I want to do something with? Can they be in same script?
    How do I connect those scripts to my main script? How do they find each other?

    • @gejohans
      @gejohans 6 месяцев назад

      Um ... or ... is that the interface ??

    • @gejohans
      @gejohans 6 месяцев назад

      I got it. This is good.

    • @samyam
      @samyam  6 месяцев назад

      Nice, good luck! Here’s our Discord if you’d like to join. You can ask for help in the #help channel discord.gg/EyCzPbP5Gw

  • @donnyhudson4196
    @donnyhudson4196 3 года назад

    Awesome vids!!! Thanks. Was wondering how I might run something in the OnClickAction that might need to be in an FixedUpdate? Like moving a Rigidbody in a Parent when I click on a button.

    • @samyam
      @samyam  3 года назад +1

      The interface is just like a recipe for the script implementing it, so you can call it anywhere in the same way. But in this case do you need an interface for a Ui button? You can just implement the OnClick function for the UI button. Also, once you click the button you can set some values if you are doing a continuous moment,
      if buttonpressed, movementup = 1
      as an example. If it’s a continuous movement I recommend using an On Screen Button which is perfect for this occasion and pipes the value into a certain control you can then call. If it just needs to move to a certain location you can use a coroutine that starts once the button is pressed and ends once it reaches its destination. Hope that helps :)

    • @donnyhudson4196
      @donnyhudson4196 3 года назад

      Thanks for the fast reply. I've never used interfaces before...they seem interesting and usful. I built my own pirate ship and it has a couple buttons with colliders on a console...for moving the ship via a rigidbody. Forward button...stop button and reverse button. Forward button I wanted it to move smoothy forward at a constant speed...stop = no motion and reverse opposite of forward. I may be in over my head...lol. I'll have to read your reply over a few times. Also my first indever into this much physics. Player has a rigidbody also. Thanks again. It amazes me how much knowledge you have 💯👍

    • @donnyhudson4196
      @donnyhudson4196 3 года назад

      I did something last night that made the ship jump forward "not smoothly" lol. Then my player slid off the back of the boat...he doesn't move with the boat...lol. I'm seeing from searches this whole senerio may be perty difficult 🤔 Here's the script I put on my forward button so far. I'm just a beginner...lol. To make the ship move at a constant continuous speed until i hit the stop button it would have to be in the fixed update somehow right?
      public class Forward : MonoBehaviour, IClicked
      {
      public float speed = 10;
      public Rigidbody rbBoat;
      public Rigidbody rbPlayer;
      public void onClickAction()
      {
      // This makes the ship jump forward but only one quick jump
      rbBoat.velocity = transform.forward * speed;
      }
      //This makes the player shoot straight up in the air...lol
      private void FixedUpdate()
      {
      FollowShip();
      }
      public void FollowShip()
      {
      rbPlayer.velocity += rbBoat.velocity;
      }

      }

    • @samyam
      @samyam  3 года назад +1

      Yeah physics objects should be moved in FixedUpdate, also try using MovePosition function for moving rigidbody instead
      docs.unity3d.com/ScriptReference/Rigidbody.MovePosition.html

  • @noone-rh7pd
    @noone-rh7pd 3 года назад

    hello can you make a video on how to make a text appear when clicking on an object cause i've been looking all over for a solution and yours is close to what i want but i cant make it work yet

    • @samyam
      @samyam  3 года назад +1

      The concept should be the same as in this video, once you click on something check if it has the interface, and in your case make some text appear if it is clicked

    • @noone-rh7pd
      @noone-rh7pd 2 года назад

      @@samyam thanks i worked on it although i have a new problem
      I have this computer in my game and i want to connect it to other objects for example i dont want the other object clickable until i turn them on from the computer any idea how it works??

    • @samyam
      @samyam  2 года назад

      You can either have a manager keep track of what objects are connectable or have each individual object keep track of if they are connected. Then your interface implementation can check if the object is connected and do something with it.

  • @ariehuybregts9875
    @ariehuybregts9875 4 года назад

    Great tutorial, I really appreciated all the options, does the clicked function need to be public because of some characteristic of interfaces, or is it just so that it can be called from the mouse script?

    • @samyam
      @samyam  4 года назад +1

      All functions in an interface are public by default, since they are meant to be called
      jeremybytes.blogspot.com/2019/11/c-8-interfaces-public-private-and.html?m=1

    • @ariehuybregts9875
      @ariehuybregts9875 4 года назад

      @@samyam thanks for the response, and that blog post is really interesting

  • @luisalbertoherrera5439
    @luisalbertoherrera5439 4 года назад

    Great tutorial! How can I do this with an android project? I was thinking that I could reemplace or add something in the beding for reemplace the right clic to a touch action...

    • @samyam
      @samyam  4 года назад

      It's very similar but you would use the touch position in the input action to track the position and the touch contact to tell if there is something touching the screen (video on that coming soon)

    • @luisalbertoherrera5439
      @luisalbertoherrera5439 4 года назад

      @@samyam thank you for your response I will wait :)

  • @anglevertex1781
    @anglevertex1781 3 года назад

    How do I acces the mouse controls menu?

    • @samyam
      @samyam  3 года назад

      Do you mean the input action asset we created? It should be somewhere in your assets folder, wherever you created it.

  • @LimitedBits
    @LimitedBits 2 года назад

    Thanks, this was awesome! Have a coffee on me.

    • @samyam
      @samyam  2 года назад

      Hey JT Hale aw thank you so much! That's super appreciated

  • @ahmetfirat23
    @ahmetfirat23 3 года назад

    I don't understand how you reference cursorcontroller. When I write the same vs says create a new class.

    • @samyam
      @samyam  3 года назад

      What are you referring to with cursor controller?

    • @ahmetfirat23
      @ahmetfirat23 3 года назад

      @@samyam controls variable at 5:18 controls.mouse.position line.

  • @SaSeshen
    @SaSeshen 4 года назад

    Have you done a tutorial on how to make movement possible by using the new input system and clicking on the map? I don't mean the isometric tile map, because that is limited to the tile map, but to use it in a generic form that has a mesh, or collider so that you can ray cast your intended move position by right clicking.
    Essentially, if that made no sense, or if that was wrong, how do you make a movement system like League of Legends, where all movement is just clicking on the screen?

    • @samyam
      @samyam  4 года назад +1

      I haven’t made a video on that yet, but it should be very similar to what I showed in this video. I’ll add that to my list of potential future videos!

    • @SaSeshen
      @SaSeshen 4 года назад

      @@samyam I figured as much and I was able to replicate it somewhat, but the issue is that with the mouse Position or Delta, it doesn't initiate solely on a click, but is a constant activation, anytime the pointer moves. I am struggling to get the silly thing to ONLY work on a click, and setting the interaction to press only, only makes the code work once, when you click on the game tab. I am missing something very simple I am certain but that is why I am asking haha.
      Of course, the solution exists in spades with the old input system, but the point of using the new input system, is to not have to rely on the old one, as it more data intensive... that and I am trying to learn how to do this stuff "the correct way" using modular coding techniques. DOTS and ECS though... not sure I am quite ready for that since it is relearning, and reapplying Unity C# which I am already struggling with haha.

    • @samyam
      @samyam  4 года назад

      I would suggest using the performed callback for that action so you can call a function only when the mouse button has been pressed
      Example at around 6:00:
      ruclips.net/video/Y1BQoAmTC2s/видео.html

  • @tessacarstairs5998
    @tessacarstairs5998 2 года назад

    thank uuuuu! xD

  • @Gnenguin
    @Gnenguin 3 года назад

    You are an absolute fucking legend 💪

  • @alphaanar2651
    @alphaanar2651 4 года назад +1

    l see there great opportunity to make guns for MMORPG

    • @samyam
      @samyam  4 года назад +2

      How do you mean?

    • @alphaanar2651
      @alphaanar2651 4 года назад +1

      @@samyam if you click to the enemy enemy can get damage

    • @samyam
      @samyam  4 года назад +1

      Yeah that's a potential use case! Or any type of resource management game too

  • @kjlkjrlkjrlkjkl
    @kjlkjrlkjrlkjkl Год назад

    Thank you very much!