- Home /
move between unique tags
Hey Guys,
Here's what I need to do:
FindGameObjectWithTag "Tower1";
Move to that GameObject;
If that GameObject is destroyed
FindGameObjectWithTag "Tower2";
Move to that GameObject.
I have a timer set up Oncollisionenter that will destroy the game objects, the real problem is moving from one uniquely tagged gameobject to another.
Here's what I've got so far:
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour {
public Transform target;
public int moveSpeed;
public int rotationSpeed;
public float maxDistance = .5f;
private Transform myTransform;
NavMeshAgent agent;
void Awake () {
myTransform = transform;
}
void Start () {
agent = GetComponent<NavMeshAgent> ();
}
void Update () {
GameObject go = GameObject.FindGameObjectWithTag("Tower1");
target = go.transform;
Debug.DrawLine(target.position, myTransform.position, Color.yellow);
//Look at Target
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
if (Vector3.Distance(target.position, myTransform.position) > maxDistance) {
//move towards Target
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
if(go == null)
{
GameObject.FindGameObjectWithTag("Tower2");
}
}
}
I'm new to scripting, and I've been wrestling with this for a few days, X( .
Any help is appreciated. Thanks so much.
Stef
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613160159im_/https://answers.unity.com/themes/thub/images/avi.jpg)