Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How can I get cubes to spawn on the edge of another cube? (I'll go in depth in description)

Discussion in 'Scripting' started by Diericx, Jan 23, 2016.

  1. Diericx

    Diericx

    Joined:
    Jun 8, 2013
    Posts:
    62
    So I am trying to create a little voxel editor. The player will be able to place a base cube, and then place other cubes on the base cube's edges. Here is a screenshot of what I have so far: http://i.imgur.com/7Hq2FiY.png?1

    The way I'm doing this is by using a raycast, finding the point where it hits the base cube, and then placing the new cube there. In the screenshot you can see that there is a "target cube" that shows where the next cube will be placed.

    The issue I ran into is that because the point where the raycast hits the base cube is right on the surface of the cube, the child cubes are placed right there and consequently end up half inside the base cube. I don't want the cubes to be placed in such a way where they are overlapping with the base cube, I want the new cubes to be completely outside the base cube so they are sitting on the edge. I want it like this so that I can attach all the rigid bodies without any issues. I just can't really think of a way to do this.

    The only way I can think of solving this would be to check which side the target cube (or target position) is on, relative to the base cube, and move the new cubes accordingly. So if the target cube was on the left side of the base cube is would move the target cube left by 0.5 in order to account for it being half inside the base cube.

    Does anyone have any other ideas? Any help is appreciated:)
     
  2. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    671
    I did something like this; with snap points etc...
    However, I ran into this problem. My fix was making the target block always in front of me, that way, I can get any snap point I want.

    https://gyazo.com/ebcc6a756f0420c1e38eb0ff1ac8822b

    I tried to do what you are doing but it didn't seem to work.
     
  3. Diericx

    Diericx

    Joined:
    Jun 8, 2013
    Posts:
    62
    What do you mean by snap points?
     
  4. Diericx

    Diericx

    Joined:
    Jun 8, 2013
    Posts:
    62
    I finally fixed it!! If anyone is interested, here is the code

    Code (csharp):
    1.  
    2. if (objectHit.point.z <= objectHit.transform.position.z - (targCbScl.z/2))
    3.    //back
    4.    targetCube.transform.position += new Vector3(0, 0, -(targetCube.transform.localScale.x / 2));
    5. else if (objectHit.point.z >= objectHit.transform.position.z + (targCbScl.z / 2))
    6.    //front
    7.    targetCube.transform.position += new Vector3(0, 0, (targetCube.transform.localScale.x / 2));
    8. else if (objectHit.point.x <= objectHit.transform.position.x - (targCbScl.x / 2) )
    9.    //left
    10.    targetCube.transform.position += new Vector3(-(targetCube.transform.localScale.x / 2), 0, 0);
    11. else if (objectHit.point.x >= objectHit.transform.position.x + (targCbScl.x / 2))
    12.    //right
    13.     targetCube.transform.position += new Vector3((targetCube.transform.localScale.x / 2), 0, 0);
    14. else if (objectHit.point.y <= objectHit.transform.position.y - (targCbScl.y / 2))
    15.    //top
    16.    targetCube.transform.position += new Vector3(0, -(targetCube.transform.localScale.x / 2), 0);
    17. else if (objectHit.point.y >= objectHit.transform.position.y + (targCbScl.y / 2))
    18.    //bottom
    19.    targetCube.transform.position += new Vector3(0, (targetCube.transform.localScale.x / 2), 0);
    20.