I’m working on a simple AI for my Starfox-ish game.
The idea is that the enemy planes will fly towards a public transform (currently the player, but I’m planning to implement some reticle in front of the player, to increase enemy chances of hitting the player) and then pull up towards the “disengager” transform when they are within a certain distance.
This works well enough when I place the planes in the scene and make them fly towards the Player prefab in the scene.
However, when I click apply, the transforms aren’t saved (they are still in bold and in the prefab folder, they remain as “none”. What I instead have to do is to set the transforms towards the folder prefabs (that’s another weird thing: the transform list is empty when I click it).
What happens when I spawn these enemies by shooting the spawnbox (I’m going to make them trigger once the player flies in to them later), is that the enemies spawn, but they start to fly in a straight line backwards, towards the ground.
Here’s my code:
using UnityEngine;
using System.Collections;
public class EnemyFighterScript : MonoBehaviour {
public float interceptvelocity = 10.0f ;
public Transform Player;
float distance;
public Transform target;
public Transform disengagetarget;
public float disengagedistance;
public bool disengage;
public float timer;
// Use this for initialization
void Start () {
disengage = false;
}
// Update is called once per frame
void Update () {
distance =Vector3.Distance(transform.position,target.position);
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 (disengagetarget);
transform.position += transform.forward * interceptvelocity * Time.deltaTime;
timer =- Time.deltaTime;
}
if(timer <= 1){
//destroys NPC once it passes player
Destroy(gameObject);
}
}
}