Hi guys i have a question about 2 Scripts. One Script makes to the Enemy/AI Follows the player.
but i have a Question. can i make so i can transfer the target from EnemyAI to The Other script?
Any Ideas?
EnemyAI:
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour {
public Transform tplayer;
public int moveSpeed;
public int rotationSpeed;
public int maxDistance;
public GameObject enemy;
private Transform myTransform;
void Awake() {
myTransform = transform;
}
// Use this for initialization
void Start () {
GameObject go = GameObject.FindGameObjectWithTag("Player");
tplayer = go.transform;
}
// Update is called once per frame
void Update () {
Debug.DrawLine(tplayer.position, myTransform.position, Color.yellow);
//Look at target
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(tplayer.position - myTransform.position), rotationSpeed * Time.deltaTime);
if(Vector3.Distance(tplayer.position, myTransform.position) < maxDistance) {
EnemyHealth st = (EnemyHealth)enemy.GetComponent("EnemyHealth");
st.ttarget = tplayer;
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
}
}
EnemyHealth:
using UnityEngine;
using System.Collections;
public class EnemyHealth : MonoBehaviour {
public int maxHealth;
public int curHealth;
public GameObject destroy;
public Transform ttarget;
public float attackTimer;
public float coolDown;
public int giveXP;
// Use this for initialization
void Start () {
attackTimer = 0;
coolDown = 2.0f;
}
// Update is called once per frame
void Update () {
AddjustCurrentHealth(0);
}
public void AddjustCurrentHealth(int adj) {
curHealth += adj;
if(attackTimer > 0)
attackTimer -= Time.deltaTime;
if(attackTimer < 0)
attackTimer = 0;
if(attackTimer == 0) {
Attack();
attackTimer = coolDown;
}
if(curHealth <= 0)
Die();
if(curHealth > maxHealth)
curHealth = maxHealth;
if(maxHealth < 1)
maxHealth = 1;
}
public void Die () {
Stats st = (Stats)ttarget.GetComponent("Stats");
st.XP += giveXP;
curHealth = 0;
Destroy(destroy);
}
private void Attack() {
float distance = Vector3.Distance(ttarget.transform.position, transform.position);
Vector3 dir = (ttarget.transform.position - transform.position).normalized;
float direction = Vector3.Dot(dir, transform.forward);
if(distance < 2.5f) {
if(direction > 0) {
PlayerHealth eh = (PlayerHealth)ttarget.GetComponent("PlayerHealth");
eh.AddjustCurrentHealth(-10);
}
}
}
}