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

Trying To Snap Gameobjects Together For A Buildsystem

Discussion in 'Scripting' started by MammothSaturn83, Apr 10, 2019.

  1. MammothSaturn83

    MammothSaturn83

    Joined:
    Feb 10, 2018
    Posts:
    20
    Hi, i am building a building system and i have Ghost floors and normal floors right now i wan't to detect if there is a normal wall in the scene then depending on where the mouse pointer is looking i wan't the ghost floor to snap to the edge of the normal floor. Can some please help?

    -Thanks Nicholas
     
  2. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583


    You should be able to use this tutorial series and adapt it to your game I would have thought, it's the same sort of system.
     
  3. MammothSaturn83

    MammothSaturn83

    Joined:
    Feb 10, 2018
    Posts:
    20
    Thankyou for the suggestion but it was not what i was looking for. Do you have any other suggestions
     
  4. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Are you talking about achieving the actual effect of a ghost effect on what you're placing in the scene? It depends on how you want to do it but for the effect itself you're just instantiating in the floor with a transparent material. You then do a check for a click and then use the code provided in the tutorial to snap the gameobject to position.

    For the ghost gameobject to work you just need to remove the rigidbody and collider, the ghost gameobject follows a raycast every frame in most cases.
     
    Last edited: Apr 10, 2019
  5. MammothSaturn83

    MammothSaturn83

    Joined:
    Feb 10, 2018
    Posts:
    20
    Oh i see what you mean thank you i will try it later.

    -Thanks Nicholas
     
  6. MammothSaturn83

    MammothSaturn83

    Joined:
    Feb 10, 2018
    Posts:
    20
    Also do you know how to make the ghost wall follow the mouse? cause that would help a lot.

    -Thanks Nicholas
     
  7. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    To make the gameobject follow the mouse you'd just use a standard hit.point raycast and have it follow the raycast using update, if you want to have your ghost appear only at certain times you can do things like setup some buttons that instantiate the ghosts at your mouse position when you click them.

    There are plenty of tutorials out their on how to to do raycasts, specifically you want your raycast to look for hit.point and that should do the trick, hope it helps.
     
  8. MammothSaturn83

    MammothSaturn83

    Joined:
    Feb 10, 2018
    Posts:
    20
    Thankyou very much!
     
  9. MammothSaturn83

    MammothSaturn83

    Joined:
    Feb 10, 2018
    Posts:
    20
    Hi i cannot find any tutorials could you please help me?
     
  10. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583


    This is the right sort of code, in order to have it work the way you're wanting you should have the raycast fire off when you press a UI button and rather than put the raycast into a mouse click you should put the code into the Update function so it happens every frame as you are moving the mouse along the ground.

    Oh before I forget, this is actually quite important, in order to prevent the object going halfway into the ground like the video, you should make it so your raycast fires at an empty underneath whatever prefab or gameobject you intend to spawn into the game on the ground this will prevent intersecting problems. So to do this, when you get the raycast code set up you should make the gameobject you're spawning a child of the empty and then that empty will take care of the positioning and placing rather than the gameobject itself.

    The raycast fires directly into the centre of any gameobject which is why this issue happen, I won't write code out for you, that's your job, but you should have everything you need here to work it all out.