![]() In this way, we can easily see in our scene the ray created, here is an example. ![]() The ray is invisible, if we want to see it in the scene for debug purposes, then we need to use the method Debug.ray. Method to draw the ray in scene for debug purposeĭebug.DrawRay(transform.position, Vector2.right * laserLength, Color.red) Hit something, print the tag of the objectĭebug.Log("Hitting: " + ) ![]() If the collider of the object hit is not NUll RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.right, laserLength) Now let’s see a simple code to generate and debug a ray in our game. The second could be returned only if the first is ignored due to the configuration we applied. If we have 2 consecutive objects only the first object hit by the laser will be returned. It’s very important to notice that this version of the method will report only the first object hit by the laser according to the configuration. MaxDepth : Detect collisions only with objects with a z transform position lesser or equal of this value.MinDepth : Detect collisions only with objects with a z transform position greater or equal of this value.(By default if you don’t set this parameter Unity ignores the collisions with the layer “Ignore Raycast” as suggested by the name). LayerMask : In Unity you can assign a layer to a gameobject, with this mask we can decide which layers we want to report the collision with the ray, ignoring the others.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. Use as support for physics collision detection (check if an object is grounded for instance or to change the behavior of a platform if the player is underneath or above it).Define where an object like a missile or a bullet for instance will hit another object.Detect if a player or an enemy or an object in general sees something.Then why could a laser beam be useful for our game? The big advantage is that every object which makes contact with the laser beam can be reported! A Raycast is like a laser beam fired from a point along a particular direction as reported in the official documentation. What is Unity Raycast 2D? How do you use it in our game? Let’s answer the two important questions about it. I think as usual it will be me and you to solve another case. Wait a second! Guys, do you know what Unity Raycast is? Well! Hey Mike would you like a Unity Raycast? Today there are two friends here to talk about the Unity Raycast 2D.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |