- Home /
Question: how to overlap 2 cubes objects
My problem is that I want to make a static cube object will be overlapped with another cube object. As a result, the cube object will be fit into the static cube object. Note: The static cube object will be bigger than the cube object, and we can see at the end of overlapping, the static cube will be like a frame of the cube object.
What I tried was: Using Sphere Collider on the Cube, so that I only need to use radius of the to sphere to detect the collision.
float distance;
//position of the letter object to vector3
Vector3 letter = GameObject.FindWithTag ("Player").transform.position;
//position of the slot object to vector3
Vector3 slot = GameObject.FindWithTag ("Respawn").transform.position;
void Update()
{
//Use to detect collision and if they are collided they will automatically overlap
distance = Mathf.Sqrt(((letter.x - slot.x) (letter.x - slot.x)) + ((letter.y - slot.y) (letter.y - slot.y)));
if (distance < letter.x - slot.x)
{
letter = new Vector3(slot.x,slot.y,slot.z);
}
}
When i was trying to run the program, the program still worked; however, my code did not work as I wanted to. Any ideas why it did not work? I know there is something wrong with the code. I am new to Unity, so I could not figure it out.
For the detection codes, the way I used is for the circle object, not sphere object, but I think sphere or circle only have 1 radius. Thus, I applied the circle formula in this case.
I'm not totally sure what you are doing and what has gone wrong but my best guess is this. I think you are tying to use one cube as a container for the other, cubes(with box colliders) are solid objects(imagine a set of solid wooden building blocks), and as a result you will get an unwanted result if you try to put one inside the other. What happened when you ran this?
Thanks so much for replying me.
The idea was just like you said that whenever one box touch another box, the one was touched become the container of the other. However, the one was contained has one face of the cube visible.
The game that i am trying to make is i have random letters, and i try to push the cubic letter into cubic slot, so that those letters gave a meaning.
I have done with most of the parts, I only got stuck with how to make the cubic letter fit into the cubic slot whenever they touch each other.
When I tried to run those code above. The program still ran, but gave me some error messages.
ArgumentException: FindGameObjectWithTag can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, ins$$anonymous$$d move initialization code to the Awake or Start function. UnityEngine.GameObject.FindWithTag (System.String tag) SwipeObj..ctor ()
$$anonymous$$ake sure you disable the collider on the one that you put inside, but this is not you issue. You need to post you full code for this script then hopefully someone can help, thanks