- Home /
How do i instantiate a prefab with the same rotation as the gameobject?
How do i instantiate a prefab with the same rotation as the gameobject? This is my code
if (Input.GetKeyDown(KeyCode.E) && Skills >= 1 && Class == 1 && Fire1Ready)
{
Instantiate(FireBall, transform.position, transform.rotation);
Fire1Ready = false;
CurrentAbility = 1;
Invoke(nameof(Cooldown), Fire1Cooldown);
}
You mean prefab?
GameObject g = Instantiate (FireBall);
g.transform.position = transform.position;
Here you make obj with prefab values and only position changing.
@MilitaryG No it should have the same rotation as the object with the script. The script is on the player and im trying to shoot a fireball.
Well you are
Maybe it's script elsewhere than you expect
Oh yeah i am i seem to have another problem Thx
Answer by RobAnthem · Nov 28, 2021 at 03:53 PM
You need a reference to the object you want to match the rotation of, unless it's the Player itself casting the fireball from the players script. There are 2 ways to do it though.
Instantiate(fireball, fireballSpawnPos, playerTransform.rotation);
or
GameObject go = Instantiate(fireball);
go.transform.position = fireballSpawnPos;
go.transform.forward = playerTransform.forward;
When instantiating, the first param is the object to instantiate, second param is the position OR transform to parent. IF you o position param, you can also set rotation param. Of course it WILL be in world coordates. If you want to set some sort of local rotation you'll have to instantiate it first with a reference to it. There is little difference in the result if you do Rotation or Forward as the end result is the same. Still I prefer using directions over rotations. Whatever you do though just leave the Eulers alone.