How to place a GameObject with mesh on a mesh "perfectly" through c# script?

I have a gameobject that have a rock mesh. However, the bottom of the mesh is hallow and ugly. I want to place it on a mesh that looks like a hill, so that the player cannot see the gameobject’s ugly and hallow bottom.

I have a script that uses raycast to place the rock. The raycast is above the mesh. When I raycast downwards, I get a Vector3 on the hill looking mesh. I place that rock on that point, and I can see its bottom! How to avoid that using C# code?

Here is my full question:

How To:
Rotate and move a GameObject, which have a “mesh” as its component, so that it sits on another mesh “perfectly” without seeing the GameObject’s bottom through c# script in Unity.

Here are some Images:

Click here to view the image of what it looks like now


Click here to view the image of what I want to achieve

Any kinds of help is appreciated!!

Here is the code I use to place it on the mesh, and it works just fine!
Vector3 pointFromAbove = new Vector3(Random…);
if (Physics.Raycast(pointFromAbove, Vector3.down, out hit))
GameObject newObject = (GameObject)Instantiate(prefab, hit.point, Quaternion.identity, parent);



Hi there,

As I see in the pictures, your goal is to make the “up” vector of your rock match the Normal vector of the hill in the point that your ray collides with it.

I can’t assure, that this will work every time. But it will be a pretty decent advancement.

Below your Instantiate call you should write:

newObject.transform.up = hit.normal;

This should rotate your rock, making it “perpendicular” to the surface of the hill.

Hope it helps.