What this part of code does ?

The script

using System;
using UnityEngine;
using Random = UnityEngine.Random;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public class CloneObjects : MonoBehaviour
{
    public GameObject ObjectToCreate;
    public int objectsHeight = 3;
    [HideInInspector]
    public GameObject[] objects;

    // for tracking properties change
    private Vector3 _extents;
    private int _objectCount;
    private float _objectSize;

    /// <summary>
    ///     How far to place spheres randomly.
    /// </summary>
    public Vector3 Extents;

    /// <summary>
    ///     How many spheres wanted.
    /// </summary>
    public int ObjectCount;
    public float ObjectSize;

    // Use this for initialization
    void Awake()
    {
        Clone();
        objects = GameObject.FindGameObjectsWithTag("ClonedObject");
    }

    private void OnValidate()
    {
        // prevent wrong values to be entered
        Extents = new Vector3(Mathf.Max(0.0f, Extents.x), Mathf.Max(0.0f, Extents.y), Mathf.Max(0.0f, Extents.z));
        ObjectCount = Mathf.Max(0, ObjectCount);
        ObjectSize = Mathf.Max(0.0f, ObjectSize);
    }

    private void Reset()
    {
        Extents = new Vector3(250.0f, 20.0f, 250.0f);
        ObjectCount = 100;
        ObjectSize = 20.0f;
    }

    // Update is called once per frame
    void Update()
    {

    }

    private void Clone()
    {
        if (Extents == _extents && ObjectCount == _objectCount && Mathf.Approximately(ObjectSize, _objectSize))
            return;

        // cleanup
        var ObjectsToDestroy = GameObject.FindGameObjectsWithTag("ClonedObject");
        foreach (var t in ObjectsToDestroy)
        {
            if (Application.isEditor)
            {
                DestroyImmediate(t);
            }
            else
            {
                Destroy(t);
            }
        }

        var withTag = GameObject.FindWithTag("Terrain");
        if (withTag == null)
            throw new InvalidOperationException("Terrain not found");

        for (var i = 0; i < ObjectCount; i++)
        {
            var o = Instantiate(ObjectToCreate);
            o.tag = "ClonedObject";
            o.transform.SetParent(base.gameObject.transform);
            o.transform.localScale = new Vector3(ObjectSize, ObjectSize, ObjectSize);

            // get random position
            var x = Random.Range(-Extents.x, Extents.x);
            var y = Extents.y; // sphere altitude relative to terrain below
            var z = Random.Range(-Extents.z, Extents.z);

            // now send a ray down terrain to adjust Y according terrain below
            var height = 10000.0f; // should be higher than highest terrain altitude
            var origin = new Vector3(x, height, z);
            var ray = new Ray(origin, Vector3.down);
            RaycastHit hit;
            var maxDistance = 20000.0f;
            var nameToLayer = LayerMask.NameToLayer("Terrain");
            var layerMask = 1 << nameToLayer;
            if (Physics.Raycast(ray, out hit, maxDistance, layerMask))
            {
                var distance = hit.distance;
                y = height - distance + y; // adjust
            }
            else
            {
                Debug.LogWarning("Terrain not hit, using default height !");
            }

            // place !
            o.transform.position = new Vector3(x, y + objectsHeight, z);
        }
        _extents = Extents;
        _objectCount = ObjectCount;
        _objectSize = ObjectSize;
    }
}

The part of the script i don’t understand:

var height = 10000.0f; // should be higher than highest terrain altitude
            var origin = new Vector3(x, height, z);
            var ray = new Ray(origin, Vector3.down);
            RaycastHit hit;
            var maxDistance = 20000.0f;
            var nameToLayer = LayerMask.NameToLayer("Terrain");
            var layerMask = 1 << nameToLayer;
            if (Physics.Raycast(ray, out hit, maxDistance, layerMask))
            {
                var distance = hit.distance;
                y = height - distance + y; // adjust
            }
            else
            {
                Debug.LogWarning("Terrain not hit, using default height !");
            }

When i’m running the game using the script it does clone the objects fine.
But i keep getting a warning:

“Terrain not hit, using default height !”

I don’t understand why i’m getting the warning and what does it mean and how to fix it ? I also don’t understand what does the variables height and maxDistance does ?

It tries to cast a ray from a position of (x, 10000, z) (since height = 10000) straight down for 20,000 units (maxDistance) and if it hits something on the “Terrain” layer, it sets y to that height. If it doesn’t hit a terrain, it prints the message you’re seeing.

However, I see at least one mistake in the code:

var nameToLayer = LayerMask.NameToLayer("Terrain");

should be:

var nameToLayer = LayerMask.GetMask("Terrain");