Search Unity

Runtime Object Placement (snap object to terrain)

Discussion in 'Scripting' started by BlueSin, Feb 14, 2014.

  1. BlueSin

    BlueSin

    Joined:
    Apr 26, 2013
    Posts:
    137
    So I am in the process of writing a script for a game where the FPS player will be able to place objects in the game (i.e. buildings, campfires, etc.). When the player chooses the campfire for example in their inventory, I want a green or red transparent copy of that model to be displayed in front of the character. Green if the placement is valid, red if it is invalid. In addition, as the player chooses the position of the object, I would like this object to snap to the surrounding terrain. Similar to object placement in the game Rust.

    I have everything working, except getting the object I am currently working with to snap to terrain. I am afraid I am a bit of a mathematical terribad, and finding it difficult to even pinpoint what values need to be adjusted to make the object snap. I am currently raycasting from the objects origin.

    The main problem obviously, is adjusting the position of the object to be just above where the hit took place (i.e. snapping it). The second problem is that the ray hit isn't being detected until the object is halfway into the ground (probably because I'm raycasting from origin).

    I could really use help on this guys, been at this all day and I would like to just move on and be done with this script lol.
     
  2. BrUnO-XaVIeR

    BrUnO-XaVIeR

    Joined:
    Dec 6, 2010
    Posts:
    1,687
    The object pivot should be on its base to make things easier or parent it to an empty transform to emulate a pivot at the object's basement.
    Than you ray cast the ground and object's pivot's Y position = hit-point's Y of the raycast against the floor.
     
  3. BlueSin

    BlueSin

    Joined:
    Apr 26, 2013
    Posts:
    137
    Okay, one of the problems I had before was that when I rotate the camera, the game object would not rotate with the view. Thus is why I parented the object to the camera, so that it was always at center view. So taking your advice I have unparented it, and now of course I have that old problem. What are the coordinates of the camera I need to set so that it is always staying in view?
     
  4. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    You can change the parent from the camera to the ground when you need to.
    Code (csharp):
    1.  
    2. childObject.transform.parent = parentObject.transform;
    3.  
     
  5. BlueSin

    BlueSin

    Joined:
    Apr 26, 2013
    Posts:
    137
    Unfortunately Garth if I do that then I put myself back in the same scenario as my original post. =( Issue still not resolved.
     
  6. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    If you put the anchor point at the bottom of your block, then can you just position the block where the Raycast hit is?
     
  7. fenderflip

    fenderflip

    Joined:
    Feb 12, 2014
    Posts:
    1
    Hey BlueSin, would you be willing to share some of your code on how to do this? It would help a great deal on my project:)
     
  8. subho0406

    subho0406

    Joined:
    Mar 20, 2014
    Posts:
    104