Instantiated Object looses prefab's rotation
Asked Answered
J

12

0

I’ve created an object, say a capsule, and in the Scene window I’ve rotated it 90.0 degrees, and now its a bullet.

However when I instantiate this object, the rotation I’ve applied the object/prefab doesn’t follow through, and it still instantiates in its original orientation…

Any ideas?

Jaredjarek answered 14/3 at 10:22 Comment(4)

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

Penman

just tried this, and again, no luck =/

Jaredjarek

I'm instantiating with this code: var tempHundred: Transform; tempHundred = Instantiate(hundred,transform.position, transform.rotation); could that be the problem?

Jaredjarek

What is this attached to? I.E. Which transform is getting passed to it? Because that's what's going to determine it's position and rotation.

Penman
R
0

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.

Resign answered 6/6, 2023 at 2:12 Comment(5)

The code is part of the enemyscript.js, this controls enemy lives etc. i have this little bit of code: if(EnemyLives<=0) { Destroy(gameObject); var tempHundred: Transform; tempHundred = Instantiate(hundred,transform.position, transform.rotation); } 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!)

Jaredjarek

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!

Jaredjarek

I've done it! haha var tempHundred: Transform; tempHundred = Instantiate(hundred, transform.position, Quaternion.Euler(90, 0, 0)); I am happy now

Jaredjarek

It's the correct way: transform.position is the point at which you should instantiate the new object. You should also use transform.rotation instead 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.

Resign

He is talking about the new Input System. And that event is probably provided through the PlayerInput component with an Input Actions asset assigned. I agree with you, he should show some code. The new Input System is fairly complex, with multiple ways of achieving the same thing. It would be very hard to help him without taking a look at the code.

Fordham
I
0

Maybe this is a little old, but I ran over the same issue and came up with two simple solutions:

Solution 1

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.

Solution 2

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!

Ivo answered 14/3 at 10:21 Comment(5)

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.

Concernment

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

Kino

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

Nappy

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

Dygall

I agree, that first method worked wonders for me as well! I know it's an older post but I wanted to reiterate it's efficacy. I was stuck on why my objects were not following the prefab transform. This works so I'll be using this from now on :). Thanks! BTW I'm using Unity 5.

Cogent
N
0

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 = Instantiate(
    hundred ,
    transform.position ,
    hundred.transform.rotation
);

Hope this helps, I just tried it and it should work very easily

Neron answered 14/3 at 10:8 Comment(3)

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

Kino

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

Growing

If you don't show your code, we won't be able to help you

Fordham
M
0

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
);
Maggie answered 14/3 at 10:23 Comment(1)

Thanks! This worked for me. Quick and easy

Apery
A
0

When Instantiating, change the rotation in the Instantiate Line using Quaternion.Euler. I changed the line below adding this:

var 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

Anesthetist answered 14/3 at 10:6 Comment(1)

This worked perfectly for me - thank you :)

Duckett
D
0

Other simple way to solve this issue without creating empty parent game object:

GameObject pc = (GameObject) Instantiate (Prefab, position, rotation, transform);
pc.transform.Rotate (new Vector3(rotationWished.x,rotationWished.y,rotationWished.z));

Where you choose in Vector3 rotationWished which rotation you want.

Dolerite answered 6/6, 2023 at 2:23 Comment(0)
G
0

@Dolerite Answer worked for me.

This is what I used to get my gun to go back to pointing in front of player. But I am no pro so if any of you that know your stuff please tell me if I should do this some other way. This is for a gun shop when you click buy button it spawns it under your player. I am not real sure if I need that Time.deltaTime in there.

public GameObject Player; //Parent 
public GameObject M4A122; //Prefab

public void M4a122()
{
    Vector3 position = new Vector3(0, 1.9f, 0 * Time.deltaTime);
    Vector3 rotation = new Vector3(0, 180, 0 * Time.deltaTime);      
    GameObject obj = Instantiate(M4A122);       
    obj.transform.position = Player.transform.position;
    obj.transform.rotation = Player.transform.rotation;
    obj.transform.parent = Player.transform;
    obj.transform.Rotate(new Vector3(0, 180, 0 * Time.deltaTime));
}

I just want to thank everyone for all the help. I look at so many posts.

Groovy answered 14/3 at 10:9 Comment(0)
F
0

Sometimes instantiated objects collide with other objects like weapon or spawn point and loose rotation. In my case I fixed it with Layer collision matrix. Other way is to make your spawn point a little farther then weapons.

Francklyn answered 27/10, 2019 at 22:19 Comment(0)
H
0

Solution 3

  • Open prefab
  • add an empty GameObject under your top level GameObject
  • carefully put all other game objects as children to the new empty game object.
  • Rotate new empty game object.
  • Move all objects back to original parent object.
  • Delete the new empty GameObject.
Homogeneous answered 14/3 at 10:11 Comment(0)
R
0

In the prefab’s script, in the Start method set the rotation -.-

Reprobate answered 14/3 at 10:24 Comment(0)
H
0

just use this:

Instantiate(
    _myPrefab_ ,
    pos ,
    Quaternion.identity * _myPrefab_.transform.localRotation
);
Hendren answered 14/3 at 10:10 Comment(0)
H
0

Solution number 1 worked for me. Thanks!

The rotation of my prefab was getting reset when I instantiated a network object. But I put my prefab in an empty game object and the rotation got saved afterwards. Cheers!

Hubris answered 15/3 at 4:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.