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. Dismiss Notice

Question Block Building project

Discussion in 'Physics' started by EhabAnarchist, Aug 19, 2023.

  1. EhabAnarchist

    EhabAnarchist

    Joined:
    Jul 23, 2023
    Posts:
    5
    Hello, I am a 3Ds Max artist, and I have recently gotten into game development, before that, it was all about media creation. I'm a visual type, so programming languages are not my speciality, I struggle when i can't visually see what I am doing.. C# seems to be easier than others I have come across though. Anyway, the point of this post, I am trying to create a block-building 3D game in Unity, up to this point I have succeed in creating a grid system and I am able to snap blocks to it, great, but sometimes they can still intersect upon placement, even though they have colliders and kinematic rigidbodies attached, also I want to be able to snap them on top of each other, lego style, can anyone please help with this? Much appreciated, I am attaching the two scripts used in this implementation for reference and hopefully improvement, thank you in advance!
     

    Attached Files:

  2. EhabAnarchist

    EhabAnarchist

    Joined:
    Jul 23, 2023
    Posts:
    5
    No one knows how to snap Blocks together? Is it a Unity limitation?
     
  3. DouglasPotesta

    DouglasPotesta

    Joined:
    Nov 6, 2014
    Posts:
    108
    looks like you need to gate when you update the mouse position by whether or not the cell is occupied.
    As it stands right now you can have the following occur if there are cells already occupied.

    start of frame
    - update
    - - ray hits ... update mouse position ... no cell to update though
    - mouse click ui
    - - instantiate obj at current mouse position
    end frame
    start of next frame
    - update
    -- ray hits ... update mouse position ... check if cell is occupied (say it is for this frame) ... skip updating instantiated obj
    - no mouse click ui event
    end frame
    start of frame
    - update
    -- ray hits ... update mouse position ... check if cell is occupied (say it is for this frame) ... skip updating instantiated obj
    - mouse click ui ... instantiate obj (replacing previous reference to the last instantated one)
    end of frame

    notice that it's possible for the spawned object to fail to find a valid cell by the time the next one is spawned.
     
  4. EhabAnarchist

    EhabAnarchist

    Joined:
    Jul 23, 2023
    Posts:
    5
    Thanks man, what should I do then? Like I mentioned in my post I am not a programmer per se, I am a 3D Artist looking to expand into gaming, so C# is still new to me and frankly I'm more the visual type, so the only help I need is in the coding, all the rest I think I can handle myself.
     
  5. DouglasPotesta

    DouglasPotesta

    Joined:
    Nov 6, 2014
    Posts:
    108
    Relying on people from a forum to program for you will only get you so far.
    Hopefully this is the most complex thing you have to do for your game, otherwise you may have to learn a lot.
    First you'll want to add a Vector3 member variable for the most recently found free position and a bool member variable for if the free position is still valid.
    You will remove the MouseUp condition from GetMousePositionOnGrid and where you are assigning the position to the prefab you will instead assign it to the free position member variable and then also set the bool member variable for whether it is free to true.
    You will want to change the isCellOccupied check to return true if OverlapBox returns more than zero colliders.
    In OnMouseClickOnUI you will want to replace the if condition for onMousePrefab with the bool member variable for whether is free. If it is free, then you instantiate the block and using the member variable for free position instead of mouse position. Then you will set your member variable for whether the position is free to false (since it's now occupied).
    This means that on mouse clicik on ui will not spawn anything if a free spot hasn't been found since the last spawn. In that scenario you may want to play a sound effect / animation to let the player know they couldn't place anything.

    Hopefully you are able to figure it out from there. good luck
     
  6. EhabAnarchist

    EhabAnarchist

    Joined:
    Jul 23, 2023
    Posts:
    5

    Thank you, I will look into it, meanwhile, I undertook another project, a racing game, it's going very well so far