private void Shoot() { Sources[0].Play(); nextFire = Time.time + fireRate; StartCoroutine(ShotEffect()); RaycastHit hit; // Create a vector at the center of our camera's viewport Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f)); laserLine.SetPosition(0, gunEnd.position); // Executes if our ray hits something // rayOrigin is where our ray begins (in this case, center of our camera's viewpoint). // Our ray is being cast forward from our camera viewport, hence fpsCam.transform.forward. // For our hit, using the out keyword allows us to store additional information from // a function, in addition to its return type. // weaponRange is the distance in which we want to cast our ray. if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, weaponRange)) { Debug.Log(hit.collider.name); // Cast the second point of our ray to be what we hit laserLine.SetPosition(1, hit.point); // Reference to a health script attached to the collider we hit BossHealthBarController bossHealth = hit.collider.GetComponent <BossHealthBarController>(); // If there was a health script attached if (bossHealth != null) { // Call the damage function of that script, passing in our gunDamage variable bossHealth.Damage(gunDamage); Sources[1].Play(); } } else { // Cast our ray to end weaponRange units forward if we don't hit anything laserLine.SetPosition(1, rayOrigin + (fpsCam.transform.forward * weaponRange)); } }
// Use this for initialization void Awake() { bossHealthBar = this; }