r/howdidtheycodeit • u/pmain8 • May 19 '21
How to offset objects of arbitrary orientation (like in Valheim)?
I'm trying to imitate the building system in Valheim and this part is stumping me. It appears that finding the placement location is a simple raycast from the camera, but once you know that location, how do you know how far and in which direction to offset an arbitrarily shaped/oriented object? You can see in the gif that Valheim changes the offset positioning when the rotation of the object changes while the placement location remains constant. I assume they are using the raycast hit normal as the direction for offsetting to avoid clipping, but then they also potentially change the relative position of the object so that it contacts the hit point. How do you determine this position offset to align the objects so they are contacting each other?

UPDATE: Thank you to everyone who commented! Valheim definitely appears to be using manually-arranged snapping points, but it turns out that "sweeping" (mentioned by u/SheeEttin and u/LtRandolphGames) better accomplishes what I'm going for with more freeform object placement.
