- Home /
Instantiate question
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[i].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[i].point.x,
hit[i].point.y + (Building[SelectedBuilding].transform.localScale.y / 2),
hit[i].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[i].point.x,
hit[i].point.y + (Building[SelectedBuilding].transform.localScale.y / 2),
hit[i].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[i].point.x,
hit[i].point.y + Building[SelectedBuilding].GetComponent<Collider>().transform.localScale.y / 2,
hit[i].point.z);
isFlat = GroundFlat(ghost);
if (collided.Count != 0 || !isFlat)
{
ghost.GetComponent<Renderer>().material.CopyPropertiesFromMaterial(Building[SelectedBuilding].GetComponent<Renderer>().sharedMaterial);
ghost.GetComponent<Renderer>().material.color = new Color(
1f,
0f,
0f,
0.6f);
}
else if (collided.Count == 0 && isFlat)
{
ghost.GetComponent<Renderer>().material.CopyPropertiesFromMaterial(Building[SelectedBuilding].GetComponent<Renderer>().sharedMaterial);
//ghost.GetComponent<Renderer>().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<Collider>().transform.position.x - ghost.transform.localScale.x / 2,
ghost.GetComponent<Collider>().transform.position.y + (ghost.transform.localScale.y / 2),
ghost.GetComponent<Collider>().transform.position.z - ghost.transform.localScale.z / 2),
Vector3.down, Mathf.Infinity);
buildingSlopeHitUR = Physics.RaycastAll(new Vector3(
ghost.GetComponent<Collider>().transform.position.x + ghost.transform.localScale.x / 2,
ghost.GetComponent<Collider>().transform.position.y + (ghost.transform.localScale.y / 2),
ghost.GetComponent<Collider>().transform.position.z - ghost.transform.localScale.z / 2),
Vector3.down, Mathf.Infinity);
buildingSlopeHitDL = Physics.RaycastAll(new Vector3(
ghost.GetComponent<Collider>().transform.position.x - ghost.transform.localScale.x / 2,
ghost.GetComponent<Collider>().transform.position.y + (ghost.transform.localScale.y / 2),
ghost.GetComponent<Collider>().transform.position.z + ghost.transform.localScale.z / 2),
Vector3.down, Mathf.Infinity);
buildingSlopeHitDR = Physics.RaycastAll(new Vector3(
ghost.GetComponent<Collider>().transform.position.x + ghost.transform.localScale.x / 2,
ghost.GetComponent<Collider>().transform.position.y + (ghost.transform.localScale.y / 2),
ghost.GetComponent<Collider>().transform.position.z + ghost.transform.localScale.z / 2),
Vector3.down, Mathf.Infinity);
buildingSlopeHitM = Physics.RaycastAll(new Vector3(
ghost.GetComponent<Collider>().transform.position.x,
ghost.GetComponent<Collider>().transform.position.y + (ghost.transform.localScale.y / 2),
ghost.GetComponent<Collider>().transform.position.z),
Vector3.down, Mathf.Infinity);
for (int i = 0; i < buildingSlopeHitM.Length; i++)
{
if ((buildingSlopeHitUL[i].collider.tag == TerrainCollisionTag) &&
(buildingSlopeHitUR[i].collider.tag == TerrainCollisionTag) &&
(buildingSlopeHitDL[i].collider.tag == TerrainCollisionTag) &&
(buildingSlopeHitDR[i].collider.tag == TerrainCollisionTag) &&
(buildingSlopeHitM[i].collider.tag == TerrainCollisionTag))
{
if ((buildingSlopeHitUL[i].distance <= maxSlopeHigh) &&
(buildingSlopeHitUR[i].distance <= maxSlopeHigh) &&
(buildingSlopeHitDL[i].distance <= maxSlopeHigh) &&
(buildingSlopeHitDR[i].distance <= maxSlopeHigh) &&
(buildingSlopeHitM[i].distance <= maxSlopeHigh))
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
return false;
}
}
Answer by Yggdraseal · Jun 16, 2015 at 01:11 PM
The problem is in this line :
bl.buildingGameObject = (GameObject)Instantiate(Building[SelectedBuilding],
new Vector3(hit[i].point.x,
hit[i].point.y + (Building[SelectedBuilding].transform.localScale.y / 2),
hit[i].point.z),
Quaternion.identity);
Quaternion.identity means the objets is instantiated with it's "original" orientation.
Try setting it to your ghost building's rotation.
Quaternion myNewRot = ghost.transform.rotation;
and then intantiating :
bl.buildingGameObject = (GameObject)Instantiate(Building[SelectedBuilding],
new Vector3(hit[i].point.x,
hit[i].point.y + (Building[SelectedBuilding].transform.localScale.y / 2),
hit[i].point.z),
myNewRot);
Assets/Placement $$anonymous$$it/Placement $$anonymous$$it Files/Scripts/Build$$anonymous$$anager.cs(73,69): error CS1061: Type UnityEngine.GameObject' does not contain a definition for
rotation' and no extension method rotation' of type
UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)
i get this error when i do that?
found out you just have to put transform before rotation