Hi all!
I’ve a serious problem. I’m working on the space shooter project and i do some customization in the script.
My player fire some laser very fast (rate 0.05) to a enemy ship who has some health. My laser has a rigidbody and “is trigger” on.
Here is my EnemyController script :
using UnityEngine;
using System.Collections;
public class EnemyController : MonoBehaviour
{
public float m_speed;
public GameObject m_shot;
public Transform m_spawnShot1;
public Transform m_spawnShot2;
public float m_fireRate;
public GameObject m_explosion;
public GameObject m_otherexplosion;
public GUIText m_healthStatus;
public float m_health;
private float m_nextShot;
void Start()
{
rigidbody.velocity = transform.forward * m_speed;
m_healthStatus.text = m_health.ToString();
}
// Update is called once per frame
void Update ()
{
if(Time.time > m_nextShot)
{
m_nextShot = Time.time + m_fireRate;
Instantiate(m_shot, m_spawnShot1.position, Quaternion.identity);
Instantiate(m_shot, m_spawnShot2.position, Quaternion.identity);
}
}
void OnTriggerEnter(Collider other)
{
if(other.tag == "Boundary")
return;
else if(other.tag == "Player")
{
Instantiate(m_otherexplosion, other.transform.position, other.transform.rotation);
Instantiate(m_explosion, transform.position, transform.rotation);
//Destroy(other.gameObject);
//Destroy(gameObject);
}
else if(other.tag == "Rocket")
{
m_health -= 10.0f;
m_healthStatus.text = m_health.ToString();
Instantiate(m_otherexplosion, other.transform.position, other.transform.rotation);
//Destroy(other.gameObject);
if(m_health <= 0.0f)
{
Debug.Log("Destroy enemy");
Instantiate(m_explosion, transform.position, transform.rotation);
//Destroy(gameObject);
}
}
else if(other.tag == "Lazer")
{
m_health -= 5.0f;
m_healthStatus.text = m_health.ToString();
Instantiate(m_otherexplosion, other.transform.position, other.transform.rotation);
//Destroy(other.gameObject);
if(m_health <= 0.0f)
{
Debug.Log("Destroy enemy");
Instantiate(m_explosion, transform.position, transform.rotation);
//Destroy(gameObject);
}
}
else
return;
}
}
Laser script
using UnityEngine;
using System.Collections;
public class Mover : MonoBehaviour
{
public float m_speed;
void Start()
{
rigidbody.velocity = transform.forward * m_speed;
}
}
PlayerController script
void Update ()
{
if(Input.GetButton("Fire1") Time.time > m_nextShot)
{
m_nextShot = Time.time + m_lazerFireRate;
Instantiate(m_lazerShot, m_lazerShotSpawn.position, Quaternion.identity);
}
if(m_shotRocket Time.time > m_nextShotRocket)
{
m_nextShotRocket = Time.time + m_rocketFireRate;
Instantiate(m_rocketShot, m_rocketShotSpawnOne.position, Quaternion.identity);
Instantiate(m_rocketShot, m_rocketShotSpawnTwo.position, Quaternion.identity);
m_shotRocket = false;
}
}
My problem is that my enemy ship is destroy before his health reach 0 and i don’t know why? If my fire rate is lower (0.2) no problem happen. What’s wrong?
Thanks for your help !