how to adjust the height of the camera to the terrain

hi guys. I am currently working an rts game.as of now my camera moves when the mouse pointer is scrolling but the problem is that the environment that i am using is a dessert in which the ground is uneven. Can you please tell me how the height of my camera would adjust to my terrain?

The code that i am using is this.

if (mousePosX < scrollDistance) 
		{ 
			transform.Translate(Vector3.right * -scrollSpeed * Time.deltaTime); 
		} 
		
		if (mousePosX >= Screen.width - scrollDistance) 
		{ 
			transform.Translate(Vector3.right * scrollSpeed * Time.deltaTime); 
		}
		
		if (mousePosY < scrollDistance) 
		{ 
			transform.Translate(transform.forward * -scrollSpeed * Time.deltaTime); 
		} 
		
		if (mousePosY >= Screen.height - scrollDistance) 
		{ 
			transform.Translate(transform.forward * scrollSpeed * Time.deltaTime); 
		}

One solutions is to use ‘Terrain.SampleHeight()’:

transform.position.y = Terrain.activeTerrain.SampleHeight(transform.position) + characterAdjust;

‘characterAdjust’ is the distance the pivot point of your character is above his feet.

Alternately Raycasting() can be used.

There may be more efficient ways to do this but I would do it this way

Set a max height for your camera

Cast a ray from your camera to the terrain (Probably -Vector3.up)

Get the position of the hit

Lerp your camera’s transform.position.y value to hit.position.y + maxheight

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour 
{
	//Public Variables
	public GameObject camera;
	//Private Variables
	private float maxHeight = 10;
	// Update is called once per frame
	void Update () 
	{
		RaycastHit hit;
		if(Physics.Raycast(camera.transform.position, Vector3.down, out hit, 100))
		{
			if(hit.name == "Terrain")
			{
				camera.transform.position = new Vector3(camera.transform.position.x,
                Mathf.Lerp(camera.transform.position.y,hit.point.y,Time.deltaTime),
                camera.transform.position.z);
    			}
    		}
    	}
    }