Type the following code in the Update method in Player.cs. Drag and drop the Player script to “Player” in the Hierarchy.ĭouble-click on the Player script to open it. We can treat the camera as the eyes of the player. Rename Main Camera “Player” because the player will look through the lens of the camera. If the ray flying from the camera touches an object, we will identify the object. We will use the ray that shoots forward in the direction the camera is facing. To draw an image, Main Camera throws rays in multiple directions to find pixels to draw. We need to understand how to identify the button. Both ways involve looking (the looking logic). There are two ways to interact with the button. If you are a beginner and want to learn how to build virtual reality games, check out our Unit圓D course! In this tutorial, we will implement the button we made in our previous Unity tutorial. 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.In virtual reality (VR) games, one of the few interactions a player can make is press a button by looking at it. 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) GameObject bulletInstance = Instantiate(bulletPrefab, firePoint.position, Quaternion.identity) 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 |