- Home /
Move from one target to another
Lads,
I'm trying to move from one game object to another, destroying the game objects as I go. The character moves to the first target preperly but then gets stuck and will not proceed to the next target. Here's the code:
using UnityEngine;
using System.Collections;
public class EnemyAI_2 : MonoBehaviour {
public Transform target;
public Transform target2;
public int moveSpeed;
public int rotationSpeed;
public float maxDistance = .5f;
public GameObject otherGameObject;
private Transform myTransform;
private NavMeshAgent agent;
private Strike_Timer strikeTimer;
void Awake () {
myTransform = transform;
strikeTimer = otherGameObject.GetComponent<Strike_Timer>();
}
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(strikeTimer.countdown <= 0.0f)
{
GameObject go2 = GameObject.FindGameObjectWithTag("baseCenter");
target2 = go2.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;
}
}
}
}
Any thoughts?
Stef
Answer by robertbu · Aug 09, 2014 at 08:01 AM
Assuming you've verified that strikeTimer.countdown goes below 0.0, then what you have is fighting conditions. That is, even after the countdown timer is below 0.0, you still try to move towards 'Tower1' on line 40, just as you are trying to move towards 'baseCenter' on line 53. Both lines are being executed and fighting with each other.
My suggestion is to restructure your code so that the target finding is done in a separate routine and only called once for each target. That code would set a class instance variable 'target' and the Update() code would move towards whatever is set to target. So line 30 gets moved to another function called from Start(). Lines 42 - 56 get change to something like:
if (strikeTimer.countdown <= 0.0) {
FindNewTarget();
strikeTimer.countdown = 20.0f;
}
FindNewTarget() would walk down a list setting 'target' to something new each time called.
Thanks Rob,
I tried the following:
using UnityEngine;
using System.Collections;
public class EnemyAI_2 : $$anonymous$$onoBehaviour {
public Transform target;
public Transform target2;
public int moveSpeed;
public int rotationSpeed;
public float maxDistance = .5f;
public GameObject otherGameObject;
private Transform myTransform;
private Nav$$anonymous$$eshAgent agent;
private Strike_Timer strikeTimer;
void Awake () {
myTransform = transform;
strikeTimer = otherGameObject.GetComponent<Strike_Timer>();
}
void Start () {
agent = GetComponent<Nav$$anonymous$$eshAgent> ();
GameObject go = GameObject.FindGameObjectWithTag("Tower1");
}
void Update () {
//target = go.transform;
Debug.DrawLine(target.position, myTransform.position, Color.yellow);
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
if (Vector3.Distance(target.position, myTransform.position) > maxDistance)
{
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
if(strikeTimer.countdown <= 0.0f)
{
GameObject.FindGameObjectWithTag("baseCenter");
strikeTimer.countdown = 2.0f;
}
}
}
The character is still not moving beyond the first target. Also, I'm getting the following error after the first target is destroyed:
Your script should either check if it is null or you should not destroy the object. UnityEngine.Transform.get_position () (at C:/BuildAgent/work/aeedb04a1292f85a/artifacts/EditorGenerated/UnityEngineTransform.cs:28) EnemyAI_2.Update () (at Assets/Scripts/EnemyAI_2.cs:31 Thoughts? Thanks again! Stef$$anonymous$$issingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it.
On lines 25 and 40, you are not assigning the result to your target (I'm assu$$anonymous$$g these are your targets). For example, line 25 could be:
target = GameObject.FindGameObjectWithTag("Tower1").transform;
... and 40:
target = GameObject.FindGameObjectWithTag("baseCenter").transform;
But there is another problem as well (assu$$anonymous$$g from your error). I'm guessing that the strikeTimer has some code that destroys the target. You either need to move that code to line 40 here, or you will need to move lines 38 - 42 to the top of Update(). That is you don't check the timer until after you accessed 'target' on line 32 and 33. If it is destroyed, these lines will still generate the error after you make the other fixes.
And if you are going to be assigning the initial target in code, make target private ins$$anonymous$$d of public.
Your answer
![](https://koobas.hobune.stream/wayback/20220613160210im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A* pathfinder - next target 1 Answer
Cycle through targets. 2 Answers
Asset Cross Platform Compatibility 0 Answers
isLocalPlayer set to true on child 0 Answers
Dynamic loading of Image target using ARKit - Unity3D 0 Answers