• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Make a game object in a scene into a prefab with script 1 Answer

Question about instatiating prefab/clone character and how they work/interact with non prefeb buttons 0 Answers

When should I use prefabs? 2 Answers

how to call the animation of prefab 0 Answers

Call a function in an instanciated prefab has no effect on the prefab 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges