Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

instantiate Prefab3d into canvas with a rotation parallel to the camera

Discussion in 'Immediate Mode GUI (IMGUI)' started by joseluisruedafotografo, Jan 8, 2021.

  1. joseluisruedafotografo

    joseluisruedafotografo

    Joined:
    May 16, 2018
    Posts:
    2
    Hi how are you, I'm new to unity :)
    I am trying to create an inventory of a stack of objects that fall on the left side canvas. I manage to create the instances but I don't know what rotation to use so that they are parallel to the camera's vision

    when I instantiate the first time when the camera is with the rotation x to 0 the objects have a good rotation
    But when the camera rotates in the x-axis looking up, the objects are refreshed with rotation not parallel to the camera.

    You should be able to indentify the objects using the local world of the canvas.
    any ideas

    Thanks a lot
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class generatorObjectCanvas : MonoBehaviour
    7. {
    8.  
    9.     public Transform container;
    10.     public Transform ball;
    11.     public Transform startPos;
    12.  
    13.     public Camera mainCamera;
    14.  
    15.     public int numberOfRows;
    16.     public int objectPerRow;
    17.     public float spacing;
    18.  
    19.   public Queue<GameObject> myLIstaObjetosCanvas = new Queue<GameObject>();
    20.  
    21.    
    22.  
    23.     // Start is called before the first frame update
    24.     void Start()
    25.     {
    26.        
    27.     }
    28.  
    29.     // Update is called once per frame
    30.     void Update()
    31.     {
    32.         foreach (Transform child in container)
    33.         {
    34.             if (child.CompareTag("ball"))
    35.             {
    36.                 child.transform.rotation = mainCamera.transform.rotation;
    37.             }
    38.  
    39.         }
    40.  
    41.     }
    42.  
    43.  
    44. // this is where i instantiate the objects//
    45.  
    46.         public void RefrescoCanvas( Queue<GameObject> myLIsta)
    47.     {
    48.         myLIstaObjetosCanvas = myLIsta;
    49.  
    50.         DestroyChild();
    51.         int row = 0;
    52.         foreach (var data in  myLIstaObjetosCanvas)
    53.         {
    54.            
    55.             Vector3 startingPos = new Vector3(startPos.position.x , startPos.position.y + row * spacing, startPos.position.z);
    56.             Transform _ball = Instantiate(data.transform, startingPos, quaternion.identity);
    57.                     _ball.SetParent(container);
    58.                     _ball.transform.localScale = new Vector3(40 , 40, 40);
    59.             row++;
    60.         }
    61.  
    62.         //for (int row = 0; row < myLIsta.Count; row++)
    63.         //{      
    64.  
    65.         //    for (int col = 0; col < objectPerRow; col++)
    66.         //    {
    67.         //        GameObject objeto = myLIsta.Dequeue() as GameObject;
    68.         //        Vector3 startingPos = new Vector3(startPos.position.x + col * spacing, startPos.position.y - row * spacing, startPos.position.z);
    69.         //        Transform _ball = Instantiate(objeto.transform, startingPos, Quaternion.identity);
    70.         //        _ball.SetParent(container);
    71.         //        _ball.transform.localScale = new Vector3(20, 20, 20);
    72.         //    }
    73.         //}
    74.     }
    75.  
    76.     public void DestroyChild() {
    77.  
    78.         foreach (Transform child in container)
    79.         {
    80.             if (child.CompareTag("ball"))
    81.             {
    82.                 Destroy(child.gameObject);
    83.  
    84.             }
    85.            
    86.         }
    87. }
    88.  
    89.  
    90.  
    91. }
    92. [code]
    93. [ATTACH=full]769324[/ATTACH]
     
unityunity