Instantiate facing away from Parent.

Hey All,

I need to be able to instantiate gameobjects so they are facing away from their parent.

My Room Object instantiates the Corridor Objects which then instantiates tiles in a straight line.

What I need help with is rotating the Corridor Objects away from the Room Object.

Any ideas how to do this?

Hey guys, im dumping the code here as I’m having difficulty apply it to my script. See where the PlaceCorridor() function is. That should place the Corridor prefab facing away from the object this script is attached too.

Adding Code:
using UnityEngine;
using System.Collections;

public class Room_Generator : MonoBehaviour {
	public GameObject Floor;
	public GameObject Corridor;
	
	void Start () {
		CreateRoom();
	}
	void CreateRoom(){
		int iRoomWidth = Random.Range(1,5);
		int iRoomLength = Random.Range(1,5);
		for(int iZ = 0; iZ <= iRoomLength; iZ++){
			for(int iX = 0; iX <= iRoomWidth; iX++){
				PlaceTile("Floor ",iX,iZ);
			}
		}
		for(int NumberOfDoors = 0; NumberOfDoors < (Random.Range(1,4)); NumberOfDoors++){
			SetCorridorLoc(iRoomWidth,iRoomLength);
		}
	}
	void SetCorridorLoc (int iRoomWidth, int iRoomLength){
		int iDoorSide = Random.Range(1,4);
		int iXcoordinate = Random.Range(0,iRoomWidth);
		int iZcoordinate = Random.Range(iRoomLength,0);
		if(iDoorSide == 1 ){
			if(CollisionChecker(iXcoordinate,-1) == false){
				PlaceCorridor("Corridor_Start ", iXcoordinate, -1);
			}
		}
		if(iDoorSide == 2 ){
			if(CollisionChecker(iXcoordinate,(iRoomLength + 1)) == false){
				PlaceCorridor("Corridor_Start ",iXcoordinate, (iRoomLength + 1));
			}
		}
		if(iDoorSide == 3 ){
			if(CollisionChecker(-1,iZcoordinate) == false){
				PlaceCorridor("Corridor_Start ",-1, iZcoordinate);
			}
		}
		if(iDoorSide == 4 ){
			if(CollisionChecker((iRoomWidth + 1), iZcoordinate) == false){
				PlaceCorridor("Corridor_Start ",(iRoomWidth + 1), iZcoordinate);
			}
		}
	}
	void PlaceTile (string strName,int iX,int iZ){
		GameObject gFloor = Instantiate(Floor) as GameObject;
		gFloor.transform.parent = transform;
		gFloor.transform.localPosition = new Vector3(iX,0,iZ) * 4;
		gFloor.name = strName + iX + "," + iZ;	
	}
	void PlaceCorridor(string strName,int iX,int iZ){
		GameObject gCorridor = Instantiate(Corridor) as GameObject;
		gCorridor.transform.parent = transform;
		gCorridor.transform.localPosition = new Vector3(iX,0,iZ) * 4;
		gCorridor.name = strName + iX + "," + iZ;
	}
	bool CollisionChecker(int iXcoordinate, int iZcoordinate){
		int iNumberOfCollisions = 0;
		for(int z = -1;z <= 1;z++){
			for(int x = -1;x <= 1;x++){
				GameObject Collision = GameObject.Find("Corridor_Start " + (iXcoordinate + x) + "," + (iZcoordinate + z));
				if(Collision != null){
					iNumberOfCollisions++;
				}
			}
		}
		if(iNumberOfCollisions == 0){
			return false;
		} else {
			return true;
		}
	}
}

Thanks

Acces the child’s rotation value and set it to the negative of the parent.

A rough example (might not work), but it gives you the general idea.

var Parent:Transform;

var Child:Transform;

function Start(){
//this will give an inverted result of the parents rotation and set it to the child (happens on the y rotational axis you can change it to your need).
Child=Parent.rotation.y=-Parent.rotation.y;

Child.rotation.y=-negative.rotation.y;
}

To have an object facing away from another object, you can simply set the transform.forward (or transform.up if you’re in 2d), to be the vector pointing from the other object to your object:

public void FaceAwayFrom(Vector3 awayFrom) {
    transform.forward = transform.position - awayFrom;
}

So in your case you’d want to call the FaceAwayFrom() method with the parent’s position:

FaceAwayFrom(transform.parent.position);