I have a main object that has a North, West, South and East object as children, ie the name of each object is N,W,S,E. I want the main object to be instantiated and put right in front of me starting with the respective end(when I am facing and going north and I reach the North-object, the main object should be instantiated so I enter South of the just instanced main object etc.). Also, it should remove all instantiated main objects being distant more than 4 instances of the main object of the player.
My code is this and the problems are that rooms are copied randomly. Going back creates new instances in the same place or even different places, sometimes I get null exceptions for Destroy.
What to change:
void OnTriggerEnter(Collider other)
{
//N:Z=1
//O:X=1
//W:X=-1
//S:Z=-1
if (other.gameObject.name == "N" && transform.forward.z > 0.8f)
{
Instantiate(Raum.gameObject, new Vector3(other.gameObject.transform.position.x, other.gameObject.transform.position.y, other.gameObject.transform.position.z + 8), Quaternion.identity);
}
if (other.gameObject.name == "S" && transform.forward.z < -0.8f)
{
Instantiate(Raum.gameObject, new Vector3(other.gameObject.transform.position.x, other.gameObject.transform.position.y, other.gameObject.transform.position.z - 6), Quaternion.identity);
}
if (other.gameObject.name == "W" && transform.forward.x < -0.8f)
{
Instantiate(Raum.gameObject, new Vector3(other.gameObject.transform.position.x - 6, other.gameObject.transform.position.y, other.gameObject.transform.position.z), Quaternion.identity);
}
if (other.gameObject.name == "E" && transform.forward.x > 0.8f)
{
Instantiate(Raum.gameObject, new Vector3(other.gameObject.transform.position.x + 6, other.gameObject.transform.position.y, other.gameObject.transform.position.z), Quaternion.identity);
}
Raeume = GameObject.FindGameObjectsWithTag("Raum").ToList();
if (Raeume.Count > 3 && Raeume.First() != null)
Destroy(Raeume.First());
//Debug.Log(other.gameObject.name);
}