private void Shoot() { Ray ray = _camera.ScreenPointToRay(target); RaycastHit hit = new RaycastHit(); //camera shake EventBroadcaster.Instance.PostEvent(EventNames.FinalGameEvents.ON_GUN_SHOT_SHAKE); EventBroadcaster.Instance.PostEvent(EventNames.FinalGameEvents.ON_CURSOR_SHOT); gunShotAudio.Play(); if (Physics.Raycast(ray, out hit, Range, _shootableMask)) { //print("hit " + hit.collider.gameObject); onHitParticle.Stop(); Vector3 onHitPosition = hit.point; onHitParticle.transform.position = onHitPosition; onHitParticle.Play(); ZombieHealth health = hit.collider.GetComponent <ZombieHealth>(); if (health != null) { health.TakeDamage(1); } } }
// Update is called once per frame void Update() { movement(); int damageToDeal = 0; //Detects to see if Player hits the Zombie if (Physics.Raycast(trans.position, trans.forward, out hit, 100)) { if (hit.collider.tag == "Player") { //Damage if punching if (Input.GetKeyDown(KeyCode.Alpha1)) { damageToDeal = 5; StartCoroutine(soundDelay(punchClip)); print("zombie is hurt"); } //Damage if slashing if (Input.GetKeyDown(KeyCode.Alpha2)) { damageToDeal = 30; StartCoroutine(soundDelay(swordClip)); print("zombie is hurt30"); } } health.TakeDamage(damageToDeal); } }
/** * Make the player shoot on left click. */ void Shoot() { timer = 0f; // Reset timer. // Enable gun effects. gunLight.enabled = true; //gunParticles.Stop(); // If particles are still playing, stop them //gunParticles.Play(); // and start them again. gunLine.enabled = true; gunLine.SetPosition(0, transform.position); // Set line at position of barrel. gunSound.Play(); ray.origin = transform.position; ray.direction = transform.forward; if (Physics.Raycast(ray, out hit, range, shootable)) //('out hit' provides information of what was hit with raycast) { /* Take away zombie's health. */ ZombieHealth health = hit.collider.GetComponent <ZombieHealth>(); // Get zombie health script. if (health != null) // Make sure we hit a Zombie and not something like the vehicles. { // Make Zombie take damage: health.TakeDamage(damage, hit.point); } gunLine.SetPosition(1, hit.point); // Set line end point to what we shot. } else // Just draw a line if not shooting at something that is 'shootable'. { gunLine.SetPosition(1, ray.origin + ray.direction * range); } }
void Shoot(PhotonPlayer attacker, Vector3 shootPosition) { if (!PhotonNetwork.isMasterClient) //如果玩家不是MasterClient,结束函数的执行 { return; } Vector3 rotatedPos = transform.rotation * shootingPosition; //随着角色的旋转,射击的初始位置也需要跟着旋转 ray.origin = rotatedPos + transform.position; //设置射击射线的起始端点 ray.direction = gunTransform.forward; //设置射击射线的方向 var hitTag = ""; //击中物体的Tag if (lineRenderer != null && lineObj == null) { GameObject go = Instantiate(lineRenderer, Vector3.zero, Quaternion.identity) as GameObject; lineObj = go.GetComponent <LineRenderer>(); } Vector3 bulletEffectPosition; //子弹爆炸效果的位置 //发出射击射线,判断是否击中物体 if (Physics.Raycast(ray, out hitInfo, shootingRange)) //如果射线击中游戏对象 { GameObject go = hitInfo.collider.gameObject; //获取被击中的游戏对象 if (go.tag == "Player") //如果击中玩家 { PlayerHealth playerHealth = go.GetComponent <PlayerHealth>(); if (playerHealth.team != GetComponent <PlayerHealth> ().team) //如果被击中玩家队伍与攻击者玩家队伍不同 { playerHealth.TakeDamage(shootingDamage, attacker); //被击中玩家扣血 } } else if (go.tag == "Zombie" || go.tag == "Guard" || go.tag == "Skeleton") //如果击中僵尸 { ZombieHealth zh = go.GetComponent <ZombieHealth> (); if (zh != null) { zh.TakeDamage(shootingDamage, shootPosition, attacker); //僵尸扣血 } } hitTag = go.tag; bulletEffectPosition = hitInfo.point; //击中游戏对象,子弹爆炸效果的位置在击中点 } else { bulletEffectPosition = ray.origin + shootingRange * ray.direction; //如果未击中,子弹爆炸效果为子弹失效的位置 } if (lineObj != null) { lineObj.SetWidth(0.02F, 0.02F); lineObj.SetPosition(0, ray.origin); lineObj.SetPosition(1, bulletEffectPosition); } photonView.RPC("ShootEffect", PhotonTargets.AllViaServer, bulletEffectPosition, hitTag); //使用RPC,调用所有玩家对象的ShootEffect函数,显示射击效果 }
//射击函数 void Shoot() { AudioSource.PlayClipAtPoint(shootingAudio, transform.position); //播放射击音效 //枪口闪光特效 if (GunShootingEffect != null && shootingEffectTransform != null) { (Instantiate(GunShootingEffect, shootingEffectTransform.position, shootingEffectTransform.rotation) as GameObject).transform.parent = shootingEffectTransform; } ray.origin = myCamera.transform.position; //设置射线发射的原点:摄像机所在的位置 ray.direction = myCamera.transform.forward; //设置射线发射的方向:摄像机的正方向 if (gunLine != null) { gunLine.enabled = true; //进行射击时,启用线渲染器(激光射线效果) gunLine.SetPosition(0, transform.position); //设置线渲染器(激光射线效果)第一个端点的位置:玩家枪械的枪口位置 } //发射射线,射线有效长度为shootingRange,若射线击中任何游戏对象,则返回true,否则返回false if (Physics.Raycast(ray, out hitInfo, shootingRange)) { if (hitInfo.transform.gameObject.tag.Equals("Enemy")) //当被击中的游戏对象标签为Enemy,表明射线击中敌人 //获取敌人生命值组件 { ZombieHealth enemyHealth = hitInfo.transform.gameObject.GetComponent <ZombieHealth> (); if (enemyHealth != null) { //调用EnemyHealth脚本的TakeDamage()函数,对敌人造成shootingDamage的伤害,注意,这里需要传入攻击者所在的位置 enemyHealth.TakeDamage(shootingDamage, myCamera.transform.position); } } if (gunLine != null) { gunLine.SetPosition(1, hitInfo.point); //当射线击中游戏对象时,设置线渲染器(激光射线效果)第二个端点的位置:击中对象的位置 gunLine.SetWidth(LINE_RENDERER_START, //射线在射程内击中对象时,需要根据击中对象的位置动态调整线渲染器(激光射线效果)的宽度 Mathf.Clamp((hitInfo.point - ray.origin).magnitude / shootingRange, LINE_RENDERER_START, LINE_RENDERER_END)); } if (bulletEffect != null) { Instantiate(bulletEffect, hitInfo.point, Quaternion.identity); } } else { if (bulletEffect != null) { Instantiate(bulletEffect, ray.origin + ray.direction * shootingRange, Quaternion.identity); } } // if (gunLine != null) { // //当射线未击中游戏对象时,设置线渲染器(激光射线效果)第二个端点的位置:射线射出后的极限位置 // gunLine.SetPosition (1, ray.origin + ray.direction * shootingRange); // //射线在射程内未击中对象,直接设置射线的初始与末尾宽度 // gunLine.SetWidth (LINE_RENDERER_START, LINE_RENDERER_END); // } }
public void Fire() { RaycastHit hit; // Check if the weapon fired hit something if (currentWeapon.Fire(out hit)) { ZombieHealth zombieHealth = hit.collider.GetComponent <ZombieHealth>(); if (zombieHealth != null) { zombieHealth.TakeDamage(currentWeapon.damage, hit, player); } } }
IEnumerator attack() { if (attackPerforming) { yield return(new WaitForSeconds(0)); } else { attackPerforming = true; target.animation.Play(attacks[attacknumber]); attacknumber++; if (attacknumber == 3) { attacknumber = 0; } if (Physics.Raycast(shootRay, out shootHit, range)) { Debug.DrawLine(shootRay.origin, shootHit.point); ZombieHealth enemyHealth = shootHit.collider.GetComponent <ZombieHealth> (); if (enemyHealth != null) { enemyHealth.TakeDamage(damagePerHit); playerAudio.clip = hitClip; playerAudio.Play(); } else { playerAudio.clip = swingClip; playerAudio.Play(); } } else { playerAudio.clip = swingClip; playerAudio.Play(); } yield return(new WaitForSeconds(0.5f)); target.animation.Play("idle"); attackPerforming = false; } }
void Shoot() { Effect(); //animator.Play("Shoot"); timer = 0f; gunAudio.Play(); shootRay.origin = transform.position; shootRay.direction = transform.forward; if (Physics.Raycast(shootRay, out shootHit, range, shootableMask)) { ZombieHealth zombieHealth = shootHit.collider.GetComponent <ZombieHealth>(); if (zombieHealth != null) { zombieHealth.TakeDamage(playerState, damagePerShot); } } }
void Shoot(PhotonPlayer attacker) { if (!PhotonNetwork.isMasterClient) //如果玩家不是MasterClient,结束函数的执行 { return; } ray.origin = shootingPosition + transform.position; //设置射击射线的起始端点 ray.direction = gunTransform.forward; //设置射击射线的方向 Vector3 bulletEffectPosition; //子弹爆炸效果的位置 //发出射击射线,判断是否击中物体 if (Physics.Raycast(ray, out hitInfo, shootingRange)) //如果射线击中游戏对象 { GameObject go = hitInfo.collider.gameObject; //获取被击中的游戏对象 if (go.tag == "Player") //如果击中玩家 { PlayerHealth playerHealth = go.GetComponent <PlayerHealth> (); if (playerHealth.team != GetComponent <PlayerHealth> ().team) //如果被击中玩家队伍与攻击者玩家队伍不同 { playerHealth.TakeDamage(shootingDamage, attacker); //被击中玩家扣血 } } else if (go.tag == "Zombie") //如果击中僵尸 { ZombieHealth zh = go.GetComponent <ZombieHealth> (); if (zh != null) { zh.TakeDamage(shootingDamage, attacker); //僵尸扣血 } } bulletEffectPosition = hitInfo.point; //击中游戏对象,子弹爆炸效果的位置在击中点 } else { bulletEffectPosition = ray.origin + shootingRange * ray.direction; //如果未击中,子弹爆炸效果为子弹失效的位置 } photonView.RPC("ShootEffect", PhotonTargets.All, bulletEffectPosition); //使用RPC,调用所有玩家对象的ShootEffect函数,显示射击效果 }
//Shoot void Shoot() { if (Time.timeScale != 0f) { //Play shoot animation if (!anim.GetBool("isAiming")) { anim.Play("Fire"); } else { anim.SetTrigger("Shoot"); } //Remove 1 bullet currentAmmo -= 1; //Instantiate the bullet if (Input.GetMouseButton(0)) { GameObject Temporary_Bullet_Handler; Temporary_Bullet_Handler = Instantiate(Bullet, Bullet_Emitter.transform.position, Bullet_Emitter.transform.rotation) as GameObject; Temporary_Bullet_Handler.transform.Rotate(Vector3.right * 90); Rigidbody Temporary_RigidBody; Temporary_RigidBody = Temporary_Bullet_Handler.GetComponent <Rigidbody>(); Temporary_RigidBody.AddForce(transform.forward * Bullet_Velocity); Destroy(Temporary_Bullet_Handler, 10f); if (Input.GetKeyDown(KeyCode.LeftControl) && Input.GetMouseButton(0)) { Time.timeScale = 0.1f; } if (Input.GetKeyDown(KeyCode.LeftControl) && Input.GetMouseButton(0) && Time.timeScale == 0.1f) { Time.timeScale = 1f; } } //Play shoot sound AudioClips.mainAudioSource.clip = AudioClips.shootSound; AudioClips.mainAudioSource.Play(); //Start casing instantiate if (!ReloadSettings.casingOnReload) { StartCoroutine(CasingDelay()); } //Show the muzzleflash StartCoroutine(MuzzleFlash()); //Raycast bullet RaycastHit hit; Ray ray = new Ray(transform.position, transform.forward); //Send out the raycast from the "bulletSpawnPoint" position if (Physics.Raycast(Spawnpoints.bulletSpawnPoint.transform.position, Spawnpoints.bulletSpawnPoint.transform.forward, out hit, ShootSettings.bulletDistance)) { Debug.DrawLine(transform.position, hit.point, Color.red, 15, false); if (Physics.Raycast(ray, shootableMask)) { //Try and find a ZombieHealth Script ZombieHealth zombieHealth = hit.collider.GetComponent <ZombieHealth>(); //If it exists... if (zombieHealth != null) { //Zombie Takes Damage Reflective to weapon settings zombieHealth.TakeDamage(ShootSettings.damagePerShot); } } //If a rigibody is hit, add bullet force to it if (hit.rigidbody != null) { hit.rigidbody.AddForce(ray.direction * ShootSettings.bulletForce); } //********** USED IN THE DEMO SCENES ********** //If the raycast hit the tag "Target" if (hit.transform.tag == "Target") { //Spawn bullet impact on surface Instantiate(Prefabs.metalImpactPrefab, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal)); //Toggle the isHit bool on the target object hit.transform.gameObject.GetComponent <TargetScript>().isHit = true; } //********** USED IN THE DEMO SCENES ********** //If the raycast hit the tag "ExplosiveBarrel" if (hit.transform.tag == "ExplosiveBarrel") { //Toggle the explode bool on the explosive barrel object hit.transform.gameObject.GetComponent <ExplosiveBarrelScript>().explode = true; //Spawn metal impact on surface of the barrel Instantiate(Prefabs.metalImpactPrefab, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal)); } //********** USED IN THE DEMO SCENES ********** //If the raycast hit the tag "GasTank" if (hit.transform.tag == "GasTank") { //Toggle the explode bool on the explosive barrel object hit.transform.gameObject.GetComponent <GasTankScript>().isHit = true; //Spawn metal impact on surface of the gas tank Instantiate(Prefabs.metalImpactPrefab, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal)); } //If the raycast hit the tag "Metal (Static)" if (hit.transform.tag == ImpactTags.metalImpactStaticTag) { //Spawn bullet impact on surface Instantiate(Prefabs.metalImpactStaticPrefab, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal)); } //If the raycast hit the tag "Metal" if (hit.transform.tag == ImpactTags.metalImpactTag) { //Spawn bullet impact on surface Instantiate(Prefabs.metalImpactPrefab, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal)); } //If the raycast hit the tag "Wood (Static)" if (hit.transform.tag == ImpactTags.woodImpactStaticTag) { //Spawn bullet impact on surface Instantiate(Prefabs.woodImpactStaticPrefab, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal)); } //If the raycast hit the tag "Wood" if (hit.transform.tag == ImpactTags.woodImpactTag) { //Spawn bullet impact on surface Instantiate(Prefabs.woodImpactPrefab, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal)); } //If the raycast hit the tag "Concrete (Static)" if (hit.transform.tag == ImpactTags.concreteImpactStaticTag) { //Spawn bullet impact on surface Instantiate(Prefabs.concreteImpactStaticPrefab, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal)); } //If the raycast hit the tag "Concrete" if (hit.transform.tag == ImpactTags.concreteImpactTag) { //Spawn bullet impact on surface Instantiate(Prefabs.concreteImpactPrefab, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal)); } //If the raycast hit the tag "Dirt (Static)" if (hit.transform.tag == ImpactTags.dirtImpactStaticTag) { //Spawn bullet impact on surface Instantiate(Prefabs.dirtImpactStaticPrefab, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal)); } //If the raycast hit the tag "Dirt" if (hit.transform.tag == ImpactTags.dirtImpactTag) { //Spawn bullet impact on surface Instantiate(Prefabs.dirtImpactPrefab, hit.point, Quaternion.FromToRotation(Vector3.forward, hit.normal)); } } } }
//If the projectile collides with anything void OnCollisionEnter(Collision collision) { if (collision.gameObject.tag == "Enemy") { //Try and find a ZombieHealth Script ZombieHealth zombieHealth = collision.gameObject.GetComponent <ZombieHealth>(); //If it exists... if (zombieHealth != null) { //Zombie Takes Damage Reflective to arrow damage zombieHealth.TakeDamage(arrowDamage); GetComponent <Rigidbody>().isKinematic = true; } } //********** USED IN THE DEMO SCENES ********** //If the projectile hit the tag "Target", and if "isHit" is false if (collision.gameObject.tag == "Target" && collision.gameObject.GetComponent <TargetScript>().isHit == false) { //Spawn explosion prefab on surface Instantiate(explosionMetalPrefab, collision.contacts[0].point, Quaternion.LookRotation(collision.contacts[0].normal)); //Animate the target collision.gameObject.transform.gameObject.GetComponent <Animation> ().Play("target_down"); //Toggle the isHit bool on the target object collision.gameObject.transform.gameObject.GetComponent <TargetScript>().isHit = true; } //If the projectile collides with metal tag or metal impact static tag if (collision.gameObject.tag == metalImpactTag || collision.gameObject.tag == metalImpactStaticTag) { Instantiate(explosionMetalPrefab, collision.contacts[0].point, Quaternion.LookRotation(collision.contacts[0].normal)); } //If the projectile collides with concrete tag or concrete impact static tag if (collision.gameObject.tag == concreteImpactTag || collision.gameObject.tag == concreteImpactStaticTag) { Instantiate(explosionConcretePrefab, collision.contacts[0].point, Quaternion.LookRotation(collision.contacts[0].normal)); } //If the projectile collides with wood tag or wood impact static tag if (collision.gameObject.tag == woodImpactTag || collision.gameObject.tag == woodImpactStaticTag) { Instantiate(explosionWoodPrefab, collision.contacts[0].point, Quaternion.LookRotation(collision.contacts[0].normal)); } //If the projectile collides with dirt tag or dirt impact static tag if (collision.gameObject.tag == dirtImpactTag || collision.gameObject.tag == dirtImpactStaticTag) { Instantiate(explosionDirtPrefab, collision.contacts[0].point, Quaternion.LookRotation(collision.contacts[0].normal)); } //Explosion force Vector3 explosionPos = transform.position; Collider[] colliders = Physics.OverlapSphere(explosionPos, radius); foreach (Collider hit in colliders) { Rigidbody rb = hit.GetComponent <Rigidbody> (); //Add force to nearby rigidbodies if (rb != null) { rb.AddExplosionForce(power, explosionPos, radius, 3.0F); } //********** USED IN THE DEMO SCENES ********** //If the explosion hit the tags "Target", and "isHit" is false if (hit.GetComponent <Collider>().tag == "Target" && hit.GetComponent <TargetScript>().isHit == false) { //Animate the target hit.gameObject.GetComponent <Animation> ().Play("target_down"); //Toggle the isHit bool on the target object hit.gameObject.GetComponent <TargetScript>().isHit = true; } //If the projectile explosion hits barrels with the tag "ExplosiveBarrel" if (hit.transform.tag == "ExplosiveBarrel") { //Toggle the explode bool on the explosive barrel object hit.transform.gameObject.GetComponent <ExplosiveBarrelScript>().explode = true; } } //Destroy the projectile on collision if (!isArrow) { Destroy(gameObject); } //If arrow collides, freeze the position if (isArrow == true) { GetComponent <Rigidbody> ().isKinematic = true; } }
//射击函数 void shoot() { AudioSource.PlayClipAtPoint(shootingAudio, transform.position); //在枪口位置播放射击音效 ray.origin = Camera.main.transform.position; //设置射线发射的原点:摄像机所在的位置 ray.direction = Camera.main.transform.forward; //设置射线发射的方向:摄像机的正方向 gunLine.SetPosition(0, transform.position); //设置线渲染器(开枪后的激光射线)第一个端点的位置:玩家枪械的枪口位置(本游戏对象) //发射射线,射线有效长度为shootingRange,若射线击中任何游戏对象,则返回true,否则返回false if (Physics.Raycast(ray, out hitInfo, shootingRange)) { if (hitInfo.collider.gameObject.tag == "Zombie") //当被击中的游戏对象标签为Enemy,表明射线射中敌人 //获取该名敌人的Zombie脚本组件 { ZombieHealth zombieHealth = hitInfo.collider.gameObject.GetComponent <ZombieHealth> (); if (zombieHealth != null) { //调用EnemyHealth脚本的TakeDamage()函数,对敌人造成shootingDamage的伤害 zombieHealth.TakeDamage(shootingDamage); } if (zombieHealth.health > 0) //若敌人受伤且未死亡,敌人将会因受到攻击而被击退 { hitInfo.collider.gameObject.transform.position += transform.forward * 2; } } else if (hitInfo.collider.gameObject.tag == "MonsterA") { MonsterAHealth monsterAhealth = hitInfo.collider.gameObject.GetComponent <MonsterAHealth> (); if (monsterAhealth != null) { monsterAhealth.TakeDamage(shootingDamage); } if (monsterAhealth.health > 0) { hitInfo.collider.gameObject.transform.position += transform.forward * 2; } } else if (hitInfo.collider.gameObject.tag == "MonsterB") { MonsterBHealth monsterBhealth = hitInfo.collider.gameObject.GetComponent <MonsterBHealth> (); if (monsterBhealth != null) { monsterBhealth.TakeDamage(shootingDamage); } if (monsterBhealth.health > 0) { //hitInfo.collider.gameObject.transform.position += transform.forward * 2; } } else if (hitInfo.collider.gameObject.tag == "MonsterC") { MonsterCHealth monsterChealth = hitInfo.collider.gameObject.GetComponent <MonsterCHealth> (); if (monsterChealth != null) { monsterChealth.TakeDamage(shootingDamage); } if (monsterChealth.health > 0) { //hitInfo.collider.gameObject.transform.position += transform.forward * 2; } } else if (hitInfo.collider.gameObject.tag == "Boss") { BossHealth bosshealth = hitInfo.collider.gameObject.GetComponent <BossHealth> (); if (bosshealth != null) { bosshealth.TakeDamage(shootingDamage); } if (bosshealth.health > 0) { //hitInfo.collider.gameObject.transform.position += transform.forward * 2; } } gunLine.SetPosition(1, hitInfo.point); //当射线击中游戏对象时,将线渲染器(开枪后的激光射线)第二个端点设为射线击中游戏对象的点 } //若射线未射中游戏对象,则将线渲染器(开枪后的激光射线)第二个端点设为射线射出后的极限位置 else { gunLine.SetPosition(1, ray.origin + ray.direction * shootingRange); } gunLine.enabled = true; //将线渲染器(开枪后的激光射线)启用,显示玩家开枪后的效果。 }
public void attack() { // if (gameObj != null) { if (HP <= 0) { return; } gameObj = hitInfo.collider.gameObject; if (gameObj.tag == "MonsterA") { // Debug.Log ("Attack MonsterA"); MonsterAHealth monsterAHealth = gameObj.GetComponent <MonsterAHealth> (); if (monsterAHealth != null && monsterAHealth.health > 0) { animator.SetTrigger("Use"); //tell mecanim to do the attack animation(trigger) monsterAHealth.TakeDamage(attackDamage); } else if (monsterAHealth.health <= 0) { disguisePermit = 1; } } else if (gameObj.tag == "MonsterB") { Debug.Log("Attack MonsterB"); MonsterBHealth monsterBHealth = gameObj.GetComponent <MonsterBHealth> (); if (monsterBHealth != null && monsterBHealth.health > 0) { animator.SetTrigger("Use"); monsterBHealth.TakeDamage(attackDamage); } else if (monsterBHealth.health <= 0) { disguisePermit = 2; } } else if (gameObj.tag == "MonsterC") { Debug.Log("Attack MonsterC"); MonsterCHealth monsterCHealth = gameObj.GetComponent <MonsterCHealth> (); if (monsterCHealth != null && monsterCHealth.health > 0) { animator.SetTrigger("Use"); monsterCHealth.TakeDamage(attackDamage); } else if (monsterCHealth.health <= 0) { disguisePermit = 3; } } /* else if (gameObj.tag == "Teddy") { * Debug.Log ("Attack Teddy"); * TeddyHealth teddyHealth = gameObj.GetComponent<TeddyHealth> (); * if (teddyHealth != null) { * teddyHealth.TakeDamage (attackDamage); * } * }*/ else if (gameObj.tag == "Zombie") { Debug.Log("Attack Zombie"); ZombieHealth zombieHealth = gameObj.GetComponent <ZombieHealth> (); if (zombieHealth != null && zombieHealth.health > 0) { animator.SetTrigger("Use"); zombieHealth.TakeDamage(attackDamage); } } else if (gameObj.tag == "Boss") { Debug.Log("Attack Boss"); BossHealth bossHealth = gameObj.GetComponent <BossHealth> (); if (bossHealth != null && bossHealth.health > 0) { animator.SetTrigger("Use"); bossHealth.TakeDamage(attackDamage); } } // } }
public void attack(int damage, float attackStart) { bool hitZombie = false; // Debug.Log("num: " + hitbox.zombies.Keys.Count); ArrayList deadZombies = new ArrayList(); foreach (Collider col in zombies.Keys) { if (col) { ZombieHealth zombie = col.GetComponent <ZombieHealth>(); if (zombie.LastHitTime < attackStart) { hitZombie = true; zombie.TakeDamage(damage, ZombieHealth.HitTypes.sword); } } else { deadZombies.Add(col); // this zombie died while in range } } foreach (Collider col in deadZombies) { zombies.Remove(col); } ArrayList deadDestructibles = new ArrayList(); foreach (Collider col in destructibles.Keys) { if (col) { col.transform.parent.gameObject.GetComponent <Destructible>().smash(); } else { deadDestructibles.Add(col); } } foreach (Collider col in deadZombies) { destructibles.Remove(col); } if (bossHealth != null && bossHealth.lastSwordHitTime < attackStart) { hitZombie = true; bossHealth.lastSwordHitTime = attackStart; bossHealth.takeDamage(damage); } if (hitZombie) { AudioSource audio = GetComponent <AudioSource>(); if (!audio.isPlaying) { GetComponent <AudioSource>().Play(); } } }