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;
}
}
}