- Home /
How to keep instatiated object above a certain height?
hi, i have made a crafting script and in this a house is instantiated where you click but if you point towards the ground it goes through it bit. any help?
extra info. the instantiation is called by a raycast. script
Distance = hit.distance;
if (Distance < MaxDistance)
{
Instantiate(activeObject,hit.point, Quaternion.identity);
Answer by robertbu · Mar 16, 2014 at 07:06 PM
If all of your objects are to be handled at by ground level, the easiest solution is to place the pivot point of the object at the bottom of the object. That is done either in the modeling program, or you can do it by using an empty game object. Place the empty game object at the bottom-middle of your prefab, make the visible object a child of the empty game object.
Another solution is to move the object up when you Instantiate():
Instantiate(activeObject, hit.point + Vector3.up * someValue, Quaternion.identity);
'someValue' is the distance between the bottom of your object and the pivot point.
If your ground is flat, yet another solution is to use Unity's mathematical Plane class and use Plane.Raycast() to position your objects.
hey, thanks for the answer i sort of realised that after i posted and tested but thanks anyway. would you also know how i would make it so it is the same angle as the ground i.e. placing it on a hill?
Assu$$anonymous$$g your prefab is constructed so that the top is in the up direction, you can do this:
var q = Quaternion.FromToRotation(Vector3.up, hit.normal);
Instantiate(activeObject, hit.point + Vector3.up * someValue, q);