void Shoot() { timer = 0f; gunAudio.Play(); gunLight.enabled = true; // Makes sure particles have stopped before playing them again gunParticles.Stop(); gunParticles.Play(); // Enable the line renderer at the end of the player's gun gunLine.enabled = true; gunLine.SetPosition(0, transform.position); // Ray starts at the end of the gun and points forward from the barrel shootRay.origin = transform.position; shootRay.direction = transform.up; // Checks if the raycast hits something in the set shootable layer if (Physics.Raycast(shootRay, out shootHit, currentGun.range, shootableMask)) { // If the ray hits something gets the health component EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth>(); // Make sure that it exists to avoid crashes when hitting things without health if (enemyHealth != null) { enemyHealth.TakeDamage(currentGun.damage, shootHit.point); } // Used to start the game PlayButton playButton = shootHit.collider.GetComponent <PlayButton>(); if (playButton != null) { playButton.StartGame(); } // Set the second position of the line renderer to the point the raycast hit gunLine.SetPosition(1, shootHit.point); } // If it didn't hit anything on the right layer else { gunLine.SetPosition(1, shootRay.origin + shootRay.direction * currentGun.range); } }