Selecting Objects in the 3D World with the Mouse Using Raycast (Unity Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys today we're going to look at how we can select a target in a 3d world using the mouse as always if you prefer written instructions you can find a link to the text version in the description if you find this tutorial useful please don't forget to click like and subscribe right we're going to start with the scene we created in our colliding with obstacles tutorial we've got a ball and a stack of boxes that all have a rigid body component attached so that they react to forces and collisions we're going to add the ability to select a target with the mouse and then fire the ball at the selected target when the left mouse button is clicked the first thing we're going to do is make the cardboard box change color when the mouse cursor is over it so the player knows which box they are targeting to do this we'll go to the project panel and navigate to assets crow art pbr cardboard box prefabs and we'll select the cardboard box prefab we'll add a script to the prefab by clicking the add component button and searching for the script component we'll call this script target adding the script to the prefab of the cardboard box will apply it to all of the boxes in our scene we'll double click the script to open it in visual studio we want to change the albedo color of the material when the mouse is over the box in the same way as we did in our changing the color of a material tutorial to get access to the material we need to get the renderer component so we'll create a private field for this then we'll get the renderer component in the start method and assign it to the field we'll then make use of the on mouse enter method which will be called when the mouse is over the box in here we'll access the material of the renderer and set the colour to red we'll then create the onmouse exit method which will be called when the mouse moves off the box in here we'll set the material back to its original appearance by accessing the material of the renderer and setting the colour to white let's save the script and switch back to unity let's press play to try this out now when we move the mouse over one of the boxes it turns red and when we move the mouse away it returns to its original color the next thing we want to do is fire the ball at the target to do this we'll select the ball in the hierarchy and add a new script component we'll call this script target selector we'll double-click the script to open it in visual studio we want to fire the ball at the target when the left mouse button is clicked so the first thing we'll do is check for this in the update method if the mouse button has been clicked the next thing we need to do is find out where exactly the mouse is pointing so we can fire the ball in this direction to do this we'll need to access our main camera so we'll add a public field for this we'll then use the screen point array method to create a ray that goes from the camera through the position in the world that the mouse is pointing you can think of a ray as a line that is emitted from a source in a particular direction in this case the source is the camera and the direction is determined by the position of the mouse cursor we can now use the physics raycast method to see if the ray passes through anything if the ray has hit something we need to check that it is one of our targets to do this we'll check to see if the game object the ray has collided with has a target script component if it does we can then calculate the distance between the collision point and our current position we'll then normalize this to get the direction we want the ball to travel the final step is to apply a force in this direction to apply the force we need access to the rigid body component of the ball we'll add a private field to hold this then we'll get the rigidbody component in the start method and assign it to this field we'll also add a public field for the desired force size now we can apply the force to the rigid body in the desired direction we'll specify the force mode as impulse so that the force is applied instantly let's save the script and switch back to unity we'll select the ball in the hierarchy then we'll drag our camera from the hierarchy into the camera field of our script in the inspector we'll set the force size to 10 and then press play now we can select a target and click the left mouse button to fire the ball in that direction also if you've watched our tutorial on slow motion we can add this in now we can have the ball knock down the boxes in slow motion okay that covers everything for this tutorial hope you found it useful please leave any questions or feedback in the comments and don't forget to subscribe so you don't miss the next one thanks guys
Info
Channel: Ketra Games
Views: 35,625
Rating: undefined out of 5
Keywords: Select Object in Unity with Mouse, Select Object, Select Object with Mouse, Raycast, ScreenPointToRay, Unity, Unity Tutorial, Unity3d, Development, Programming, Coding, C#, Unity3d Tutorial, Learn Unity, Indie Game Development, Learn Unity3d
Id: fw7h3UBgNW4
Channel Id: undefined
Length: 6min 19sec (379 seconds)
Published: Fri Nov 27 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.