//射击函数 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); } } // } }