- Home /
Instantiate is throwing my throwing my objects?
So I'm creating a spawning script to spawn my AI using Instantiate. I'm passing the spawner's location and rotation and the game object I want to have spawned. The script works perfectly except that for some reason when the object is instantiated, it is thrown at about 200 meters/second and I have no idea how to fix it. because when I set the vector3 position as 0, 0, 0 it works perfectly fine, but once the location is moved from the origin, the object it actually thrown at that vector. I'm extremely confused.
Here's the sample of code where I instantiate the the new objects. I'm basically trying to instantiate them at the spawner's position.
easyAI, mediumAI, and toughAI are all primitive GameObjects with an attached rigidbody purely used for testing.
private void spawn(int spawnNum){
if(spawnNum < chanceEasy){
Instantiate(easyAI, gameObject.transform.position, gameObject.transform.rotation);
n_AI++;
lastSpawnTime = Time.time;
spawnTime = r.Next(minWait, maxWait);
spawnChance = r.Next(0, 100);
}else if(spawnNum > chanceEasy && spawnNum < (100 - chanceTough)){
Instantiate(mediumAI, gameObject.transform.position, gameObject.transform.rotation);
n_AI++;
lastSpawnTime = Time.time;
spawnTime = r.Next(minWait, maxWait);
spawnChance = r.Next(0, 100);
}else if(spawnNum > (100-chanceTough)){
Instantiate(toughAI, gameObject.transform.position, gameObject.transform.rotation);
n_AI++;
lastSpawnTime = Time.time;
spawnTime = r.Next(minWait, maxWait);
spawnChance = r.Next(0, 100);
}else {
Debug.Log("Error, Spawn Number Out of range");
}
}
If anyone could offer any insight to my problem it would be appreciated. I am baffled by what is going on. Thank you
well I removed the rigidbody from each object and they seem to be spawning in the correct location. So I guess it has to do with the rigidbodies.
Yeah, Unity has some weird stuff like that lol =p. $$anonymous$$y Instantiate problem was that when my object was spawning, the longer it spawned for the more it traveled down the Z axis away from the Spawn location I originally wanted due to my prefab not being 0'ed out.
well now what if I want the prefab to have a rigidbody and still spawn in the right location? What do you think I could do?
Shot in the dark. If you are instantiating your objects from a scene object and not from a prefab, note that they will inherit all the properties of the object at the time of the instantiate. So if the object you are instantiating has a velocity, the instantiated object will as well. You can "fix" the problem by setting the velocity to zero.
@ zBlanco Idk, I'm trying to avoid Rigidbodies & movement until I understand physics better
Answer by voporak5 · Aug 05, 2013 at 03:19 AM
Idk if this will help but 1, is your prefabs' locations set to 0,0,0 and also do they have a rigidbody? I know somethings if you throw a rigidbody on they do weird stuff like try throwing it onto your Main Camera and the Camera will just start floating up lol.
Your answer
Follow this Question
Related Questions
How to Spawn after checking if the clones are destroyed. 1 Answer
How Do I Add An Instantiated Object To An Array? 3 Answers
Enemy spawn, round based 1 Answer
Having Trouble with Instantiating an object on an axis 2 Answers
player spawning 0 Answers