4. Shooting bullets, Laser Scope and Aiming Icon
To shoot bullets I created bullet prefab which contains bullet script and bullet object. Bullet script encapsulates bullet speed. In function Update() single line of code represents movement transform.Translate"Vector2.up * bulletSpeed * Time.deltaTime" and after short amount of time bullet gets destroyed. I would like to add that it's not efficient to put any collision detection inside the bullet class (we might have hundreds of bullets and if each one will check for collisions it will consume lot of computer resources). In game object each bullet have Sprite Renderer and Bullet script attached. Each time when player shoots "Input.GetMouseButtonDown(0)" Player script will instantiate bullet prefab with player position and rotation creating clone of bullet "tempBullet = (Instantiate(bullet, transform.position, transform.rotation)) as GameObject".
To detect collision of bullet there are two ways. First which I used is suitable for fast moving objects (depends how fast do you want your bullets to move). For fast moving objects (speed > 50) function "OnCollisionEnter2D()" is unreliable so my enemies are using "RaycastHit2D". It is function to draw invisible line between two objects and return true if something is on its way, I also used layer mask so line will collide only with objects placed on that layer e.g. walls or other enemies. My collision detection is if mouse over object and left mouse button pressed and ammo greater than zero and no collisions on the way - enemy was hit (you can also add range condition).
Second way to detect collisions is to use "OnCollisionEnter2D()" function for slow moving objects. In that case our bullet object needs extra elements like Rigidbody 2D() and Circle Collider 2D so we can add some physics to our object. This function also returns many useful information about collision like object tag we collide with or position of collision. I'm going to use it for ammo or if enemy will touch you.
In my game player have useful gadget Laser Scope attached to his guns. To do that I used Line Renderer. Line Renderer has many options like color, number of points line consist of, and shader. Starting point is player rotation and position transformed by some offset (so laser coming out of gun instead of center of player). End point is a returned vector of Raycasting against layer mask. Layer mask consists of three layers (walls, furniture and enemies) or together in binary values and convert to decimal e.g. layers 12, 13 and 14 or together as binary numbers will give us decimal 14336. To get nice laser effect I used material "Default-Particle" for both points. To set end point I used "lineRenderer.SetPosition(1, endPoint)" syntax where 1 is second point (0 is start point) and endPoint is returned vector of Raycasting collision.
To replace standard mouse cursor first I disabled it with "Screen.showCursor = false" and in FixedUpdate() function I transformed it position to mouse position with "Camera.main.ScreenToWorldPoint(Input.mousePosition)". In Unity we need new cursor object with desired sprite and cursor script attached.
Laser and cursor: