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 /
This question was closed Nov 13, 2019 at 11:03 PM by Josh-Allen for the following reason:

Solved problem on my own

avatar image
0
Question by Josh-Allen · Nov 13, 2019 at 03:07 AM · instantiatelistsmovetowards

Using Vector3.MoveTowards with each GameObject in a list

Below is my script for storing targets in a list and instantiating a missile to go to each target in the list. I can clearly print the position of each object in the list, but when I instantiate the missile(s), they do not move. How do I pass the position of each object to the missiles so they go to the targets?

    void fireControl(){ //called from Buttons.cs when fire button is down
         //Increment frame counter while held
         frameCounter = frameCounter + 1;
         if (frameCounter >= lockDelay){
             myguiText.text = "Lock On";
             //change reticle to lock on texture if held for delay period
             img.texture = (Texture)Locked;
             img2.texture = (Texture)Locked;
             //add enemies to array
             if (objCount < 4 && target != obj){
                 TargetList.Add (target as GameObject);
                 objCount = objCount + 1;
                 obj = target;
             }
         }
     }
 
     void fireMissiles(){
         print ("Fire Missiles");
         float step = speed * Time.deltaTime;
         //check for number of items in array
         foreach (GameObject newtarget in TargetList) {
         //get location and send bullet
             gameObject.GetComponent<Transform>();
             print(newtarget.transform.position);
             GameObject fired2 = Instantiate (Missile, playerLocation, Quaternion.identity); 
             fired2.transform.position = Vector3.MoveTowards (fired2.transform.position, newtarget.transform.position, step);
         }
         //clear list after button release
         TargetList.Clear();
     }
 }
Comment
Add comment
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

2 Replies

  • Sort: 
avatar image
1

Answer by Captain_Pineapple · Nov 13, 2019 at 08:25 AM

sigh


please read the documentation on things before you start using them... ->Documentation for MoveTowards


you should give some "homingAction" script to your missle where you implement it's movement. There you should then set the target after instantiation..

Comment
Add comment · 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
0

Answer by Ermiq · Nov 13, 2019 at 02:12 PM

MoveTowards() must be placed in the Update() loop to work. In your code you have just one iteration of the MoveTowards() that fires only once and it moves the missile for just one step. To continue the movement you have to call MoveTowards() again and again. PS: What is the gameObject.GetComponent<Transform>()for? You don't need that. Just use gameObject.transform.

Comment
Add comment · Show 3 · 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 Captain_Pineapple · Nov 13, 2019 at 02:50 PM 0
Share

just use transform

avatar image Bonfire-Boy Captain_Pineapple · Nov 13, 2019 at 05:06 PM 0
Share

...or just cache transform and use that (if it's in Update)

avatar image Ermiq Captain_Pineapple · Nov 13, 2019 at 05:39 PM 0
Share

Well, yes, you're right. If you need to get just this.transform then it's just transform. However, if the object is some other object then it will be someObject.transform.

Follow this Question

Answers Answers and Comments

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

Transform List not updating position of prefab. 2 Answers

Adding prefabs to a list or an array from a folder and instantiating them. 2 Answers

Spawn List of game Objects 3 Answers

Checking if object intersects? 1 Answer

Moving instantiated prefab from randomly chosen startPoint to randomly chosen endPoint 1 Answer


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