- Home /
Prefab always spawns with the wrong orientation
I have a prefab which consists of a knife model. In the Prefab view, I set its rotation to z = -90. Despite that, everytime I spawn a knife, it always spawns at z = 0. I don't know how to tackle this issue.
First image: the knife in its prefab view.
Second image: the knife when I spawn it. Do note as to how it spawns 'sleeping'.
Below is the code that I use to spawn a knife: -
private void FixedUpdate()
{
if (Input.GetKeyUp(KeyCode.Mouse0) && coolDownTimer == 0f)
{
GameObject projectile = Instantiate(knife, origin.position, transform.localRotation);
projectile.GetComponent<Rigidbody>().AddForce(origin.forward * force * Time.deltaTime, ForceMode.Impulse);
coolDownTimer = coolDown;
}
}
Answer by venomjadhav · May 22, 2021 at 06:11 AM
Yeah, I solved this myself (happens most of the time). And the solution was with the object I was using to spawn the prefab. I first changed transform.localRotation
to origin.rotation
, where origin
is the gameobject I use to spawn the prefab. And then I changed the rotation of origin
to z = -90 in the Inspector. And it works perfectly.
Answer by Monsoonexe · May 21, 2021 at 05:45 PM
I notice your code on line 5 says to use the spawning object's transform.localRotation
. localRotation
is relative to its position in the hierarchy, not to the world. It's possible you want the orientation of the spawning object in WorldSpace. Try changing localRotation
to just rotation
.