- Home /
AI call tag function
Alrighty, so i threw together this little AI script that i'm going to use in my game. I decided to use the Free spawner system made by Corupted smile studio. In order for me to do this i think i need to change the target transform to a tag but i don't know how to call a tag in my script. The script is in C#.
using UnityEngine; using System.Collections;
public class EnemyAI : MonoBehaviour { public Transform Target; public int movespeed; public int rotationspeed;
private Transform MyTransform;
void Awake() {
MyTransform=transform;
}
// Use this for initialization
void Start () {
GameObject go = GameObject.FindGameObjectWithTag("Player");
Target = go.transform;
}
// Update is called once per frame
void Update () {
Debug.DrawLine(Target.position, MyTransform.position, Color.red);
//look at target
MyTransform.rotation = Quaternion.Slerp(MyTransform.rotation, Quaternion.LookRotation(Target.position - MyTransform.position), rotationspeed * Time.deltaTime);
//Move Towards Target
MyTransform.position += MyTransform.forward * movespeed * Time.deltaTime;
}
}
Answer by Berenger · Feb 21, 2012 at 02:53 AM
What's wrong with that script ?
You can't "call a tag", nor change a Transform into a tag or he other way around. the tag is no more than a string, here to help you in some cases (Find an object very quickly, or identifying a collider as part of a group etc). Your code seems fine to me.