- Home /
 
 
               Question by 
               Carbongrip · Jan 03, 2014 at 12:32 AM · 
                c#rotationinstantiateobject  
              
 
              Rotate the Object 90 Degrees
I have been looking around but can't find how to say rotate a object to instantiate so say I hit the left and right arrow keys to move the object say like 90 degrees so that it goes from a wall facing one way to the other. For example building a square house requires the object be rotated…
So the wall needs to go from this - to this | Here is the code…
 using UnityEngine;
 using System.Collections;
 
 public class Placement : MonoBehaviour
 {
     public bool objectplace = false, newobjectselected = false;
     public GameObject block;
     private int permanentLayer = 20;
     private GameObject nextToPlace;
     
     void Update ()
     {
         if(objectplace == true)
         {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
 
             if (Physics.Raycast(ray, out hit, 14, 1 << permanentLayer))
             {
                   float snap = 0.25f;
                 Vector3 snapped = new Vector3(Mathf.RoundToInt(hit.point.x/snap)*snap, Mathf.RoundToInt(hit.point.y/snap)*snap, Mathf.RoundToInt(hit.point.z/snap)*snap);
                 nextToPlace.transform.position = snapped;
                 if(Input.GetKeyDown("right"))
                 {
                     nextToPlace.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
                 }
                   nextToPlace.SetActive(true);
                   if (Input.GetButtonDown("Fire1"))
                   {
                     nextToPlace.layer = permanentLayer; // place it, so we can put things on top
                     // get a new one:
                     nextToPlace = Instantiate(block, Vector3.zero, Quaternion.identity) as GameObject;
                     nextToPlace.SetActive(false);
                     objectplace = false;
                   }
             }
             else
             {
                   nextToPlace.SetActive(false);
             }
         }
     }
 
     void LateUpdate()
     {
         if(newobjectselected == true)
         {
             Destroy(nextToPlace);
             nextToPlace = Instantiate(block, Vector3.zero, Quaternion.identity) as GameObject;
             nextToPlace.SetActive(false);
             newobjectselected = false;
         }
     }
 }
 
 
              
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by sparkzbarca · Jan 03, 2014 at 03:45 AM
object.transform.rotate(0,90,0);
Thanks Dude! If anyone else needs this remember to make it a capital R in Rotate. :)
Your answer
 
             Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
C# create and destroy objects 0 Answers
rotate axis of movement 0 Answers
Thrown objects always have same rotation 2 Answers