- Home /
Merging my enemy AI and target script together smart?
So I have been having troubles with disabling my movement script for when the enemy dies off. I thought maybe if I just merge the scripts together, I would be easily able to access the movement functions and disable it from the inside. But then I didn't know if this was a smart move since they are two separate functions to how the enemy works. I'll post both codes down below and you can tell me what you think. Just trying to disable the script has not been working with the getComponent().enabled thing.
//Enemy Health Script
#pragma strict
var curHealth : float = 30f;
var healthBar: UnityEngine.UI.Slider;
var isAttacked: boolean;
var isDead: boolean;
var animator : Animator;
//var maxHealth : float = 100f;
function Start () {
isAttacked = false;
isDead = false;
// healthBar = UI.Slider.FindObjectOfType(UI.Slider);
animator = GetComponent(Animator);
}
function Update () {
// healthtext.text = curHealth + " / " + maxHealth;
// healthBar.value = curHealth;
if(curHealth <= 0 ) {
curHealth = 0;
}
if(curHealth >= 30) {
curHealth = 30;
}
if (isAttacked == true) {
animator.SetBool("isDamaged", true);
}
else { animator.SetBool("isDamaged", false);}
if(isDead == true){
animator.SetBool("isDead", true);
}
}
function OnTriggerExit(collider:Collider) {
if (collider.tag == "Player" || collider.tag == "Weapon"){
if(curHealth == 0) {
isDead = true;
}
else {
curHealth -= 10;
isAttacked = true;
}
}
}
//Enemy AI Script
#pragma strict
var target : Transform; //the enemy's target
var targetHealth : int;
var moveSpeed = 3; //move speed
var rotationSpeed = 3; //speed of turning
var range : float=15f;
var range2 : float=15f;
var stop : float=1;
var myTransform : Transform; //current transform data of this enemy
var anim : Animator;
function Awake()
{
myTransform = transform; //cache transform data for easy access/preformance
}
function Start()
{
target = GameObject.FindWithTag("Player").transform; //target the player
anim = GetComponent(Animator);
if (!target == null) {
anim.SetFloat("foundTarget", 1.0);
}
}
function Update () {
//rotate to look at the player
var distance = Vector3.Distance(myTransform.position, target.position);
if (distance >= range2) {
anim.SetFloat("foundTarget", 1.0);
}
if (distance<=range2 && distance>=range){
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
}
else if(distance<=range && distance>stop){
//move towards the player
anim.SetFloat("targetDistance", distance);
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
else if (distance<=stop) {
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
anim.SetFloat("targetDistance", distance);
anim.SetBool("canAttack", true);
}
else {
anim.SetFloat("targetDistance", distance);
anim.SetBool("canAttack", false); }
}
Answer by LaneFox · Jul 02, 2015 at 11:13 AM
There isn't any reason you could not merge the two scripts, but there may be incentive to keep them separate.. Typically when scripts start getting more complicated you can start to make Update much more high level.
You could do something like.. (C#)
void Update()
{
DoHealthProcess(); // here's your health script
DoMovement(); // here's your movement script
DoOtherStuff(); // heres some other magic
}
Then just put your two separate scripts into the other methods and let a single update invoke them.
However it could be to your advantage in the long term to keep Health separate so it is easier to access generically. For instance when you want to start damaging things you'll have a specific script you can target which will deal with processing damage and collisions that will always have the same class name and method names. If you put Health inside a different script, you may have a harder time accessing the Health between different enemies/classes.
Thank you for your advice! I forget about just making stuff in its own methods!
Good point on script management, but this single-update issue isn't required since update is called once per frame. You can just use update in every script individually.