Hi, i got this asset from the asset store for building placement. There’s this ghost building before the final building places. And when the final building places i would like it to also instantiate with the ghost buildings z rotation because i have added a script so the ghost can be rotated before the final object is instantiated.
just to help clarify the question ill give an example say this line is my ghost object | When i rotate the ghost like this \ the final object still instantiates like | like it was originaly
Here’s the Code-
public class BuildManager: MonoBehaviour
{
public int SelectedBuilding;
private int LastSelectedBuilding;
public GameObject[] Building;
public List<GameObject> collided = new List<GameObject>();
public List<BuildingList> buildings = new List<BuildingList>();
public string TerrainCollisionTag;
private GameObject ghost;
private bool ghostOn = false;
private bool isFlat;
public float maxSlopeHigh = 5f;
void Start()
{
LastSelectedBuilding = SelectedBuilding;
}
void Update()
{
Ray ray;
RaycastHit[] hit;
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
hit = Physics.RaycastAll(ray, Mathf.Infinity);
if(GUIScript.TimeToPlace==true){
for (int i = 0; i < hit.Length; i++)
{
if (hit*.collider.tag == TerrainCollisionTag)*
-
{*
-
if (SelectedBuilding != LastSelectedBuilding && ghost != null)*
-
{*
-
Destroy(ghost);*
-
ghostOn = false;*
-
LastSelectedBuilding = SelectedBuilding;*
-
collided.Clear();*
-
break;*
-
}*
-
if (!ghostOn)*
-
{*
-
ghost = (GameObject)Instantiate(Building[SelectedBuilding],*
_ new Vector3(hit*.point.x,_
_ hit.point.y + (Building[SelectedBuilding].transform.localScale.y / 2),
hit.point.z),
Quaternion.identity);*_
* ghostOn = true; *
* }*
* if (Input.GetMouseButtonDown(0) && collided.Count == 0 && isFlat )*
* {*
* BuildingList bl = new BuildingList();*
* DestroyImmediate(ghost);*
* bl.buildingGameObject = (GameObject)Instantiate(Building[SelectedBuilding],*
_ new Vector3(hit*.point.x,
hit.point.y + (Building[SelectedBuilding].transform.localScale.y / 2),
hit.point.z),
Quaternion.identity);*_
* string s = bl.buildingGameObject.name.Replace(“(Clone)”, “”);*
* bl.buildingGameObject.name = s;*
* bl.buildingName = s;*
* buildings.Add(bl);*
* ghostOn = false;*
* GUIScript.TimeToPlace=false;*
* collided.Clear();*
* break;*
* }*
* if (ghost != null)*
* {*
* ghost.transform.position = new Vector3(*
_ hit*.point.x,
hit.point.y + Building[SelectedBuilding].GetComponent().transform.localScale.y / 2,
hit.point.z);*_
* isFlat = GroundFlat(ghost);*
* if (collided.Count != 0 || !isFlat)*
* {*
* ghost.GetComponent().material.CopyPropertiesFromMaterial(Building[SelectedBuilding].GetComponent().sharedMaterial);*
* ghost.GetComponent().material.color = new Color(*
* 1f,*
* 0f,*
* 0f,*
* 0.6f);*
* } *
* else if (collided.Count == 0 && isFlat)*
* {*
* ghost.GetComponent().material.CopyPropertiesFromMaterial(Building[SelectedBuilding].GetComponent().sharedMaterial);*
* //ghost.GetComponent().material.color = new Color(*
* // 0f,*
* // 1f,*
* // 0f,*
* // 0.6f);*
* }*
* }*
* } *
* }*
* }*
* }*
* bool GroundFlat(GameObject Ghost)*
* {*
* RaycastHit[] buildingSlopeHitUL;*
* RaycastHit[] buildingSlopeHitUR;*
* RaycastHit[] buildingSlopeHitDL;*
* RaycastHit[] buildingSlopeHitDR;*
* RaycastHit[] buildingSlopeHitM;*
* buildingSlopeHitUL = Physics.RaycastAll(new Vector3(*
* ghost.GetComponent().transform.position.x - ghost.transform.localScale.x / 2,*
* ghost.GetComponent().transform.position.y + (ghost.transform.localScale.y / 2),*
* ghost.GetComponent().transform.position.z - ghost.transform.localScale.z / 2),*
* Vector3.down, Mathf.Infinity);*
* buildingSlopeHitUR = Physics.RaycastAll(new Vector3(*
* ghost.GetComponent().transform.position.x + ghost.transform.localScale.x / 2,*
* ghost.GetComponent().transform.position.y + (ghost.transform.localScale.y / 2),*
* ghost.GetComponent().transform.position.z - ghost.transform.localScale.z / 2),*
* Vector3.down, Mathf.Infinity);*
* buildingSlopeHitDL = Physics.RaycastAll(new Vector3(*
* ghost.GetComponent().transform.position.x - ghost.transform.localScale.x / 2,*
* ghost.GetComponent().transform.position.y + (ghost.transform.localScale.y / 2),*
* ghost.GetComponent().transform.position.z + ghost.transform.localScale.z / 2),*
* Vector3.down, Mathf.Infinity);*
* buildingSlopeHitDR = Physics.RaycastAll(new Vector3(*
* ghost.GetComponent().transform.position.x + ghost.transform.localScale.x / 2,*
* ghost.GetComponent().transform.position.y + (ghost.transform.localScale.y / 2),*
* ghost.GetComponent().transform.position.z + ghost.transform.localScale.z / 2),*
* Vector3.down, Mathf.Infinity);*
* buildingSlopeHitM = Physics.RaycastAll(new Vector3(*
* ghost.GetComponent().transform.position.x,*
* ghost.GetComponent().transform.position.y + (ghost.transform.localScale.y / 2),*
* ghost.GetComponent().transform.position.z),*
* Vector3.down, Mathf.Infinity);*
* for (int i = 0; i < buildingSlopeHitM.Length; i++)*
* {*
_ if ((buildingSlopeHitUL*.collider.tag == TerrainCollisionTag) &&
(buildingSlopeHitUR.collider.tag == TerrainCollisionTag) &&
(buildingSlopeHitDL.collider.tag == TerrainCollisionTag) &&
(buildingSlopeHitDR.collider.tag == TerrainCollisionTag) &&
(buildingSlopeHitM.collider.tag == TerrainCollisionTag))
{
if ((buildingSlopeHitUL.distance <= maxSlopeHigh) &&
(buildingSlopeHitUR.distance <= maxSlopeHigh) &&
(buildingSlopeHitDL.distance <= maxSlopeHigh) &&
(buildingSlopeHitDR.distance <= maxSlopeHigh) &&
(buildingSlopeHitM.distance <= maxSlopeHigh))
{
return true;
}
else*
* {
return false;
}
}
else*
* {
return false;
}*_
* }*
* return false;*
* }*
}