Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
0
Question by Shindy117 · Apr 16, 2017 at 06:10 AM · arraylookatmovetowardsmissile

Homing missile: choosing a new target

Hey guys student here

And for the record my instructor told me: find all the help you can get.

So here I am my issue is this:

I have a working homing missile script however I want them to choose a new target or the nearest target. Honestly I was originally going to randomly choose a gameobject to home in on but I think that moving to the nearest one would be better and less time consuming

(which ever one you guys are willing to show me.)

Here is my script:

 public class HomingMissile : MonoBehaviour {
 
     public GameObject[] targets;
     public float speed;
 
     private Rigidbody rb;
 
     void Start()
     {
         rb = GetComponent<Rigidbody>();
 
     }
 
     void FixedUpdate()
     {
         GameObject target = targets[Random.Range(0, targets.Length)];
 
         transform.LookAt(target.transform.position);
 
         float step = speed * Time.deltaTime;
 
         transform.position = Vector3.MoveTowards(transform.position, target.transform.position, step);
     }
 }

Now my issue is I want to choose a new target every time a object is spawned however as you can see in my code It will choose a new object every frame. However I can't move the 'target' gameobject to the start function cause it loses the reference for looking at and moving towards the target. So I am SUPER stumped on how I can do this.

So any suggestions?

Comment
Add comment · Show 1
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 sleepandpancakes · Apr 16, 2017 at 06:23 AM 0
Share

You want to choose a new target every time a Ho$$anonymous$$g$$anonymous$$issile is spawned, or every time some other object is spawned? And what do you mean that it is losing the reference? Are you getting a null reference exception?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by sleepandpancakes · Apr 16, 2017 at 06:54 AM

Ok I think I get what you're saying. Assuming you want to choose the target when the HomingMissile is spawned, move the declaration of target outside of FixedUpdate so that it can be used in any function.

  public class HomingMissile : MonoBehaviour {
  
      public GameObject[] targets;
      public float speed;
  
      private Rigidbody rb;
      private GameObject chosenTarget;
  
      void Start()
      {
          rb = GetComponent<Rigidbody>();   
          ChooseTarget();
      }
 
      void ChooseTarget() 
      {
           //you can change this as you like if you want something non-random
            chosenTarget = targets[Random.Range(0, targets.Length)];
      }
  
      void FixedUpdate()
      { 
          transform.LookAt(target.transform.position);
  
          float step = speed * Time.deltaTime;
  
          transform.position = Vector3.MoveTowards(transform.position, target.transform.position, step);
      }
  }

As a side note, the code you wrote technically doesn't choose a new target every frame, since FixedUpdate() is called independently of frame rate. I recommend reading up on the execution order of Monobehaviour events.

Comment
Add comment · Show 1 · 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 Shindy117 · Apr 16, 2017 at 03:28 PM 0
Share

Thanks for comment

I noticed you just made "target" its own variable and that fixed the issue. SO THAN$$anonymous$$ YOU VERY $$anonymous$$UCH!

Now when the missile hits and destroys a city I have to re-move that choice from the array.

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.

Follow this Question

Answers Answers and Comments

75 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 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 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 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

Make object move backwards/reverse on array of waypoints 0 Answers

Instantiate same PreFab to an array of GameObject by RayCast. 1 Answer

Mega Bug In Turret - Help 5 Answers

Jerky Camera or Object motion 1 Answer

Would someone tell me why this doesn't work? 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