RaycastHit2D itself is a structure and not a class so hit can't be null this means you have to check for the collider of a RaycastHit2D variable. Make sure the Layermask is set to ground only, otherwise it will detect other kinds of objects as well. The distance is 0.6f times the height of the character so the raycast won't give a hit when he hits the ground that is way lower than the ground he is standing on at the moment. The variable raycastRightPart is the right part of the character, so the raycast will happen at the right part of the character. RaycastHit2D hit = Physics2D.Raycast(raycastRightPart, Vector2.down, 0.6f * heightCharacter, LineOfSightMask) Public class Physics2dRaycast: MonoBehaviour You can see that it will work the same way as before, but right now our bullet is too fast, let’s decrease it’s speed so we can see it better that it only deals damage when it hits the enemy.You can use raycasts to check if an ai can walk without falling off the edge of a level. private void OnTriggerEnter2D(Collider2D collision)Įnemy enemy = () Next, go to the Bullet script on the OnCollisionEnter2D function and let’s get the Enemy component from the collider the Bullet is hitting at the moment, the same way the raycast did but now it doesn’t happen at the same time as we click the mouse button, this way we can have a bullet that travels way lower through the space and it only deals damage when it hits our enemy. if(hit) // If this is true then it hits something RaycastHit2D hit = Physics2D.Raycast(firePoint.position, firePoint.right) Specifying queryTriggerInteraction allows you to control whether or not Trigger. Usted puede opcionalmente proporcionar una LayerMask, para filtrar cualquier otro Collider que no le interese que genere colisiones. GameObject bulletInstance = Instantiate(bulletPrefab, firePoint.position, Quaternion.identity) Casts a ray, from point origin, in direction direction, of length maxDistance, against all colliders in the Scene. First things first, let’s comment out the piece of code from our Shoot function from the Gun script that casts a ray. Well we have everything pretty much ready for that. Now after all this, it is very easy to make our bullet deals damage to the enemy after touching them instead of using raycasts. Split each character spritesheet to generate the frames. I configured the Red Character’s Spritesheets to Point on Filter Mode, Multiple on Sprite Mode and 48 pixels per unit. I downloaded some free assets from herefor the characters. So, I will show you how you do both of these methods, but first let’s setup the project. This mode is more precise and faster than using prefabs because you instantly get the enemy if the ray hits it at that moment. Point B being where the ray hits on the collider) directly where the gun is aiming at and if the ray hits an enemy collider then we can deal damage. Using this method, instead of instantiating anything, we will cast a ray (basically a line from point A to point B. We can instantiate prefabs from the fire point coming from the player and check if the instance collides with an enemy to deal damage, in this method the bullets can take a little time until it reaches the target, you can set its velocity but you cant put too fast so you dont break collision handling, also there is seconds or miliseconds that the enemy can escape the bullets. But if you want to see the bullets in the air, even for a fraction of a second, until it hits the target then using prefabs is a better way to achieve that. If you want a bullet to immediately hit the target you are aiming then maybe raycast is the best way to go. Using Prefabs or Raycasts will do to make it, you can choose one of them based on what you want to achieve in your game. Here is a simple walkthrough to make a simple 2D shooting mechanic.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |