子弹不会造成任何伤害

我有一个标签为enemy的近战enemyenemy有两个coliders :一个capsule (物理)和一个sphere (用于攻击半径和istrigger球员)和一个particlesystemenemy有附加的敌对脚本。

同样, bulletcapsule对撞机和设置istrigger ,我已经把enemyhealth脚本附加到bullet作为敌方的球体和子弹囊是触发器(子弹没有任何粒子系统需要修复这一点,没有光线投射), bullet s在碰撞时被破坏与enemy但不伤害敌人。

脚本用于敌人的健康

 using UnityEngine; using System.Collections; public class enemyhealth : MonoBehaviour { public int health = 40; public int current; //public int score = 10; public AudioSource deathclip; public ParticleSystem death; CapsuleCollider Collider; bool dead; // Use this for initialization void Start () { deathclip = GetComponent<AudioSource> (); Collider = GetComponent<CapsuleCollider> (); death = GetComponentInChildren <ParticleSystem> (); current = health; dead = false; } void Update() {} public void Takedamage (int amount ) { if (dead) return; current -= amount; if (current <=0) die (); } void die () { //GetComponent<NavMeshAgent> ().enabled = false; dead = true; Collider.isTrigger = true; deathclip.Play (); death.transform.parent = null; death.Play (); Destroy(death.gameObject); Destroy(gameObject); } } 

用于子弹的脚本

 using UnityEngine; using System.Collections; public class mover : MonoBehaviour { public float speed; public int damage = 10; enemyhealth Enemyhealth; GameObject ENEMY; bool enemyinrange; // Use this for initialization void Awake () { GetComponent<Rigidbody>().velocity = transform.forward*speed; Enemyhealth = GetComponent<enemyhealth> (); ENEMY = GameObject.FindGameObjectWithTag ("enemy"); } void OnTriggerEnter(Collider other) { if(other.gameObject == ENEMY && Enemyhealth.currenthealth >=0) enemyinrange=true; //Destroy(gameObject); } void OnTriggerExit(Collider other) { if (other.gameObject == ENEMY) enemyinrange = false; } void Damage() { Enemyhealth.Takedamage (damage); } // Update is called once per frame void Update () { if (enemyinrange) Damage (); } } 

1)不要把这些代码放在Awake() 。 当脚本醒来时,它只是被解雇了。

2)切换它们的顺序并使用ENEMY.GetComponent()

  void OnTriggerEnter(Collider other) { //if you have more than one enemy, this is the only way to hit the correct one ENEMY = other.gameObject; Enemyhealth = ENEMY.GetComponent<enemyhealth> (); enemyinrange=true; } 

哦, Draco18是对的。 这是你修改后的整个脚本。 代替它。

 using UnityEngine; using System.Collections; public class mover : MonoBehaviour { public float speed; public int damage = 10; enemyhealth Enemyhealth; GameObject ENEMY; bool enemyinrange; // Use this for initialization void Awake () { GetComponent<Rigidbody>().velocity = transform.forward*speed; } void OnTriggerEnter(Collider other) { if(other.gameObject.tag == "enemy") { GameObject enemyNew = other.gameObject; enemyhealth enemyHealthNew = enemyNew.GetComponent<enemyhealth> (); if (enemyHealthNew.currenthealth >=0) enemyHealthNew.Takedamage (damage); } Destroy(gameObject); } }