Rotation issue when not in centre of scene?

Can somebody help me out here?! This is doing my head in. The rotation on my script is all kind of messed up.


Each of the green squares is a spawn point for this script. The spawn point for the set of blue boxes is in the centre (0,1.5,0) of the scene. The other red boxes aren’t. Each of them are rotated at a 20 degree angle and the blue set is what I’m expecting. The others seem to change the distance from the green spawn point depending on where they are in the scene which is causing issues.

Can anybody suggest how to fix this issue so the rotation result is the same as the blue set regardless of the location of the spawn point in the scene? The script is below…

productArray = new GameObject[productAmount];
         for (int count = 0; count < productAmount; count++)
         {
             Vector3 Position = new Vector3(transform.position.x, transform.position.y, transform.position.z + count * distance);
             Quaternion rotation = Quaternion.Euler(new Vector3(0, ProductAngle, 0));
             GameObject pile = Instantiate(Product, rotation * Position, Quaternion.identity);
             pile.transform.rotation = Quaternion.Euler(new Vector3(0, IndividualAngle, 0));
             productArray[count] = pile;
         }

Your line 5 rotation is being applied to the Position you use in line 6 (where you multiply by the rotation) that you pass into Instantiate, so the position has been rotated at that point.

Instead, just position it with Position, THEN rotate it.

Personally I like to break each of these things into steps:

  • instantiate
  • position it
  • rotate it

All on separate lines of code to make the steps clear.

So to start, you’re suggesting I get rid of Line 5 where it is rotating it?

No, read carefully. You asked why your positions are wrong, and what I said is that YOU are rotating the position by taking rotation and applying it with multiplication in line 6.

If you NEED that rotation later, of course you keep it.

I’m saying your application of that rotation to the Position in line 6 is your actual problem making the positions incorrect.

Ah I see what you mean thanks! The use of the rotation earlier is causing problems because it’s rotating the initial spawn point. I’ve attempted to apply the rotation afterwards but it results in rotating the individual clones on their own axis rather than the direction the pile goes which is what I’m after and I’m unsure where to apply the rotation elsewhere as this seems to have the same result of rotating the individual cloned objects