Search Unity

How do I rotate an instantiated prefab?

Discussion in 'Scripting' started by forsrobin, Feb 18, 2018.

  1. forsrobin

    forsrobin

    Joined:
    Feb 10, 2018
    Posts:
    4
    Hello !

    Iv'e made a script so I can instantiate a prefab at my current mouse position for my building game.
    But right now the prefab only have one rotation and when I press "R" I want to rotate it 90 deg.
    This is my current code for instantiating a prefab.

    Code (CSharp):
    1.     private Transform currentBuilding;
    2.     private Grid grid;
    3.     void Awake()
    4.     {
    5.         grid = FindObjectOfType<Grid>();  
    6.     }
    7.  
    8.     void Update()
    9.     {
    10.         if (currentBuilding != null)
    11.         {
    12.             Vector3 m = Input.mousePosition;
    13.             m = new Vector3(m.x, m.y, transform.position.y);
    14.             Vector3 p = grid.GetNearestPointOnGrid(GetComponent<Camera>().ScreenToWorldPoint(m));
    15.             currentBuilding.position = new Vector3(p.x, 0, p.z);
    16.         }
    17.  
    18.     }
    19.  
    20.     public void SetItem(GameObject b)
    21.     {
    22.  
    23.         currentBuilding = ((GameObject)Instantiate(b)).transform;
    24.  
    25.     }
    Any ideas on how I can rotate the instantiated prefab?
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712