public void Explode() { // call the shake function on the cameraShake script to shake the camera. // StartCoroutine(cameraShake.Shake (shakeDuration, shakeMagnitude)); // an array of colliders within the blast radius. Collider [] collidersToDamage = Physics.OverlapSphere(transform.position, blastRadius); // For each collider within the blast radius get the health script and apply damage. foreach (Collider nearObjects in collidersToDamage) { ObjectHealth objectHealth = nearObjects.GetComponent <ObjectHealth>(); if (objectHealth != null) { objectHealth.TakeDamage(blastDamage); } } // an array of colliders within the blast radius. Collider [] collidersToMove = Physics.OverlapSphere(transform.position, blastRadius); // For each collider within the blast radius get their rigibody and apply force. foreach (Collider nearObjects in collidersToMove) { Rigidbody rigidbody = nearObjects.GetComponent <Rigidbody>(); if (rigidbody != null) { rigidbody.AddExplosionForce(blastForce, transform.position, blastRadius, 1, ForceMode.Impulse); } } }
private void OnTriggerEnter(Collider other) { var randomDamage = Random.Range(0, 99); //몬스터에게 Damage if (hitTarget == HitTarget.ENEMY) { enemyMng.EnemyDamage(other.gameObject, damage + randomDamage); } //Player에게 Damage else { playerHealth.TakeDamage(damage + randomDamage); } }
private void OnTriggerEnter(Collider other) { ObjectHealth targetHealth = other.GetComponent <ObjectHealth>(); CarHealth carHealth = other.GetComponent <CarHealth>(); if (targetHealth) { targetHealth.TakeDamage(m_Damage); } else if (carHealth) { carHealth.TakeDamage(m_Damage); } Destroy(gameObject); }
public IEnumerator attackBehavior() { Debug.Log("Attacking"); RaycastHit attackHit = new RaycastHit(); Vector3 attackDirection = playerTransform.position - transform.position; if (Vector3.Angle(attackDirection, transform.forward) < attackAngle) { if (Physics.Raycast(transform.position, attackDirection, out attackHit, attackRange)) { ObjectHealth objectHealth = attackHit.collider.GetComponent <ObjectHealth>(); if (objectHealth != null) { objectHealth.TakeDamage(attackDamage, attackHit.point); } } } yield return(new WaitForSeconds(1.5f)); }
void Fire() { approxDeltaShotTime = System.Math.Round((Mathf.Abs(lastShot - Time.time)), 2); if ((approxDeltaShotTime - lockTime) < 0.1f) { return; } //Instantiates the prefab at the CasingSpawner loc, and sets it as a child of the handgun casing = Instantiate(prefabCasing) as GameObject; casing.transform.SetParent(this.transform, true); casing.transform.position = ejectionLoc; //Sends a reference of the just spawned casing to the GlobalBehavior Script for tracking GlobalBehavior.CasingSpawned(casing); //setup the rigidbody details for the casing casingRB = casing.GetComponent <Rigidbody>(); Vector3 casingCOM = Vector3.zero; casingCOM.y = 0.008f; casingRB.centerOfMass = casingCOM; //Sets the ejection angle, then adds a bit of randomness to it. Vector3 ejectionVelocity; ejectionVelocity = (-transform.forward + (0.8f * transform.right)) * 1.25f; ejectionVelocity.y = 1f; Vector3 randVelocity = Random.insideUnitSphere; ejectionVelocity += (randVelocity * 0.4f); //Makes the casing spin a bit Vector3 randAngularVelocity = Random.insideUnitSphere; randAngularVelocity.x = -500; //Sets the ejection velocity to a random Vec3 times the multiplier casingRB.AddForce(ejectionVelocity * ejectionMult); casingRB.AddRelativeTorque(randAngularVelocity * 10, ForceMode.VelocityChange); casing.transform.SetParent(null, true); //plays the GunSmoke particlesystem smoke.Play(); if (pelletGO != null) { pellets.Play(); } /* temp comment out for testing */ // gunLine.enabled = true; gunLine.SetPosition(0, transform.position); // Set the shootRay so that it starts at the end of the gun and points forward from the barrel. shootRay.origin = transform.position; shootRay.direction = transform.forward; if (Physics.Raycast(shootRay, out shootHit, 50f, shootableMask)) { ObjectHealth objectHealth = shootHit.collider.GetComponent <ObjectHealth>(); if (objectHealth != null) { objectHealth.TakeDamage(damageAmt, shootHit.point); } gunLine.SetPosition(1, shootHit.point); } else { gunLine.SetPosition(1, shootRay.origin + shootRay.direction * 50f); } //Sets the time of the last time this method was called lastShot = Time.time; }
public void Shoot() { currentAmmo--; //Instantiate muzzleFlash at barrel position. if (barrel) { var muzzleFlashInstance = Instantiate(muzzleFlash, barrel.transform.position, barrel.transform.rotation) as GameObject; Destroy(muzzleFlashInstance, 4); } // Instantiate spent bullet shells at shellEjectPosition. if (shell && shellEjectSpot) { Vector3 shellEjectPosition = shellEjectSpot.position; Quaternion shellEjectRotation = shellEjectSpot.rotation; GameObject shellInstance = Instantiate(shell, shellEjectPosition, shellEjectRotation); if (shellInstance.GetComponent <Rigidbody>()) { Rigidbody rigidB = shellInstance.GetComponent <Rigidbody>(); rigidB.AddForce(shellEjectSpot.forward * shellEjectSpeed, ForceMode.Impulse); } Destroy(shellInstance, 7); } //Instantiate bullet object at barrel position. if (bulletPrefab && barrel) { Instantiate(bulletPrefab, barrel.transform.position, barrel.transform.rotation); } //Play shooting sound. if (shootingSound.Length > 0) { if (audio) { audio.PlayOneShot(shootingSound [Random.Range(0, shootingSound.Length)]); } } //Raycast is instantiated at barrel position. RaycastHit hit; if (Physics.Raycast(barrel.transform.position, barrel.transform.forward, out hit, range)) { // Debug.Log(hit.transform.name); //Cause damage to object health. ObjectHealth objecthealth = hit.transform.GetComponent <ObjectHealth>(); if (objecthealth != null && objecthealth.isAlive == true) { objecthealth.TakeDamage(damage); } //Playes impact sounds, via MyHitSounds script on object hit by the raycast. MaterialType materialType = hit.transform.GetComponent <MaterialType>(); MyHitSounds myHitSounds = GetComponentInChildren <MyHitSounds>(); if (materialType != null && myHitSounds != null) { if (materialType.TypeOfMaterial == MaterialType.MaterialTypeEnum.Brick) { var clips = myHitSounds.Brick [UnityEngine.Random.Range(0, myHitSounds.Brick.Length)]; AudioSource.PlayClipAtPoint(clips, hit.point); } if (materialType.TypeOfMaterial == MaterialType.MaterialTypeEnum.Concrete) { var clips = myHitSounds.Concrete [UnityEngine.Random.Range(0, myHitSounds.Concrete.Length)]; AudioSource.PlayClipAtPoint(clips, hit.point); } if (materialType.TypeOfMaterial == MaterialType.MaterialTypeEnum.Dirt) { var clips = myHitSounds.Dirt [UnityEngine.Random.Range(0, myHitSounds.Dirt.Length)]; AudioSource.PlayClipAtPoint(clips, hit.point); } if (materialType.TypeOfMaterial == MaterialType.MaterialTypeEnum.Folliage) { var clips = myHitSounds.Folliage [UnityEngine.Random.Range(0, myHitSounds.Folliage.Length)]; AudioSource.PlayClipAtPoint(clips, hit.point); } /* if (materialType.TypeOfMaterial == MaterialType.MaterialTypeEnum.Glass) * { * var clips = myHitSounds.Glass [UnityEngine.Random.Range (0, myHitSounds.Glass.Length)]; * AudioSource.PlayClipAtPoint (clips, hit.point); * } */ if (materialType.TypeOfMaterial == MaterialType.MaterialTypeEnum.Metall) { var clips = myHitSounds.Metall [UnityEngine.Random.Range(0, myHitSounds.Metall.Length)]; AudioSource.PlayClipAtPoint(clips, hit.point); } if (materialType.TypeOfMaterial == MaterialType.MaterialTypeEnum.Plaster) { var clips = myHitSounds.Plaster [UnityEngine.Random.Range(0, myHitSounds.Plaster.Length)]; AudioSource.PlayClipAtPoint(clips, hit.point); } if (materialType.TypeOfMaterial == MaterialType.MaterialTypeEnum.Rock) { var clips = myHitSounds.Rock [UnityEngine.Random.Range(0, myHitSounds.Rock.Length)]; AudioSource.PlayClipAtPoint(clips, hit.point); } /* if (materialType.TypeOfMaterial == MaterialType.MaterialTypeEnum.Water) * { * var clips = myHitSounds.Water [UnityEngine.Random.Range (0, myHitSounds.Water.Length)]; * AudioSource.PlayClipAtPoint (clips, hit.point); * }*/ if (materialType.TypeOfMaterial == MaterialType.MaterialTypeEnum.Wood) { var clips = myHitSounds.Wood [UnityEngine.Random.Range(0, myHitSounds.Wood.Length)]; AudioSource.PlayClipAtPoint(clips, hit.point); } if (materialType.TypeOfMaterial == MaterialType.MaterialTypeEnum.Flesh) { var clips = myHitSounds.Flesh [UnityEngine.Random.Range(0, myHitSounds.Flesh.Length)]; AudioSource.PlayClipAtPoint(clips, hit.point); } } //Applies force to rigidbody on bullet impact. if (hit.rigidbody != null) { hit.rigidbody.AddForce(-hit.normal * BulletImpactForce, ForceMode.Impulse); } //Instantiate hit effects on object hit via MyHitEffect script var effect = myHitEffects.GetImpactEffect(hit.transform.gameObject); if (effect == null) { return; } var effectIstance = Instantiate(effect, hit.point, new Quaternion()) as GameObject; effectIstance.transform.LookAt(hit.point + hit.normal); Destroy(effectIstance, impactLife); } //Plays an animation only when useAnimation is true. if (useAnimation) { animator.Play(fireAnimationName); } }