RTS camera help

This is a part of a camera script that zooms in - out with te mouse wheel.

The problem is taht if I have a terrain with hills, the camera moves through them.
How can I make the camera moves a little bit upper when it touches the floor?

I think some raycast should be used, right?

    if (Input.GetAxis("Mouse ScrollWheel") < 0) // back
	{
		Camera.main.transform.position += Vector3(0, zoomingSpeed, 0);
	}
	if (Input.GetAxis("Mouse ScrollWheel") > 0) // forward
	{
		Camera.main.transform.position += Vector3(0, -zoomingSpeed, 0);
	}
	if (transform.position.y < minDistance)
	{
		transform.position.y = minDistance;
	}
	if (transform.position.y > maxDistance)
	{
		transform.position.y = maxDistance;
	}

You’re correct, a raycast would be a good way of achieving this.

What I’d try is, once you’ve moved your camera, perform a raycast from the cameras position directly downwards. If it detects a collision with your terrain collider (or any other collider if you want it to react to everything) then you calculate the distance to that intersection. (using RaycastHit.point) with this you can then fix the cameras “height” to a minimum if it goes bellow a certain threshold (set your camera position to equal the RaycastHit.point + (Vector3.up * minHeight))

hope that helps give you a bit more information.