- Home /
Instantiated Object looses prefab's rotation
I've created an obect, say a capsule, and in the scene windown i've rotated it 90 degrees, and now its a bullet. However when I instantiate this object, the rotation i've aplied the object/prefab doesn't follow through, and it still instantiates in its original orientation...
Any ideas?
Did you rotate the object before making it a prefab? If not, try doing so.
I'm instantiating with this code:
var tempHundred: Transform;
tempHundred = Instantiate(hundred,transform.position, transform.rotation);
could that be the problem?
What is this attached to? I.$$anonymous$$ Which transform
is getting passed to it? Because that's what's going to deter$$anonymous$$e it's position and rotation.
Answer by GeorgeRigato · Nov 29, 2012 at 09:44 PM
maybe this is a little old, but I ran over the same issue and came up with two simple solutions:
1st- Place your object inside a empty game object at the desired rotation and create the prefab from this empty one. When you instantiate your prefab at identity rotation, the actual object inside the parent will keep the original saved rotation.
2nd- use this code to initialize object: Instantiate (myPrefab, new Vector3 (x ,y, z), myPrefab.transform.rotation); To be honest, I've not tried the second one, but I'm assuming it will spawn it at the prefab stored rotation.
good luck!
Thanks dude, Your 1st method worked like a charm. I watched so many tutorials for gun shooting but all use sphere as projectile or fps so they never face problem with the rotation. Your simple trick made my day.
Actually the second solution will work. You can do this in the weapon code
void PullTrigger()
{
Transform baseAmmoTransform = loadedAmmo.GetComponentsInChildren<Transform>(true)[0];
Quaternion baseAmmoRotation = baseAmmoTransform.rotation;
Instantiate(loadedAmmo, transform.position, ammoRotation);
GameObject shot = (GameObject)Instantiate(loadedAmmo, transform.position, ammoRotation);
shot.send$$anonymous$$essage("GoTowards", transform.forward);
}
And then in the ammo code
void GoTowards(Vector3 direction)
{
transform.rotation = Quaternion.LookRotation(direction) * transform.rotation;
rigidbody.velocity = direction*speed;
}
This will make your bullet go in the direction the weapon is facing, and use the rotation of the prefab as a base for the further rotation needed to make it face the direction it's going.
Thanks for this code. $$anonymous$$uch better solution because it will then let you setup the orientation of specific prefabs in the inspector. You only made a small mistake on this line : GameObject shot = (GameObject)Instantiate(loadedAmmo, transform.position, ammoRotation);
the last word should be baseAmmoRotation ins$$anonymous$$d of just ammoRotation (because that the name you initiated the variable with.)
I modified this code to spawn random monster from an array and I could then rotate each monster prefabs in their correct direction.
@$$anonymous$$stino: Your solution #2 works perfectly for me. At first I overlooked the myPrefab.transform.rotation, but that's exactly what I was trying to do in rotating my prefab. Thanks!
I'd still like to know why rotating the prefab otherwise doesn't carry over to the instantiated object...
@brag42 You were setting the rotation when you instantiated it, in Instantiate(hundred,transform.position, transform.rotation);
Solution #1 will prove difficult when the object has a navmeshagent attached to it. Solution #2 works perfectly.
2nd method was exactly what I was looking for, thanks a bunch!
Answer by aldonaletto · Jul 21, 2011 at 05:10 PM
Which rotation are you using in the Instantiate instruction? If it's Quaternion.identity, the bullet keeps the same rotation as the prefab. If it is transform.rotation, then the object to which you've attached the script probably is rotated too - its rotation is combined to the original prefab's, producing the wrong direction.
If that's the case, add an empty game object to your weapon as a child, align it to have the blue axis pointing the correct shoot direction, then move your script to this object - the bullet will be instantiated with the correct orientation.
The code is part of the enemyscript.js, this controls enemy lives etc. i have this little bit of code:
if(EnemyLives
But that don't work, i tried :
tempHundred = Instantiate (hundred, Vector3(0,2,-14), Quaternion.identity);
which didn't do the rotation either, neither did it spawn at the enemies location (but i guess that cause the code didn't tell it too!)
var tempHundred: Transform;
tempHundred = Instantiate(hundred, new Vector3(-1.48088, 2, -12.85), Quaternion.Euler(90, 0, 0));
The above code fixed it! (i did some learning). however trying to get it to spawn at enemy location.. will post another question! Thank you!
I've done it! haha
var tempHundred: Transform; tempHundred = Instantiate(hundred, transform.position, Quaternion.Euler(90, 0, 0));
I am happy now
It's the correct way: transform.position is the point at which you should instantiate the new object. You should also use transform.rotation ins$$anonymous$$d of Quaternion.Euler(...) to keep the enemy orientation, but case this refuses to work you can keep the Quaternion.Euler thing. You will have problems only case the enemy dies in another orientation, because the new object will always be spawned facing the same side.
Answer by Northrop · May 22, 2015 at 02:52 PM
I had similar problem. I am instantiating a prefab. I want to keep rotation but it to appear at the SpawnPoint. That is what I used:
Instantiate(prefab, SpawnPoint.transform.position, prefab.transform.rotation);
Answer by Guardian2300 · Apr 09, 2014 at 04:14 PM
when your instantiating you'll notice that for your rotation you have transform.rotation which is saying that rotate your instantiated object by the transform that the script is running off of. So If you want to keep your rotation from prefab
Should work like this
var tempHundred: Transform;
tempHundred = Instantiate(hundred,transform.position, hundred.transform.rotation);
Hope this helps, I just tried it and it should work very easily
@Guardian: thanks, this also worked for me. I rotated the prefab how I wanted it and then did the "hundred.transform.rotation" like you did, where hundred was the name of the variable storing the GameObject.
Answer by Fiercest · Jul 12, 2016 at 12:38 PM
When Instantiating, change the rotation in the Instantiate Line using Quaternion.Euler, I changed the line below adding this.
var tempHundred: Transform;
tempHundred = Instantiate(hundred,transform.position, transform.rotation * Quaternion.Euler (0, 0, 0)) ;
Fill in 0,0,0 with x,y,z
It will rotate every object coming out. Had the same issue hope this helps