• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Stef · Aug 09, 2014 at 06:34 AM · targetnext

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

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image

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.

Comment
Stef

People who like this

1 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Stef · Aug 09, 2014 at 03:39 PM 0
Share

Thanks Rob,

I tried the following:

 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> ();
             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:

MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it.

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
avatar image robertbu · Aug 09, 2014 at 05:42 PM 0
Share

On lines 25 and 40, you are not assigning the result to your target (I'm assuming 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 (assuming 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 instead of public.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A* pathfinder - next target 1 Answer

Cycle through targets. 2 Answers

Using Configurable Joints to create Pinball Flippers? 3 Answers

Enemy spawn then select target? 1 Answer

AI Turret Targeting System 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges