Search Unity

Question Rotating prefabs with the RotateAround function

Discussion in 'Scripting' started by julien83franceschi, May 29, 2023.

  1. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    235
    Hi everyone,

    I'd like my grid and rectangle to rotate 90° at their centers and at the center of the scene (Vector3.zero).

    To do this, I use the RotateAround function.

    Here's my code:
    Code (CSharp):
    1.    public GameObject plane, rectangle;
    2.    public void OnMouseUp()
    3.     {
    4.         rectangle.transform.RotateAround(Vector3.zero, Vector3.up, -90f);
    5.         plane.transform.RotateAround(Vector3.zero, Vector3.up, -90f);
    6.     }
    The problem is that the rectangle and plane don't rotate.

    I assume this doesn't work because the rectangle and plane are prefabs.

    If you have any idea.

    Your help is most welcome,

    A+
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,727
    I assume it doesn't work because the code isn't running.

    This is almost always the correct naive assumption.

    Prove me wrong with a Debug.Log()

    If it isn't running, then go verify the chain of things you think will result in the code running and see which part is not living up to your expectations.
     
  3. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    235
    Hi there,

    I placed a capsule next to the grid and gave it the following code:

    Code (CSharp):
    1. public void OnMouseUp()
    2.     {
    3.         rectangle.transform.RotateAround(Vector3.zero, Vector3.up, -90f);
    4.         plane.transform.RotateAround(Vector3.zero, Vector3.up, -90f);
    5.         Debug.Log("Hello");
    6.     }
    and the console responds "Hello" when the capsule is clicked.

    The problem is that the rectangle and the plane don't rotate.

    Looking forward to hearing from you,

    A+
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,727
    It certainly does for me. Try it in my enclosed package.

    Before:

    Screen Shot 2023-05-29 at 10.38.00 AM.png

    After:

    Screen Shot 2023-05-29 at 10.38.20 AM.png
     

    Attached Files:

    Yoreki likes this.
  5. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,108
    How do you even notice whether a plane and a grid rotated by 90 degrees if not through transform?
    Should they turn red and warp to another dimension? No, they stay exactly the same.
    Can't you at least try with something much more obvious, like 45 degrees?

    I mean, maybe it's just me, but I would find that much simpler than posting on a forum...
     
  6. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    235
    Hi everyone,

    The problem is that I would like to rotate all the small planes that make up the 8x8 grid with a few empty cells (the small planes are stored in the objArray array).
    The code is more explicit:



    To do this, I use the following code:

    The code is more explicit:

    Code (CSharp):
    1. void Update()
    2.  
    3.     {
    4.      
    5.         if (Input.GetMouseButtonDown(0))
    6.             {
    7.                 for (int i = 0; i < 64; i++)
    8.      
    9.                 grilleRouge.objArray[i].RotateAround(Vector3.zero, Vector3.up, -90f);
    10.             }
    11.     }
    The problem is that when I click, nothing happens.
    How can I get the objArray to rotate all the small planes by 35°, or even 90°?

    Thank you very much,

    A+
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,727
    Once a prefab is dragged into a scene, the position and rotation is "captured" by the scene and driven there.

    You would need to re-drag the prefab into the scene.

    Try it yourself:

    - make a prefab
    - drag it in
    - rotate the prefab
    - drag it in again

    Compare the two prefabs.
     
  8. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    235
    The problem is that I can't drag prefabs into a scene.
    The little planes that make up the grid are created with code (they're stored in the objArray array):

    Code (CSharp):
    1. for (float i = 0; i < Lignes; i++)
    2.  
    3.             pour (float j = 0; j < Colonnes; j++)
    4.             {
    5.                  if (Grille[i, j] != -1)
    6.                 {
    7.                     Vector3 worldPosition = new Vector3(i + centrage, 0f, j + centrage) * 1.6f ;
    8.                     obj = Instantiate(gridCellPrefab, worldPosition, Quaternion.identity);
    9.                     obj.transform.parent = empty.transform;
    10.                     obj.name = "cube" + k ;
    11.                     objArray[k] = obj ;
    12.                     k++;
    13.                   }
    14.               }
    15.                     k++;
    They are then reused in the following code:
    Code (CSharp):
    1. void Update()
    2.     {
    3.    
    4.         if (Input.GetMouseButtonDown(0))
    5.             {
    6.                 for (int i = 0; i < 64; i++)
    7.    
    8.                    objArray[i].RotateAround(Vector3.zero, Vector3.up, -90f);
    9.             }
    10.     }
    The problem is that the small shots don't turn.

    Thanks for your help.
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,727
    There's where you are wiping out the rotation: you're setting it to
    Quaternion.identity
    . :)
     
  10. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    235
    What do you mean by :

    "This is where you remove the rotation: you set it on Quaternion.identity"

    Thanks for your help,

    A+
    .
     
  11. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,727
  12. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    235
    In fact, my real code is as follows:

    Code (CSharp):
    1. void Update()
    2.     {
    3.  
    4.         if (Input.GetMouseButtonDown(0))
    5.             {
    6.                 for (int i = 0; i < 64; i++)
    7.  
    8.                    grilleRouge.objArray[i].RotateAround(Vector3.zero, Vector3.up, -90f);
    9.             }
    10.     }
    And grilleRouge is a C# file containing as variable:
    public static Transform[] objArray = new Transform[Lignes * Colonnes];

    Et qui stocke les plans dans le tableau:
    Code (CSharp):
    1. objArray[k] = obj ;
    Merci encore,

    A+
     
    Last edited: May 30, 2023
  13. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    235
    Hello everyone,

    I tried the following code:
    Code (CSharp):
    1. void Update()
    2. {
    3.     for (int i = 0; i < 64; i++)
    4.     {
    5.         if (grilleRouge.objArray[i] != null)
    6.         {
    7.             Transform objTransform = grilleRouge.objArray[i].transform;
    8.             objTransform.RotateAround(Vector3.zero, Vector3.up, -90f);
    9.         }
    10.     }
    11. }
    But nothing turns (35° or 90°)

    Looking forward to your help, or if you have code

    A+
     
    Last edited: May 31, 2023
  14. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,727
    Start printing out
    objTransform.name
    ... then CHANGE those names...

    Code (csharp):
    1. objTransform.name = "FOOOOOO!";
    Does the grid names change? IF not then you're not changing the objects you think.

    Find out what is happening! You are our only hope.
     
    orionsyndrome likes this.
  15. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,108
    @julien83franceschi You're clearly doing something wrong.
    You either target wrong objects, or something like that.

    If you could just make a simple class that does approximately what you want, so that we can run it, debug it, fix it for good. Currently you're not making any progress, and it is also very difficult to pinpoint your mistakes because you're not showing the code exactly. And there is a language barrier. Debugging issues like this should last 5-10 minutes, maybe even an hour on a somber day. Definitely not days.