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.
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.
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?