- Home /
How to change the origin of a gameobject?
I am working on some random generation, and before you laugh at my script, it's only a test for a very small part of the actual algorithm thingy. Anyway, I this script instantiates the hallway at the position of the object the script is attached to. But the trouble is, all the hallway objects have origins in the middle of them, so it spawns with the center at the gameobject. I want it to spawn at the edge of the game object, so I want the origin of the hallway to be at the edge of it. I can't just change the position it spawns at in the script, because each hallway is a different length and I am going to add a lot more. Hopefully you understand what I'm asking, basically I just want to change the point the gameobject spawns. If you need to see my script to understand what I'm asking about, here it is, but you might not need to look at it:
using UnityEngine;
using System.Collections;
public class HallSpawner : MonoBehaviour {
public GameObject redHall;
public GameObject orangeHall;
public GameObject yellowHall;
public GameObject greenHall;
public GameObject blueHall;
private Vector3 spawnPos;
private int hallChooser;
private GameObject chosenHall;
void Start(){
spawnPos = (transform.position);
hallChooser = Random.Range(1,6);
if (hallChooser == 1){
chosenHall = redHall;
}
else if (hallChooser == 2){
chosenHall = orangeHall;
}
else if (hallChooser == 3){
chosenHall = yellowHall;
}
else if (hallChooser == 4){
chosenHall = greenHall;
}
else if (hallChooser == 5){
chosenHall = blueHall;
}
/*else if (dungeonChooser == 6){
chosenDungeon = purple;
}*/
else {
Debug.Log ("You suck at coding!");
}
Instantiate(chosenHall,spawnPos, transform.rotation);
}
}
I found a ghetto way to fix it for my problem only, but I'm gonna leave this question up because I'm sure I'm not the only person who has had this question, and it would be great to have a universal fix for these problems rather than a screwy, acceptable fix for each individual problem.
Answer by Fabkins · Feb 03, 2014 at 12:11 AM
The easiest way is to create an empty object as a parent object. Then you put the corridor object as a child of the empty object. You can then alter the position of the corridor object so its positioned at the edge.
PS you might want to use a switch/case statement instead of a bunch of nested ifs. Or even better, put all in array if nothing special happens with each hall.
I really, really wish you had seen this question a long time ago and shared that answer, because that is freaking brilliant. I feel so, so, so stupid. But you rock, ima mark that as accepted. And yeah, I have since changed it to the array method. The script is much cleaner now, thank you for the advice.
Your answer
Follow this Question
Related Questions
Make sure objects are not instantiated on each other 2 Answers
Randomly Generated Levels 3 Answers
How do I split a rectangle into random smaller rectangles? 1 Answer
How to create a perlin noise heightmap with the values being the highest around a point? 1 Answer
How to randomly generate non-intersecting cylinders within a circle? 2 Answers