Distance : The coverage of the ray, you can think of it like the length of the ray.Origin : The starting point of the ray.Then we can see different parameters, we need to find out their scope. įirst clue, if this method returns a RaycastHit2D object it means the laser hit something, if this method returns null, then the laser has hit nothing. To be hit by the ray an object should have a collider 2D component, if you want more information about this great and extremely useful component please check the article Unity Collision Detection 2D Everything You Need To Know + Examples. We can immediately see that the method returns one object called RaycastHit2D and eventually it’s the object hit by the laser. public static RaycastHit2D Raycast(Vector2 origin, Vector2 direction, float distance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, float minDepth = -Mathf.Infinity, float maxDepth = Mathf.Infinity) We can see different versions of it, but let’s focus on the first one, we will talk about the others later in the article. I knew you would have noticed, using the Physics 2D library means we have to use it in the FixedUpdate for a good practice. The method we need to do all the magic is this one Physics2D.Raycast. ![]() So what are we waiting for? Let’s create our first laser beam to detect objects in our game! Physics 2D Raycast method We will cover some of these interesting cases together.
0 Comments
Leave a Reply. |