I seem to be having an issue with a placement script I haven written. I am attempting to add a prefab at user-defined coordinates using a RayCastHit. While my code does add a prefab near where I tell it, but never where I actually tell it to.
I have attempted to use both hit.point
and hit.point + hit.normal
to get my prefabs to be placed where I want. Neither work.
Here is my current actual code for placing:
Vector3 positionRaw = hit.point + hit.normal;
Vector3 intPosition = new Vector3(Convert.ToInt32(positionRaw.x), Convert.ToInt32(positionRaw.y), Convert.ToInt32(positionRaw.z)); //Convert to Int Coords to Align with Grid
worldReference.AddBlock(intPosition);
worldReference.AddBlock handles the creation of the prefab.
Here is what I have observed: If I click the Cube, that is my test prefab, in the lower right, the new Prefab block appears in front, relatively, as I want. If I click the lower left, the new prefab appears in front of the prefab, but 1 extra unit to the left. Click the Upper Right and the prefab appears in front but 1 extra unit high. And clicking on the Upper Left Places it in front, but 1 extra high and 1 extra to the left.
What I am attempting to do is allow Object placement not just on Terrain, but on other objects.
(Because it was the first question a friend asked, I can not use hit.transform.position
because There are several objects that are a bit large so the Transform is not always where the user clicked.)