Instantiate object on face of an other object [RESOLVE]

Hi everybody !

I can’t find any answer for that …
I have a mountain mesh in my scene, and i want trees and rocks appears on his surface.

My conditions are Y axis have to detect the height of the mountain at random X and Z.

Only answers i could find was with a terrain mesh, but my mountain isnt a terrain and will never be a terrain.

I suppose it’s easy, but i block on it, anyone have solution ?

You could start a raycast, at the top of the world, so its higher up than anything else in the scene. The point of the hit will be the placd.

Thanks, any basic code for this ?

I actually try to set up raycast but i don’t really understand how it works :confused:

raycast is like a laserpointer

use Random.Range() to pick a random point in a 2d plane at a height well above the top of your mountain, i.e.

Vector3 myRandomPoint = new Vector3(Random.Range(0f, maxWidth), 1000, Random.Range(0f, maxDepth));

(assuming mountain has dimensions 0-maxwidth, 0-maxdepth, <1000)
then use a raycast “down”

RaycastHit hit;
if(Raycast(myRandomPoint, Vector3.down, out hit))
{
// do stuff with hit.point which is where the laserpointer hit the mountain
}
Physics.Raycast(Vector3 origin, Vector3 direction, RaycastHit hitInfo, float distance, int LayerMask);

This is the basics of a Raycast. The parameters are very self explanatory. The origin is where the Raycast will be shot from, the direction is the way in which the Ray will move, the hitInfo stores information surrounding the colliders the Ray has hit. The next two parameters are optional: distance is the length of which the Ray spans, if you leave this then by default it is of infinite length, the LayerMask is used to selectively ignore colliders which the Ray hits.

Here is an example:

public float height;

void Update()
{
     Ray positionRay = new Ray(transform.position, Vector3.down);
     RaycastHit hit;

     if(Physics.Raycast(positionRay, out hit, height)
     {
          if(hit.collider.tag == "ground")
          {
                DeployShoot();
          }
     }
}

Here we check to see if we have hit a collider, with tag “ground”, with our Ray. If we have then we call a method called DeployShoot(). Also this example can be found in the Unity tutorials, I used it because it is a simple but great example

Ok thanks for answers,
so if I put a layer int in “layermask” value, raycast will collide only with this layer, right ?

@Yearl Yeah. If you wanted to make it so the Ray ignored a particular layer you would do this:

int layerMask = 1 << 8; // This bitshifts the index of the layer 8 to get a bitmask
layerMask = ~layerMask; // The ~ operator inverts the bitmask so the layer is ignored

Ok thanks, but right now have another problem …

Here’s my script :

On the instantiate of ground (can be plain, hill, or mount)

            int tempManyDecor = 1;
            gameObject.GetComponent<InstantiateDecors>().ActiveInstanceDecors(zones[z], tempManyDecor);

Done on each ground with a “for”.

And second one intantiate details (tree or rock)

    public List<GameObject> woodDecors;

    public void ActiveInstanceDecors(GameObject thisZone, int howMany)
    {
        for(int d = 0; d < howMany; d++)
        {
            /*float minWidth = thisZone.transform.position.x-(thisZone.transform.localScale.x/3);
            float maxWidth = thisZone.transform.position.x+(thisZone.transform.localScale.x/3);
            float minDepth = thisZone.transform.position.z-(thisZone.transform.localScale.z/3);
            float maxDepth = thisZone.transform.position.z+(thisZone.transform.localScale.z/3);*/

            Vector3 myRandomPoint = new Vector3(thisZone.transform.position.x,
                                                3000,
                                                thisZone.transform.position.z);


            RaycastHit hit;
            if(Physics.Raycast(myRandomPoint, Vector3.down, out hit))
            {
                if(hit.collider.gameObject.layer == 12)
                {
                    Debug.Log("Yay"+hit.collider.gameObject.name);
                    GameObject tempDecor = (GameObject)Instantiate(woodDecors[0]);
                    tempDecor.transform.parent = hit.collider.gameObject.transform;
                    tempDecor.transform.position = hit.point;
                    tempDecor.transform.localScale = new Vector3(0.05f,0.05f,0.05f);
                }
            }
        }
    }
}

Then instantiate work, but only on my mounts !
Don’t know why …

Image ref (cones on mounts are mesh test for tree or rock) :

http://postimg.org/image/ioh4388kt/

EDIT: all ground are on same layer.

@Yearl I’m a bit confused as to what exactly you want to achieve here as there is nothing wrong with the code you have.

I would compact it down a little, and instead of this:

GameObject tempDecor = (GameObject)Instantiate(woodDecors[0]);
tempDecor.transform.position = hit.point;

I would simply do this:

GameObject tempDecor = (GameObject)Instantiate(woodDecors[0], hit.point, Quaternion.identity);

Yeah thanks, always forgot to use all values of Instantiate().

Sorry I’m not good to explain something :S

I just find why it doesn’t work with plains and hills … I have some other colliders upper than ground boudaries, but mountains pass on it.

I tried your solution to ignore a layer, but don’t work … I want to ignore layer 9 so I did :

RaycastHit hit;
            int layerMask = 1 << 9;
            layerMask = ~layerMask;
            if(Physics.Raycast(myRandomPoint, Vector3.down, out hit, layerMask))
            {
                if(hit.collider.gameObject.layer == 12)
                {
                    Debug.Log("Yay"+hit.collider.gameObject.name);
                    GameObject tempDecor = (GameObject)Instantiate(woodDecors[0], hit.point, Quaternion.identity);
                    tempDecor.transform.parent = hit.collider.gameObject.transform;
                    tempDecor.transform.localScale = new Vector3(0.05f,0.05f,0.05f);
                }
            }

Where i’m wrong ?

Well you did not state the distance in the Physics.Raycast parameter. This would be the code for the Raycast if you wanted an infinite Ray:

if(Physics.Raycast(myRandomPoint, Vector3.down, out hit, Mathf.Infinity, layermask))

OK great !! Thanks, still working now !
Thanks again to spend time on my case, bye !

Glad I could help :slight_smile: Best of luck to you