11/15/2023 0 Comments Unity raycast tutorial![]() Input Action in the Inspector - New Input System Level Builder - From Pixels to Playable Level So instead, when the mouse button comes up we can set the position to the last point hit by the raycast as well as setting the selectedObject variable to null - allowing us to select a new object. If we left it like this and let go of the mouse button the object would stay levitated above the ground. We now move the selected object to the point hit by the ray cast, plus for just for fun, we move it up a bit as well. ![]() Just like the first raycast this one goes from the camera to the mouse, but it makes use of the second layer mask, which allows the ray to go through the selected object and hit the ground. Our second raycast happens when the left mouse button is held down AND the selected object is NOT null. ![]() Note the use of the “object” layer mask in the raycast function. If both are true, we’ll use a raycast just like the last example to store a reference to the transform of the object we clicked on. To get our selected object, we’ll first check if the left mouse button has been clicked and if the selected object is currently null. To make this all work, we’re also going to need variables to keep track of the selected object (Transform) and the last point hit by the raycast (Vector3). There is also a duration parameter, which is how long the line is drawn in seconds which can be particularly helpful when raycasting is only done for one frame at time. The DrawLine function does just as it says drawing a line from one point to another. When getting started with raycasting it is SUPER easy to get rays going in the wrong direction or maybe not going far enough. Notice that I’ve also added the function Debug.DrawLine. The caching is so when we release the mouse button we can restore the color to the correct material on the correct mesh renderer. If we hit an object on our selectable layer, we can cache the mesh renderer and the color of the first material. With our ray created we can add our raycast function, using the created ray, a RaycastHit, a reasonable distance, and our layer mask. Thankfully Unity has given us a nice built function that does this for us! If the button is pressed we’ll need to raycast and in this case, we need to create a ray from the camera to the mouse position. Then if and else if statements check for the left mouse button being pressed and released, respectively. Flipping back into Unity the value of the layer mask can be set to “selectable.” Then in the script, I created a private serialized field of the type layer mask. I can’t tell you how many hours I’ve spent trying to debug raycasting only to find I forgot to put a collider on an object. This means that for raycasting to hit or find an object, that object needs to have a collider or a trigger on it. The final and really important tidbit is that raycasting is part of the physics engine. It’s also worth noting that the code for all the raycasting in the following examples, except for the jumping example, can be put on any object in the scene, whether that is the player or maybe some form of manager. The examples here are all going to be 3D, if you are working on a 2D project the ideas and concepts are nearly identical - with the biggest difference being that the code implementation is a tad different. ![]() So… It’s pretty useful and a tool you should have in your game development toolbox. When something is found, we can get all kinds of info about that object and have access You can think of it as something like a long stick used to poke and prod around a scene. Raycasting is a lightweight and performant way to reach out into a scene and see what objects are in a given direction. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |