# Height Sampling...

Ok, so I have a script that creates a circle around the object its attached to, samples the height points, then determines if the ground is flat enough to place the object permanently. The script works perfect on the the flat ground (say 0 feet) by changing green. Then when the object hits too steep of a grade it turns red.

The problem is that: if i have a raised area that is completely flat (a plateau) the object will become red signifying that the grade is too steep...but its flat.

Heres my script:

function Update() { isLevel(); }

///////////////////////////////////////////////////////////////////////////////////////////

function isLevel () {

``````    var sampleHeights       =   new Array();

``````

//Array used to store the heights of the sampled points on the circle. var theta : float = 0; //Increments the radius of the sweep. var maximumHeight : float; var minimumHeight : float;

``````    objectCenter        =   self.renderer.bounds.center;

//Adds the center height to the array

//Samples points in a cricle around the object
while(theta < 6.28) //6.28 default number
{
theta += Mathf.PI/6;    //Amount of points you wish to sample
var pos     : Vector3 = objectCenter + Vector3(objectRadius*Mathf.Cos(theta),objectRadius*Mathf.Sin(theta)); //Calculates the points being sampled
}

//Used to find the minimum and maximum heights.
maximumHeight   = sampleHeights[0];

for(var i = 0; i < sampleHeights.length; i++)
{
//Goes through all the sample points and finds the min and max values.
minimumHeight       = Mathf.Min(minimumHeight,sampleHeights*);*
_maximumHeight   = Mathf.Max(maximumHeight,sampleHeights*);*_
_*}*_
_*//returns maximum height minus the minimum height and compares it to the grade tollerance*_
_*greenOrRed = false;*_
_*else*_
_*greenOrRed = true;*_
_*footprint();*_
_*}*_
_*```*_``````

I'm going to guess the problem is that you haven't initialized minimumHeight. You need to add this line:

``````minimumHeight = sampleHeights[0];

``````

Also, instead of 6.28, you can just use Math.PI * 2.

Ok, so i just realized that the points i am finding are just y values. As we all know, y=mx+b, I am trying to find the slope (m) which is m = (Y2 -Y1) (X2 -X1). Terrain.SampleHeight is just giving me the y values so its not calculating slope at all, just a maximum height. This is why i cant place on a plateau.

My new question is: How do i get both the x and y coordinates of the sampling points. Terrain.GetPosition does not seem to like Vector3...

The best overload for the method 'UnityEngine.Terrain.GetPosition()' is not compatible with the argument list '(UnityEngine.Vector3)'.

I would also like to avoid making 6 offset raycasts to detect points...

heres the updated code...

``````function isLevel ()
{

var sampleHeights       =   new Array();        //Array used to store the heights of the sampled points on the circle.
var theta               :   float = 0;          //Increments the radius of the sweep.
var maximumHeight       :   float;
var minimumHeight       :   float;

objectCenter        =   self.renderer.bounds.center;

//Adds the center height to the array

//Samples points in a cricle around the object
while(theta < Mathf.PI * 2) //6.28 default number
{
theta += Mathf.PI/6;    //Amount of points you wish to sample
var pos     : Vector3 = objectCenter + Vector3(objectRadius*Mathf.Cos(theta),objectRadius*Mathf.Sin(theta)); //Calculates the points being sampled

}

//Used to find the minimum and maximum heights.
maximumHeight   = sampleHeights[0];
minimumHeight   = sampleHeights[0];

for(var i = 0; i < sampleHeights.length; i++)
{
//Goes through all the sample points and finds the min and max values.
minimumHeight       = Mathf.Min(minimumHeight,sampleHeights*);*
_maximumHeight   = Mathf.Max(maximumHeight,sampleHeights*);*_
_*}*_
_*print (sampleHeights[0] + " " +  sampleHeights[1]+ " " +  sampleHeights[2]);*_
_*//returns maximum height minus the minimum height and compares it to the grade tollerance*_