void Shoot() { _muzzleFlash.SetActive(true); currentAmmo--; _uiManager.UpdateAmmo(currentAmmo); if (_weaponAudio.isPlaying == false) { _weaponAudio.Play(); } Ray rayOrigin = Camera.main.ViewportPointToRay(new Vector3(.5f, .5f, 0)); RaycastHit hitInfo; if (Physics.Raycast(rayOrigin, out hitInfo)) { Debug.Log("Hit: " + hitInfo.transform.name); GameObject hitMarket = Instantiate(_hitMarkerPrefab, hitInfo.point, Quaternion.LookRotation(hitInfo.normal)) as GameObject; Destroy(hitMarket, 1f); Destructible crate = hitInfo.transform.GetComponent <Destructible>(); if (crate != null) { crate.DestroyCrate(); } } }
private void ShootOrActivate() { // Shoot a ray (for a raycast) from the center of the ViewPort (0.5f to 0.5f) Ray rayOrigin = GetComponent <Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f)); // Turn on the muzzle flash, subtract ammo, play shooting sound _muzzelFlash.SetActive(true); _currentAmmunation--; _uiManager.UpdateAmmunation(_currentAmmunation); if (!_audioSource.isPlaying) { _audioSource.Play(); } // Cast meaningful rays RaycastHit hitInfo; if (Physics.Raycast(rayOrigin, out hitInfo)) { Instantiate(_hitMarker, hitInfo.point, Quaternion.LookRotation(hitInfo.normal)); Destructible crate = hitInfo.transform.GetComponent <Destructible>(); if (crate != null) { crate.DestroyCrate(); } } }
void Shoot() { muzzleFlash.SetActive(true); currentAmmo--; uIManager.UpdateAmmo(currentAmmo); //if audio is not playing if (weaponAudio.isPlaying == false) { weaponAudio.Play(); } //Cross_hair shooting Ray rayOrigin = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0)); RaycastHit hitInfo; if (Physics.Raycast(rayOrigin, out hitInfo)) { Debug.Log("Ray cast hit something: " + hitInfo.transform.name); GameObject hitMarker = (GameObject)Instantiate(hitMarkerPrefab, hitInfo.point, Quaternion.LookRotation(hitInfo.normal)); Destroy(hitMarker, 1f); Destructible crate = hitInfo.transform.GetComponent <Destructible>(); if (crate != null) { crate.DestroyCrate(); } } }
void Shoot() { currentAmmoCount--; _uiManager.UpdateAmmo(currentAmmoCount); //Viewport width and height goes from 0 to 1, so zenter is 0.5 Vector3 viewPortCenter = new Vector3(0.5f, 0.5f, 0.0f); Ray rayOrigin = Camera.main.ViewportPointToRay(viewPortCenter); RaycastHit hitInfo; if (Physics.Raycast(rayOrigin, out hitInfo, 100.0f)) { //Type Casting: (Don't have to do this in this case, but typecast to change to a different (matching) data type) //GameObject hitmarkerClone = Instantiate(_hitmarkerPrefab, hitInfo.point, Quaternion.LookRotation(hitInfo.normal)) as GameObject; //GameObject hitmarkerClone = (GameObject)Instantiate(_hitmarkerPrefab, hitInfo.point, Quaternion.LookRotation(hitInfo.normal)); if (hitInfo.transform.name != "Sharkman") { GameObject hitmarkerClone = Instantiate(_hitmarkerPrefab, hitInfo.point, Quaternion.LookRotation(hitInfo.normal)); Destroy(hitmarkerClone, 1.0f); } Destructible crate = hitInfo.transform.GetComponent <Destructible>(); if (crate != null) { crate.DestroyCrate(); } } }
void shoot() { //check for audio if (_weaponAudio.isPlaying == false) { _weaponAudio.Play(); } currentAmmino--; _uiManager.UpdateAmmo(currentAmmino); _muzzle.SetActive(true); Ray rayOrigin = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0)); RaycastHit hitInfo; if (Physics.Raycast(rayOrigin, out hitInfo)) { Debug.Log("Hit: " + hitInfo.transform.name); GameObject hitmarker = Instantiate(_hitMarker, hitInfo.point, Quaternion.LookRotation(hitInfo.normal)) as GameObject; //TypeCasting Destroy(hitmarker, 1f); //check for destruction Destructible crate = hitInfo.transform.GetComponent <Destructible>(); if (crate != null) { crate.DestroyCrate(); } } }
void Shoot() { // decrease ammo --_currentAmmo; _uiManager.UpdateAmmo(_currentAmmo); // play the muzzle flash _muzzleFlash.SetActive(true); // play the shoot sound _shootSound.Play(); // cast ray from center of camera Ray origin = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0)); RaycastHit hitInfo; // check what you hit if (Physics.Raycast(origin, out hitInfo)) { Debug.Log("Raycast hit " + hitInfo.transform.name); // create hitMarker and destroy after one second GameObject hitMarker = Instantiate(_hitMarker, hitInfo.point, Quaternion.LookRotation(hitInfo.normal)); Destroy(hitMarker, 1f); // if you shoot a crate, destroy it Destructible crate = hitInfo.transform.GetComponent <Destructible>(); if (crate != null) { crate.DestroyCrate(); } } }
public void Fire() { if (Input.GetMouseButton(0) && _currentAmmo > 0) { _currentAmmo--; _uiManager.UpdateAmmo(_currentAmmo); if (!_shootAudio.isPlaying) { _shootAudio.Play(); } Ray origin = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0.0f)); RaycastHit hitInfo; if (Physics.Raycast(origin, out hitInfo)) { GameObject hit = Instantiate(_hitMarkerPrefab, hitInfo.point, Quaternion.LookRotation(hitInfo.normal)); Destroy(hit, 1); if (hitInfo.transform.CompareTag("Crate")) { Destructible d = hitInfo.transform.GetComponent <Destructible>(); d.DestroyCrate(); } } _muzzleFlash.SetActive(true); } else { _shootAudio.Stop(); _muzzleFlash.SetActive(false); } }
private void Shoot() { Ray ray = Camera.main.ViewportPointToRay(new Vector3(.5f, .5f, 0)); RaycastHit hit; _muzzleFlash.SetActive(true); _currentAmmo--; _uiManager.UpdateAmmoCount(_currentAmmo); if (!_audioSource.isPlaying) { _audioSource.Play(); } if (Physics.Raycast(ray, out hit, Mathf.Infinity)) { Debug.Log("Hit: " + hit.transform.name); GameObject hitMarkers = Instantiate(_hitMarkerPrefab, hit.point, Quaternion.LookRotation(hit.normal)) as GameObject; Destroy(hitMarkers, 1.0f); Destructible create = hit.transform.GetComponent <Destructible>(); if (create != null) { create.DestroyCrate(); } } }