- Home /
Instantiating Objects on the Sides of Other Objects
I want to spawn cubes or other prefab models on the sides of a main objects. For instance you would have engines, shields or communication objects. Also being able to rotate objects in the desired orientation. What I know I need to do is to have a script that knows when the mouse is over an object. Then I need it to snap the object to the side of the main object. Then I think this part is much simpler is rotating the object if the right keys are pressed.
Answer by Zamaroth · Oct 28, 2013 at 01:39 PM
When instantiating a prefab, you can set the position right away in the second parameter:
Instantiate(prefab, position, Quaternion.identity);
Second parameter is used to set the position, for examle transform.position.
Third parameter Quaternion.identity is used to set the rotation. However, if you don´t know how Quaternions work, you can store the instatiated object into the variable:
//JavaScript
var clone : GameObject = Instantiate(prefab, position, Quaternion.identity);
clone.transform.eulerAngles = Vector3(x, y, z);
//C#
GameObject clone = (GameObject) Instantiate(prefab, position, Quaternion.identity);
clone.transform.eulerAngles = new Vector3(x, y, z);
You can manipulate with objects created with GameObject.CreatePrimitive the same way:
//JavaScript
var clone : GameObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
clone.transform.position = Vector3(x, y, z);
clone.transform.eulerAngles = Vector3(x, y, z);
//C#
GameObject clone = GameObject.CreatePrimitive(PrimitiveType.Cube);
clone.transform.position = new Vector3(x, y, z);
clone.transform.eulerAngles = new Vector3(x, y, z);
Except I want to have a main object already set up so that you have to build of of that, by literately clicking on the sides of the object.
... so you want to have a cube for example and it will spawn another cube or an prefab on the side you click on. Is that right?
Correct. I was hoping that maybe a snap system could be implemented where an empty gameobject is created and it would snap to other empty gameobjects. This way it could be set up where it would not rely on a "$$anonymous$$inecraft style" snap system.