How can i get the Y if i only know the X and Z

The walking method is almost perfect now, i only have a problem with the hills(it just walkes through when its in the way) and the dephts(sorry, i couldn’t find a better word) where it just fly’s over. How can i make the script generating a path over the terrain instead of going through/flying over?

This is my code:

function Update () {
	
	if(newPos != transform.position) {
		Debug.DrawLine(newPos, transform.position, Color.yellow);
		// walk to new position
		
		distance = Vector3.Distance(newPos, transform.position);
		
		transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(newPos - transform.position), rotateSpeed * Time.deltaTime);
		transform.position += transform.forward * moveSpeed * Time.deltaTime;
		
                // Don't let it walk around the new position
		if(distance < 2.0f) {
			newPos = transform.position;
		}
		
	} else {
		// Don't walk away directly
		if(Random.Range(0,100) == 1)
			newWalkingPos();
		
	}

}

function newWalkingPos () {
	
	var newZ : float;
	var newX : float;
	var newY : float;
	var hit : RaycastHit;
	
	newZ = Random.Range(maxBottom, maxTop);
	newX = Random.Range(maxLeft, maxRight);
	
	if (Physics.Raycast (Vector3(newX, 100, newZ), -Vector3.up, hit)) {
		newY = (100 - hit.distance) + 0.6;
	}
	
	newPos = Vector3(newX, newY, newZ);
	
}

Sorry for bad English, I got a ‘get the * up’-6 for English on highschool

have you to know the object position or the y axis terrain’s measure?

You want to find the elevation of the terrain at a given x+y coordinate? If so, do a raycast down from the sky, and look at the hitinfo. If you’re dealing with actual terrain there might be a helper method in the terrain class for getting height at a point.

The distance variable in RaycastHit should be sufficient I think. There even is an example in the docs:

function Update () {
   var hit : RaycastHit;
   if (Physics.Raycast (transform.position, -Vector3.up, hit)) {
      var distanceToGround = hit.distance;
   }
}

For performance, you might want to look into using yourTerrain.terrainData.GetHeight (x, y) in stead of raycasting.

The walking method is almost perfect now, i only have a problem with the hills(it just walkes through when its in the way) and the dephts(sorry, i couldn’t find a better word) where it just fly’s over. How can i make the script generating a path over the terrain instead of going through/flying over?

This is my code:

function Update () {
	
	if(newPos != transform.position) {
		Debug.DrawLine(newPos, transform.position, Color.yellow);
		// walk to new position
		
		distance = Vector3.Distance(newPos, transform.position);
		
		transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(newPos - transform.position), rotateSpeed * Time.deltaTime);
		transform.position += transform.forward * moveSpeed * Time.deltaTime;
		
                // Don't let it walk around the new position
		if(distance < 2.0f) {
			newPos = transform.position;
		}
		
	} else {
		// Don't walk away directly
		if(Random.Range(0,100) == 1)
			newWalkingPos();
		
	}

}

function newWalkingPos () {
	
	var newZ : float;
	var newX : float;
	var newY : float;
	var hit : RaycastHit;
	
	newZ = Random.Range(maxBottom, maxTop);
	newX = Random.Range(maxLeft, maxRight);
	
	if (Physics.Raycast (Vector3(newX, 100, newZ), -Vector3.up, hit)) {
		newY = (100 - hit.distance) + 0.6;
	}
	
	newPos = Vector3(newX, newY, newZ);
	
}

How can i make this?