- Home /
How to bind primative shapes together programmatically?
In my game I have a large cube that represents the player. It can bump into smaller cubes or spawn smaller cubes that are supposed to 'stick' to it. That is, they move in the X and Y axis and rotate by the same amount as the player cube. Sort of like this, if the cubes were touching and attached.
So for example, it might have a small cube half the size of the big cube, stuck to its left side. As the player cube rotates or moves the small cube should stay there.
I am pretty new to Unity, but I looked at combining the meshes, but I need access to the hit data for each piece separately so that won't work.
Is there a simple way to do this, without keep a list of all things the player has stuck to him and moving them all separately? Even a link to something relevant would be greatly appreciated, since I am new enough that I don't even know what to google. Thanks in advance!
Answer by AlucardJay · Apr 23, 2013 at 09:25 PM
make the new cube a child of the old cube it is supposed to be attached to :
newCube.transform.parent = oldCube.transform;
Here is an example script. Create a new scene, create a cube, attach this script, then hit play. After 3 seconds, another cube is created, and then parented to the main cube :
#pragma strict
private var mainCube : Transform;
function Start()
{
mainCube = transform;
Invoke( "AttachNewCube", 3.0 );
}
function Update()
{
mainCube.Rotate( Vector3.up * 5.0 * Time.deltaTime );
}
function AttachNewCube()
{
// this is just creating a new cube
var cube : GameObject = GameObject.CreatePrimitive( PrimitiveType.Cube );
var newCube : Transform = cube.transform;
newCube.position = mainCube.position + mainCube.forward;
newCube.rotation = mainCube.rotation;
// Here is where the new cube is being parented to the main cube
newCube.parent = mainCube;
}
Wow, thank you very much! That's absolutely perfect!
I knew I was missing out on something simple but fundamental like that!
Your answer
Follow this Question
Related Questions
Spawning won't work 1 Answer
Unity 3D C# - Gameobject Minecraft Like Placing 0 Answers
Looking for CheckCube 1 Answer
Using Gizmos.DrawWireCube to draw position bounds? 1 Answer
how do I add text to a 3D cube? 2 Answers