Rotation when instantiated

Most of you will not need this if you are following along with @Rick_Davidson but I decided to make my game side scrolling. This meant that I rotated my laser prefab but Quaternion.identity ignores the prefab rotation. My solution was to create a new variable to check the rotation.

The same logic could be used to spawn enemies at other rotations as well.

Quaternion laserRotation = playerLaser.transform.rotation;
Instantiate(playerLaser, transform.position, laserRotation);

I also found myself needing to instantiate at a particular rotation. I was making my own art assets (I also did some of the 2D art course, although my art is still too novice to share), and needed to figure out how to instantiate my characters I had drawn vertically rotated around so they would face the player. I did it using the line:

var newEnemy = Instantiate(waveConfig.GetEnemyPrefab(), waveConfig.GetWaypoints()[0].transform.position, Quaternion.Euler(0,0,180));

The important bit being “Quaternion.Euler(0,0,180)”. The Quaternion.Euler command lets you prescribe a particular rotation about x, y, or z. Since we’re working in 2D, the only one you’ll need to modify is the z-value (the third one; the others can stay 0). 180 degrees flips the image around, so where my art was drawn “upright” with their heads pointing up, this command turns them around so their heads point down. The .Euler extension seemed easier to understand than the other options I was reading about.

I saw that option in the Unity docs but even they advise against altering the values manually. The way I used just means I can visually set the enemy etc as I want them and the code just uses the prefab settings.

Privacy & Terms