I have a game with a level map and a script that generates objects randomly in a circle above it. Is there a way I could have the object actually use the surface objects y coordinate rather than floating down on to it from above? The surface object is not a flat surface you see.
I am guessing that I need to find the mesh vertices from the object I am wanting to stick my new object to, but am unsure how to go about it.
For anyone that wants it or might find it useful this is the script that I attached to the prefab as it is instantiated, which then aligns it to the level map:
var hit : RaycastHit;
var down = Vector3(0,-1, 0);
if (Physics.Raycast (transform.position, down, hit)) {
var distanceToGround = hit.distance;
var currentPos = transform.position;
var newY = currentPos.y-distanceToGround;
transform.position = Vector3(currentPos.x, newY, currentPos.z);
}
This basically grabs the objects initial starting position (out of sight in my case at +1000 y and then instantly alters it to the underlying objects height by subtracting this distance from its initial position.
Thanks to Jesse for the pointer in using Raycasting to find the Vector3 of the object to stick to.