//fire the weapon using a raycast void Shoot() { //calculate direction to shoot Vector3 rayDirection = cameraController.transform.forward; //cast a debug ray UnityEngine.Debug.DrawRay(rayOrigin.position, rayDirection * shootDistance, Color.blue, 1f); //do the raycast if (Physics.Raycast(rayOrigin.position, rayDirection, out objectHit, shootDistance)) { //do stuff when hitting an object UnityEngine.Debug.Log("You hit the " + objectHit.transform.name + "!"); //visual feedback visualFeedbackObject.transform.position = objectHit.point; //apply damage to enemy EnemyShooter enemyShooter = objectHit.transform.gameObject.GetComponent <EnemyShooter>(); if (enemyShooter != null) { enemyShooter.TakeDamage(weaponDamage); enemyShooter.TakeKnockback(weaponKnockback); } } else { //do stuff when missing objects UnityEngine.Debug.Log("Miss"); } }
// fire the weapon using a raycast void Shoot() { // calculate direction to shoot the ray Vector3 rayDirection = cameraController.transform.forward; // cast a debug ray Debug.DrawRay(rayOrigin.position, rayDirection * shootDistance, Color.cyan, 1f); // do the raycast if (Physics.Raycast(rayOrigin.position, rayDirection, out objectHit, shootDistance, hitLayers)) { // Hit thing DoStuff() code Debug.Log("You HIT the " + objectHit.transform.name); if (objectHit.transform.tag == "Enemy") { Debug.Log("DEAL DAMAGE"); // Visual Feedback visualFeedbackObject.transform.position = objectHit.point; // Apply damage if it's enemy EnemyShooter enemyShooter = objectHit.transform.gameObject.GetComponent <EnemyShooter>(); if (enemyShooter != null) { enemyShooter.TakeDamage(weaponDamage); } } } else { Debug.Log("Miss."); } }
void Shoot() { //aim Vector3 rayDirection = cameraController.transform.forward; //feedback muzzleFlash.Play(); AudioHelper.PlayClip2D(fireSound, 1f); //debug ray Debug.DrawRay(rayOrigin.position, rayDirection * shootDistance, Color.cyan, 1f); //raycast check if (Physics.Raycast(rayOrigin.position, rayDirection, out objectHit, shootDistance, hitLayers)) { UnityEngine.Debug.Log(objectHit.transform.name); ImpactFlash(); //apply damage if object is enemy if (objectHit.transform.tag == "Enemy") { EnemyShooter enemyShooter = objectHit.transform.GetComponent <EnemyShooter>(); if (enemyShooter != null) { enemyShooter.TakeDamage(primaryWeaponDamage); } } } else { UnityEngine.Debug.Log("miss"); } }
void HitObject() { Vector3 rayDirection = cameraController.transform.forward; Debug.DrawRay(rayOrigin.position, rayDirection * shootDistance, Color.red, 0.5f); if (Physics.Raycast(rayOrigin.position, rayDirection, out objectHit, shootDistance, hitLayers)) { EnemyShooter enemyShooter = objectHit.transform.GetComponent <EnemyShooter>(); if (enemyShooter != null) { enemyShooter.TakeDamage(punchDamage); } } }
//on trigger enter void OnTriggerEnter() { positionHolder = this.gameObject.transform.position; AudioHelper.PlayClip2D(explosionSound, 1f); GameObject explosionInstance = Instantiate(explosion, positionHolder, Quaternion.identity); Destroy(explosionInstance, 3.5f); Collider[] hitColliders = Physics.OverlapSphere(positionHolder, hitRadius, hitLayers); foreach (var hitCollider in hitColliders) { EnemyShooter shooter = hitCollider.gameObject.GetComponent <EnemyShooter>(); if (shooter != null) { shooter.TakeDamage(grenadeDamage); } } //do visuals and sound //destroy Destroy(this.gameObject, 4f); }
//Shoot gun using raycast void Shoot() { //calc direction to shoot Vector3 rayDirection = cameraController.transform.forward; //cast debug ray Debug.DrawRay(rayOrigin.position, rayDirection * shootDistance, Color.blue, 1f); //do raycast if (Physics.Raycast(rayOrigin.position, rayDirection, out objectHit, shootDistance, hitLayers) && (PlayerHealth.health > 0)) { Debug.Log("You Hit the " + objectHit.transform.name); if (objectHit.transform.tag == "Enemy") { Debug.Log("deal damage"); //visual feedback visualFeedbackObject.transform.position = objectHit.point; //apply damage if enemy EnemyShooter enemyShooter = objectHit.transform.gameObject.GetComponent <EnemyShooter>(); if (enemyShooter != null) { enemyShooter.TakeDamage(weaponDamage); _enemyHit.Play(); Debug.Log("Hit enemy"); } } if (objectHit.transform.tag == "Wall") { _hitWall.Play(); Debug.Log("Hit wall"); } //check health, kill enemy } else { Debug.Log("Miss"); } }
// fire weapon using raycast void Shoot() { Vector3 rayDirection = cameraController.transform.forward; // cast ray Debug.DrawRay(rayOrigin.position, rayDirection * shootDistance, Color.blue, 1f); // do raycast if (Physics.Raycast(rayOrigin.position, rayDirection, out objectHit, shootDistance)) { EnemyShooter enemyShooter = objectHit.transform.gameObject.GetComponent <EnemyShooter>(); if (enemyShooter != null) { Debug.Log("Hit enemy!"); enemyShooter.TakeDamage(weaponDamage); } } else { Debug.Log("Missed!"); } }
void ShootLaserFromTargetPositionL(Vector3 targetPosition, Vector3 direction, float length) { Ray ray = new Ray(targetPosition, direction); RaycastHit raycastHit; Vector3 endPosition = targetPosition + (length * direction); if (grabAction1L.GetStateDown(handTypeL)) { Sound.PlayOneShot(soundReload); if (Time.time - aowailop2 >= ti) { if (AmmoL > 0) { float seconds = (float)900 / 9000f; vibra.Execute(0, seconds, 20f / seconds, 20, handTypeL); laserLineRendererL.enabled = true; Sound.PlayOneShot(soundShoot); particleL.Emit(1); if (Physics.Raycast(ray, out raycastHit, length)) { if (raycastHit.transform.tag == "Enemy") { if (raycastHit.transform.name == "head") { EnemyShooter target = raycastHit.transform.GetComponentInChildren <EnemyShooter>(); if (target != null) { Quaternion rot = Quaternion.FromToRotation(Vector3.forward, raycastHit.normal); GameObject particleGun2 = Instantiate(blood, raycastHit.point, rot); Destroy(particleGun2, 2f); target.TakeDamage(100); print("001"); } } else { EnemyShooter target = raycastHit.transform.GetComponent <EnemyShooter>(); if (target != null) { Quaternion rot = Quaternion.FromToRotation(Vector3.forward, raycastHit.normal); GameObject particleGun2 = Instantiate(blood, raycastHit.point, rot); Destroy(particleGun2, 2f); target.TakeDamage(ShootDamage); } } } if (raycastHit.transform.name == "Militarytarget") { raycastHit.rigidbody.AddForce(-raycastHit.normal * 50f); } if (raycastHit.transform.tag == "headAi") { if (raycastHit.transform.name == "head") { ScriptSitShoot target = raycastHit.transform.GetComponentInChildren <ScriptSitShoot>(); if (target != null) { Quaternion rot = Quaternion.FromToRotation(Vector3.forward, raycastHit.normal); GameObject particleGun2 = Instantiate(blood, raycastHit.point, rot); Destroy(particleGun2, 2f); target.TakeDamage(100); print("001"); } } else { ScriptSitShoot target = raycastHit.transform.GetComponent <ScriptSitShoot>(); if (target != null) { Quaternion rot = Quaternion.FromToRotation(Vector3.forward, raycastHit.normal); GameObject particleGun2 = Instantiate(blood, raycastHit.point, rot); Destroy(particleGun2, 2f); target.TakeDamage(ShootDamage); } } } if (raycastHit.transform.tag == "Bomb") { ScriptBome target = raycastHit.transform.GetComponent <ScriptBome>(); if (target != null) { Quaternion rot = Quaternion.FromToRotation(Vector3.forward, raycastHit.normal); GameObject particleGun2 = Instantiate(blood, raycastHit.point, rot); Destroy(particleGun2, 2f); target.TakeDamage(ShootDamage); } } if (raycastHit.transform.tag == "Bossfinal") { ScriptHeadShot target = raycastHit.transform.GetComponent <ScriptHeadShot>(); if (target != null) { Quaternion rot = Quaternion.FromToRotation(Vector3.forward, raycastHit.normal); GameObject particleGun2 = Instantiate(blood, raycastHit.point, rot); Destroy(particleGun2, 2f); target.TakeDamage(10); } } if (raycastHit.transform.tag == "StartGame") { OpenDoor target = raycastHit.transform.GetComponent <OpenDoor>(); if (target != null) { Quaternion rot = Quaternion.FromToRotation(Vector3.forward, raycastHit.normal); GameObject particleGun2 = Instantiate(particleGun, raycastHit.point, rot); Destroy(particleGun2, 2f); target.TakeDamage(ShootDamage); } } if (raycastHit.transform.tag == "lobot") { EnemyShooter2 target = raycastHit.transform.GetComponent <EnemyShooter2>(); if (target != null) { Quaternion rot = Quaternion.FromToRotation(Vector3.forward, raycastHit.normal); GameObject particleGun2 = Instantiate(blood, raycastHit.point, rot); Destroy(particleGun2, 2f); target.TakeDamage(ShootDamage); } } if (raycastHit.transform.tag == "newgame") { SceneManager.LoadScene(0); } if (raycastHit.transform.tag == "hp") { reHp target = raycastHit.transform.GetComponent <reHp>(); if (target != null) { Quaternion rot = Quaternion.FromToRotation(Vector3.forward, raycastHit.normal); GameObject particleGun2 = Instantiate(particleGun, raycastHit.point, rot); Sound.PlayOneShot(soundReHp, 30f); Destroy(particleGun2, 2f); ScriptHPStam.currentHP += 100; target.TakeDamage(500); } } if (raycastHit.transform.tag == "Bullet") { EnemyBullet target = raycastHit.transform.GetComponent <EnemyBullet>(); if (target != null) { Quaternion rot = Quaternion.FromToRotation(Vector3.forward, raycastHit.normal); GameObject particleGun2 = Instantiate(particleGun, raycastHit.point, rot); Destroy(particleGun2, 2f); target.TakeDamage(ShootDamage); } } if (raycastHit.transform.tag == "StartGame") { Sound.PlayOneShot(soundReHp, 30f); StartCoroutine(stargame()); } AmmoL--; } else { print("Empty"); } endPosition = raycastHit.point; } aowailop2 = Time.time; } laserLineRendererL.SetPosition(0, targetPosition); laserLineRendererL.SetPosition(1, endPosition); } }