I’m working on the script for the enemies in the game.
Thus far, they fly towards the player once spawned and that’s pretty much it.
What I want to happen is firstly, for the enemy plane to lose health once hit by a players bullet. Then, once the enemy has zero healt, I want to turn on gravity, to make the enemy crash into the ground. And once they crash into the ground, I want the enemy to disappear.
The problem I’m having right now is that it doesen’t seem to register the collision. I’ve given the bullets the correct tag, but still nothing: the bullets pass straight through the enemy plane, even when I give the bullets ridiculously big hitboxes. And yes, I have paused the game and verified that the hitboxes in fact have collided.
Here’s my current enemy code
using UnityEngine;
using System.Collections;
public class EnemyFighterScript : MonoBehaviour {
public float interceptvelocity = 10.0f ;
private Transform Player;
float distance;
private Transform Disengage;
public float disengagedistance;
public bool disengage;
public float timer;
public float health;
// Use this for initialization
void Start () {
disengage = false;
GameObject go = GameObject.Find ("Player") as GameObject;
Player = go.transform;
GameObject dis = GameObject.Find ("disengager") as GameObject;
Disengage = dis.transform;
}
void OnColliderHit(Collider collision)
{
if(collision.transform.tag == "Ground")
{
DestroyObject(collision.gameObject);
}
if(collision.transform.tag == "PlayerProjectile")
{
Debug.Log("Iz ded");
}
}
// Update is called once per frame
void Update () {
distance =Vector3.Distance(transform.position,Player.position);
if (health<0){
// Rigidbody.useGravity = true;
}
if (distance<disengagedistance){
disengage=true;
}
if (disengage==false){
transform.LookAt (Player);
transform.position += transform.forward * interceptvelocity * Time.deltaTime;
}
if (disengage==true) {
//makes enemy fly away and prepare self destruct after reaching proximity to player
transform.LookAt (Disengage);
transform.position += transform.forward * interceptvelocity * Time.deltaTime;
timer =- Time.deltaTime;
}
if(timer <= 1){
//destroys NPC once it passes player
Destroy(gameObject);
}
}
}
Worth mentioning is that I’ve also had problems with the gravity code, as it just keeps on giving me error messages when I try to implement it.