Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Rotation issue when not in centre of scene?

Discussion in 'Scripting' started by RDG_Admin, Mar 15, 2021.

  1. RDG_Admin

    RDG_Admin

    Joined:
    Oct 4, 2014
    Posts:
    22
    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...

    Code (CSharp):
    1. productArray = new GameObject[productAmount];
    2.          for (int count = 0; count < productAmount; count++)
    3.          {
    4.              Vector3 Position = new Vector3(transform.position.x, transform.position.y, transform.position.z + count * distance);
    5.              Quaternion rotation = Quaternion.Euler(new Vector3(0, ProductAngle, 0));
    6.              GameObject pile = Instantiate(Product, rotation * Position, Quaternion.identity);
    7.              pile.transform.rotation = Quaternion.Euler(new Vector3(0, IndividualAngle, 0));
    8.              productArray[count] = pile;
    9.          }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    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.
     
  3. RDG_Admin

    RDG_Admin

    Joined:
    Oct 4, 2014
    Posts:
    22
    So to start, you're suggesting I get rid of Line 5 where it is rotating it?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    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.
     
  5. RDG_Admin

    RDG_Admin

    Joined:
    Oct 4, 2014
    Posts:
    22
    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