Ray-casting from the screen : Look at the mouse in 3D [GODOT]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone it's noe here and welcome to this video where I will teach you how to make a 3d object look instinctively at the mouse position in digital engine but this method is applicable anywhere the goal here is to get something that looks like this where a player object is looking where my mouse is pointing but first of all I will explain you the used method with pretty animations that I handcrafted with love let's imagine a simple 3d scene with some objects in it here we have a sphere and a cube next we will represent the view frustum of our camera which looks something like this now let's take a closer look at the camera we can see its origin represented by the purple dot and the viewport over there with the two objects displayed on it this is what will be seen on screen now let's say I click the side of the sphere on the viewport and I want to know where that point originally is in the 3d space what I can do is to cast a ray from the origin passing through the clicked point then when the ray continues it hits the sphere at some position and that position in 3d is what we were looking for the same can be applied to any position as long as the Ray hits a physical object at the end it should click empty space like the sky it will not work because the Ray will never hit something so back in Godot you want to make sure you have a ground plane so here I have a static body with a mesh instance and a collision shape then I have a player scene and here I have just a simple mesh instance make sure your mesh or object is facing towards the negative z-axis and then of course I have a camera and the viewer looks like this in your player script you want to set up a few variables so I have an already variable which gets essentially the camera node so I have a reference for later then I have a variable called ray origin which will be a vector free and another which is right and which is also a vector free next in the physics process function we want to define a new variable called space state and we set it to get world the direct space state basically we get the current physics state of the world next we create a new variable called mouse position and we set it to get viewport cat mouse position basically it's the mouse position on the screen or the viewport next we set the Ray origin variable to camera dot project ray origin of mouse position so basically on the camera we get the origin of the array so it's the same as the origin of the camera as I showed you on the animation next we set the array and variable to the Ray origin so we we go back to the camera origin and we add an order vector to that that vector is so camera dot project ray normal of Mouse position so basically this gives us a direction vector for the array according to where you clicked so that's a normalized vector so we want to multiply it by some high number so it's actually like long gray and when we add this to the array origin we get the array and if you don't quite understand that ask me down in the comments so anyways next we define a new variable called intersection and we set it to space State so space state that we defined previously and dot intersect array so this is a cool function on the physics state object that is basically a ray casting function so we cast away from the array origin to the array end and this whole thing returns dictionary with some info about the intersection or the head so if you click the sky for example the array will not hit anything so the dictionary will be empty so if it's not empty so we had something want to set the VAR path to intersection dog position so we get the position of the head next we want the player or the representation of the player to look at that position so I get my rig which is my representation and I use the function look at and I look at so a vector free of all the axes of the intersection position well if I do that look what happens my player or the object looks directly at the the mouse position or where the array hit the ground I don't want that I want the player to look parallel to the ground so instead of pause Y here I set it to my own translation Y so my own Y axis and this fixes the issue all right anyways the last argument to D look at function is a vector pointing upwards so here in my game pointing upwards means one on the y axis no e from the future here I want to explain you why we want the object to be facing towards the minus Z direction basically the look at function that we are using rotates the the object so that the local - the axis points towards the target position so that's why so this is the end I hope I helped you and if you have any questions ask them in the comments and I will try to reply and see you in the next video
Info
Channel: Nolkaloid
Views: 26,984
Rating: undefined out of 5
Keywords: godot, gamedev, raycast, camera, projection, tutorial, game engine, help, look at mouse, 3D
Id: mmvIkkKJVlQ
Channel Id: undefined
Length: 6min 37sec (397 seconds)
Published: Thu Apr 23 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.