As the title states, how would I make it so that if you click on one side of a cube, another cube spawns flush to that cube, similar to the CAD mockup I have in the attached image. I am relatively new to Unity and do not know how to go about this issue.
I know what I need to do, just not how to implement it via Unity. I know that I need to
detect a click and know what side is clicked
spawn a new cube with click detectors on all sides except those which already have cubes
delete the click detectors that overlap placed cubes
I just don’t know what unity classes should be used to add this.
You would need a raycast from camera to mouse position, that gives variables of hitposition and the object clicked. Assuming the (hit)cube prefab has it’s position as the center, you would subtract hitPosition minus (hit)cube position then round that(direction), and Instantiate a new cube prefab at that position times 2 from (hit)cube.
Excuse my poor pseudo code, I’m just thinking aloud… I’ll play around with my thought and see if I can get it to work right.
using UnityEngine;
public class AddCube : MonoBehaviour
{
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray=Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray,out RaycastHit hit,500f))
{
GameObject obj=Instantiate(hit.transform.gameObject,hit.transform.position+hit.normal,hit.transform.rotation);
}
}
}
}
Exactly what I was looking for, thanks! I will be using this for something larger than 1x1 cubes but I think upscaling this will be a fun learning challenge for me.
I was originally using that cad file to model a collision I was in to show how my idiocy caused an accident, then decided to repurpose it to showcase this.
Modified the code so it works with any rectangular/hexagonal/octagonal
using UnityEngine;
public class AddCube : MonoBehaviour
{
void Update()
{
if (Input.GetMouseButtonDown(0))//if user clicks
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);//where user clicks
bool cast = Physics.Raycast(ray, out RaycastHit hit, 500f);
if (cast && hit.transform.gameObject.CompareTag("Room"))//if user clicks an object, duplicate it along that face
{
if (hit.normal.x != 0)
{
Instantiate(hit.transform.gameObject, hit.transform.position + hit.normal * hit.transform.localScale.x, hit.transform.rotation);
}else if (hit.normal.y != 0)
{
Instantiate(hit.transform.gameObject, hit.transform.position + hit.normal * hit.transform.localScale.y, hit.transform.rotation);
}
else
{
Instantiate(hit.transform.gameObject, hit.transform.position + hit.normal * hit.transform.localScale.z, hit.transform.rotation);
}
}
}
}
}
lol, I knew that question was going to pop up, that’s why a few posts ago I threw it out there:
public class AddCube : MonoBehaviour
{
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray=Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray,out RaycastHit hit,500f))
{
Vector3 sideVector = new Vector3(
hit.transform.localScale.x * hit.normal.x,
hit.transform.localScale.y * hit.normal.y,
hit.transform.localScale.z * hit.normal.z);
Instantiate(hit.transform.gameObject,
hit.transform.position + sideVector, hit.transform.rotation);
}
}
}
}
Because “hitting” the cube will return it’s size, and position. So you only need to change the position, in relevance, as the “Instantiate” will duplicate the cube being hit. Tested this function with a cube scale (2,2,2) and (1,2,3) and saw no issues.
However if you want certain functionality with different size/side hits, than for sure you’ll need “if-else if-else” statements.