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 smokk83 · Jan 20, 2020 at 07:55 PM · instantiateprefabstransform.positionnewbietargetting

Instanitiate PreFabs and let them move to different locations

HI,

and another Problem.

Right now i am trying to make an "Skill" for my warrior. When i hit a button, i want to create an Arrow Pre fab für each enemy that is on my world.

This works for me with

  foreach (GameObject go in GameManager.spawnEnemys)
         {
 
             GameObject disc = Instantiate(discPreFab);
             disc.transform.position = new Vector3(transform.position.x+10,transform.position.y+10,transform.position.z+10);

now comes the problem, how can i say my prefabs where they should move to? I want one Disc for each enemy.

Tryed

   void Update()
     {
         SetTargets();
 
         //while (i < targets.Length) // while seems not to work here 
         //{
         //    transform.position = Vector3.MoveTowards(transform.position, targets[i].transform.position, 50);
         //    i++;
         //}
 
         if(GameManager.spawnEnemys.Count > 0)
         {
             foreach (GameObject go in targets)
             {
                 transform.position = Vector3.MoveTowards(transform.position, go.transform.position, 50);
                 //GameManager.spawnEnemys.Remove(go);
             }
         }
        
     
     }
 
     public GameObject[] targets;
 
     public void SetTargets()
     {
 
         targets = GameManager.spawnEnemys.ToArray();
 
     }


combination but like expected doenst work for me. Have many trouble with target setting, if anyone knows a good tutorial especialy for multitarget it would be great. But any Answer which i can test and try to understand would be great :)

Greetz Smokki

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

1 Reply

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

Answer by unity_ek98vnTRplGj8Q · Jan 20, 2020 at 09:10 PM

I'm not sure whether that second script is attached to your character or to each disc object, but the easiest way to do this would be to assign each disc a target when you spawn it, and then let the disc travel towards its own target.


So you could make a script and attach it to your disc prefab. Also, 50 seems like a large distance to use in Vector3.MoveTowards, and is not frame independent. Did you mean 50 * Time.deltaTime?

 public class discFlightBehavior : Monobehavior {
     public GameObject targetEnemy;
 
     void Update(){
         transform.position = Vector3.MoveTowards(transform.position, targetEnemy.transform.position, 50 * Time.deltaTime);
     }
 }

And then when you spawn each disc:

 foreach (GameObject go in GameManager.spawnEnemys) {
     GameObject disc = Instantiate (discPreFab);
     disc.GetComponent<discFlightBehavior>().targetEnemy = go;
     disc.transform.position = new Vector3 (transform.position.x + 10, transform.position.y + 10, transform.position.z + 10);
 }

Comment
Add comment · Show 5 · 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 smokk83 · Jan 20, 2020 at 09:28 PM 0
Share

Wow thank you so much :) This worked i am hyped :)

P.S. Now i have the problem, that all the discs need an rigidbody otherwise they fly through my gameobject like trees etc.

But when i add rigidbody and spawn them all at the same location -- boom they all get disturbed in different locations cause of there rigdbody^^ Damn

avatar image unity_ek98vnTRplGj8Q smokk83 · Jan 20, 2020 at 09:32 PM 0
Share

There are several different things you could do to fix this depending on what you want the behavior to be. Do you want these disks to simply stop when they hit a tree? Do you want them to slide around the tree and keep going towards the target?

avatar image smokk83 unity_ek98vnTRplGj8Q · Jan 21, 2020 at 07:43 AM 0
Share

Perfekt would it when then slide aruound the tree.

And right now they are looking for the shortest way, without rigidbody they breezese throug my surface which is a sphere collider that acts like a planet^^

Show more comments

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

154 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to get the position of a new Instantiated prefab 1 Answer

Instantiating Prefabs Lags in Android 0 Answers

I need to move a clone of a prefab to a specific position after it instantiates 1 Answer

transform of instantiated bullet via ui button press not following the player 0 Answers

I want to make a gun with 2 parts and follow their positions and scales 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