Spawning above the terrain

For my multiplayer game, I want random spawning. This is what I’m using:

Network.Instantiate (Player, Vector3 (Random.Range (5, 95), transform.position.y+4, Random.Range (5, 95)), transform.rotation);

It doesn’t work, but it isn’t what I want anyway. How could I make it so that it always spawns a bit above the ground (on the y axis)?

Hey! I have a script like this, however not taking the object’s pivot into account as Ashkan_gc

  1. Instantiate a prefab with the following script as a component.

  2. Put the ground in a layer called ground.

  3. Set the mask on the script to the ground layer.

    using UnityEngine;
    using System.Collections;
    
    public class PutObjectOnGround : MonoBehaviour {
    
    public LayerMask mask = -1;
    float radius;
    
    void Start () {
    	// set the vertical offset to the object's collider bounds' extends
    	if (GetComponent<Collider>() != null) {
    		radius = GetComponent<Collider> ().bounds.extents.y;
    	} else {
    		radius = 1f;
    	}
    	
    	// raycast to find the y-position of the masked collider at the transforms x/z
    	RaycastHit hit;
    	// note that the ray starts at 100 units
    	Ray ray = new Ray (transform.position + Vector3.up * 100, Vector3.down);
    	
    	if (Physics.Raycast(ray, out hit, Mathf.Infinity, mask)) {		
    		if (hit.collider != null) {
    			// this is where the gameobject is actually put on the ground
    			transform.position = new Vector3 (transform.position.x, hit.point.y + radius, transform.position.z);
    		}
    	}
    }
    

    }

first do a raycast toward ground and find it’s position and then spawn a litle bit above it. it depends on the pivot of your object too. you should calculate the distance between pivote and bottom of your object and then position it based on that.
procedural object placement is a really fun topic.