Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
10
Question by BobbleHead · Jul 21, 2011 at 03:43 PM · rotationinstantiatetransformprefab

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?

Comment
Add comment · Show 4
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 Chris D · Jul 21, 2011 at 03:58 PM 0
Share

Did you rotate the object before making it a prefab? If not, try doing so.

avatar image BobbleHead · Jul 21, 2011 at 04:54 PM 0
Share

just tried this, and again, no luck =/

avatar image BobbleHead · Jul 21, 2011 at 05:00 PM 0
Share

I'm instantiating with this code:

     var tempHundred: Transform;
     tempHundred = Instantiate(hundred,transform.position, transform.rotation);

could that be the problem?

avatar image Chris D · Jul 21, 2011 at 05:11 PM 0
Share

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.

10 Replies

· Add your reply
  • Sort: 
avatar image
32

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!

Comment
Add comment · Show 10 · 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 Yogesh Pawar · Jun 19, 2013 at 07:28 PM 0
Share

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.

avatar image Agostino · Dec 02, 2013 at 06:47 PM 2
Share

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.

avatar image Frostblood Agostino · May 13, 2017 at 07:29 PM 0
Share

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.

avatar image brag42 · Feb 07, 2014 at 05:18 AM 0
Share

@$$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...

avatar image Bonfire-Boy brag42 · Oct 24, 2019 at 11:31 AM 0
Share

@brag42 You were setting the rotation when you instantiated it, in Instantiate(hundred,transform.position, transform.rotation);

avatar image Simon-Larsen · Dec 30, 2014 at 12:24 AM 0
Share

Solution #1 will prove difficult when the object has a navmeshagent attached to it. Solution #2 works perfectly.

avatar image RottingBum · Oct 13, 2015 at 07:39 PM 0
Share

2nd method was exactly what I was looking for, thanks a bunch!

Show more comments
avatar image
5

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.

Comment
Add comment · Show 4 · 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 BobbleHead · Jul 22, 2011 at 01:02 PM 0
Share

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!)

avatar image BobbleHead · Jul 22, 2011 at 02:09 PM 1
Share
     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!

avatar image BobbleHead · Jul 22, 2011 at 02:12 PM 0
Share

I've done it! haha

var tempHundred: Transform; tempHundred = Instantiate(hundred, transform.position, Quaternion.Euler(90, 0, 0));

I am happy now

avatar image aldonaletto · Jul 22, 2011 at 04:38 PM 1
Share

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.

avatar image
4

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);

Comment
Add comment · Show 1 · 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 satyajitdas · Aug 10, 2019 at 11:06 AM 0
Share

Thanks! This worked for me. Quick and easy

avatar image
1

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

Comment
Add comment · Show 2 · 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 brag42 · Apr 10, 2014 at 01:26 AM 0
Share

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

avatar image jlfreund · Jun 16, 2017 at 03:36 AM 0
Share

spent 3 hours, before finding this comment :) +1

avatar image
1

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

Comment
Add comment · Show 1 · 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 SeanMayich · Jan 10, 2020 at 04:22 PM 0
Share

This worked perfectly for me - thank you :)

  • 1
  • 2
  • ›

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

25 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

Related Questions

Planes not instantiating properly on gameobject location 2 Answers

Trying to make a bullet fire at the mouse using transform.up 0 Answers

How to make sprites instantiate based on randomly generated integer? 1 Answer

transform position and rotation of instantiated object 1 Answer

Spawning unique prefabs at different transform posititions 0 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