- Home /
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
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);
}
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
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?
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^^
Your answer
Follow this Question
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