[Godot3] How to Click 3D Objects in Godot | Game Dev Tutorial 8

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome friends in the last day of the dead tutorial video we learned how to make dynamic mouse cursors but now we need something to click in this tutorial I'll show you how to interact with 3d objects using the mouse so let's get to it I've already gone ahead and set up a new scene which includes a spatial game root node directional light a camera and a spatial node that will become the base node for our 3d sphere that we'll be clicking in our game I've also imported our tests sphere mesh and made it a child of the sphere spatial node but to be able to click our 3d object our 3d sphere mesh can't do the job on its own in fact the mesh node is only responsible for rendering our game object to the screen to detect if a 3d object has been clicked we need something akin to a hitbox in the Godot engine this can be done with an area node so I'll add an area note to our sphere when we first add an area node to our sphere you'll notice a configuration warning but not to worry this is just godot's way of telling us that we need to define the 3d shape of our area to fix this all we need to do is add a collision shape node as a child of this area node I'll do that and make Godot happy however we get another configuration warning just our luck but again don't worry about it in Godot collision shapes or hitboxes if you prefer are considered resource objects all we need to do to clear this warning is create a new collision shape in the collision shaped nodes shape property in the inspector there are quite a few shapes to pick from the shape that is right for you is largely dependent on the shape of the 3d object you want to be able to click since our test object is the sphere will choose the new sphere shaped resource each shape resource comes with its own set of customizable properties click on the shape resource to view these properties our sphere shape has a radius property which I'll change to match the radius of our objects 3d mesh now that we have all the required nodes in place click on the area node and then the node tab at the top of the inspector the area node has an input event signal built into it that will fire anytime the mouse interacts with any of its child collision shapes now our job is to connect to this signal to a custom script when this signal fires our code we'll check the type of mouse event like left clicking right clicking or double clicking and then running any desired functionality that we want to be associated with these events to start this will create a script on our 3d object space spatial node then we'll use the Godot editor to connect the area node signal to the script which will automatically generate a new function for us in the code when the signal fires it will pass this function quite a few useful properties but the property we're most interested in in this tutorial is the event in our function will first check if the past event type is a input event mouse button type if it is we'll use the input event mouse button objects properties to check what kind of mouse event occurred anytime you need information about how to use a Godot object or function you can press f4 to bring up a list of every object in the engine and get details about the properties and methods they own or if you want to know something specific you can highlight something in press shift f1 to find more details about it from the input event mouse button object will use the button index property to checked which mouse button was pressed and we'll use the pressed boolean to check if the button was just pressed or just released you can also use the double-click boolean to check if the area has just been you guessed it double clicked I'll use these properties to write some code that will print a message to the output console every time we left-click our sphere object now before we hit f6 and test our game there's one more important thing that's easy to miss by default our area note doesn't listen for a mouse picking events so we need to enable it to do so click on the area node and then in the inspector check the input ray pickable on box now when we run our game with f6 every time we click our sphere with the left mouse button we should get a printed message in the output console and here are a few more examples of various types of cliques being printed to the console of course we're not limited to just printing messages to the screen any logic you can dream up can be coupled to the events when you click a 3d object in this example I have three spheres rotating around each other whenever one of these spheres is left clicked a function is called to change that sphere to a new random color if you want to take a closer look for yourself this demo and the tutorial project can be downloaded from the Dave the dev github page linked below well that'll do for this video if you learn something useful consider hitting the like button and if you want to continue learning about how to use Godot and other open software to develop games be sure to subscribe and hit the bell icon to join this growing community hope to see in the next video until then happy Debi
Info
Channel: Dave the Dev
Views: 26,322
Rating: undefined out of 5
Keywords: Godot, Game Dev, Game Development, Game Dev Tutorials, Godot Tutorials, 3D Mouse Picking, Clicking 3D Objects
Id: U5qGj8qt7VU
Channel Id: undefined
Length: 6min 4sec (364 seconds)
Published: Mon Sep 23 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.