- Home /
Problems When Shooting Multiple Enemies round about 4 times
When I have Multiple Enemies they have a float number for health round about 250.0f so when i shoot at one of them that number should decrease by 50 each time i shoot at the enemy but heres my problem the first enemy dies round about 4/5 shots to it but when i start to shoot at the other (after the first enemy) It takes 1 shot and there dead so heres the Bullet script for the players Bullet called Bullet.cs:
using UnityEngine;
using System.Collections;
public class Bullet : MonoBehaviour {
public GameObject BulletObject;
public GameObject GlobelObjectExplosion;
public GameObject GlobelBulletExplosion;
public float life = 3.0f;
private EnemyAI enemy;
private Transform bulletTransform;
// Use this for initialization
void Start () {
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "Enemy")
{
EnemyAI.currEnemyLife -= 50;
Player.score += 50;
//Creates an explosion on collision
GameObject objBullet = Instantiate(GlobelBulletExplosion, transform.position, Quaternion.identity) as GameObject;
Destroy(objBullet, 2.0f);
Debug.Log("ColliderWorking");
if(EnemyAI.currEnemyLife <= 0.0f)
{
Destroy(other.gameObject);
//creates an explosion when enemy is destoryed
GameObject obj = Instantiate(GlobelObjectExplosion, other.gameObject.transform.position, Quaternion.identity) as GameObject;
Destroy(obj, 2.0f);
Player.score += 250;
}
Destroy(gameObject);
}
}
// Update is called once per frame
void Update () {
life -= Time.deltaTime;
transform.Translate(0, 0, Player.bulletSpeed * Time.deltaTime);
if(life <= 0.0)
{
Destroy(gameObject);
}
}
}
And heres the EnemyAI Script called EnemyAI.cs:
using UnityEngine;
using System.Collections;
public class EnemyAI: MonoBehaviour {
public Transform target;
public int moveSpeed;
public int rotationSpeed;
public GameObject explosion;
public static float maxEnemyLife = 250.0f;
public static float currEnemyLife;
public static float maxEnemyBullets = 60.0f;
public static float currEnemyBullets;
public static float maxEnemyFuel = 1260.0f;
public static float currEnemyFuel;
private Transform myTranform;
public static Transform LocalTransform;
void Awake(){
myTranform = transform;
LocalTransform = myTranform;
}
// Use this for initialization
void Start () {
if(GameObject.FindGameObjectWithTag("Player") != null){
GameObject go = GameObject.FindGameObjectWithTag("Player");
currEnemyFuel = maxEnemyFuel;
currEnemyLife = maxEnemyLife;
currEnemyBullets = maxEnemyBullets;
target = go.transform;
}
}
// Update is called once per frame
void Update () {
if (currEnemyFuel > maxEnemyFuel)
{
currEnemyFuel = maxEnemyFuel;
}
//Debug.DrawLine(target.position, myTranform.position, Color.cyan);
if(currEnemyFuel <= 0.0)
{
currEnemyFuel = 0.0f;
//myTranform.rotation = Vector3.zero;
//myTranform.position = Vector3.zero;
moveSpeed = 0;
}
else if (currEnemyFuel > 0.0)
{
//Look at target
if(GameObject.FindGameObjectWithTag("Player") != null){
myTranform.rotation = Quaternion.Slerp(myTranform.rotation, Quaternion.LookRotation(target.position - myTranform.position), rotationSpeed * Time.deltaTime);
//Move towards Target
myTranform.position += myTranform.forward * moveSpeed * Time.deltaTime;
currEnemyFuel -= 0.2f;
}
}
}
}
Ive tried to find if someone had the some problem or if theres a workaround but i can find a solution on here or google. So can someone tell me if im doing something wrong or is there a tutorial or something
Answer by dorpeleg · May 31, 2013 at 03:38 PM
In your void OnTriggerEnter(Collider other)
In the Bullet script, do the following changes:
After
if(other.gameObject.tag == "Enemy"){
Add
enemy = other.gameObject.GetComponent<EnemyAI>();
Then, every time you have EnemyAI.something, change it to enemy.something.
I think your problem is incorrect reference.
Your answer
![](https://koobas.hobune.stream/wayback/20220613103255im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Cubes wont Collide 1 Answer
Collision Detection without a RigidBody 2 Answers
Collision not detected 3 Answers
Collision problem in C# 4 Answers
OnTrigger event when colliders are already touching eachother 1 Answer