Set Y position same as another object

In my game you are trapped in a room with a lot of hills and an enemy that acts like a weeping angel (for those that don’t know, it is a little bit like slender man). The enemy script works fine, but when the player goes onto a hill the enemy will go straight through the hill rather than going up it to follow the player. Is there any way to fix this? I would be happy with the enemy just being set to the same Y axis as the player, but if you know of a better way to get the enemy to actually go up the hill, that would be awesome. Here is my script for setting the y axis of the enemy:

transform.position.y = 11;

Thanks in advance for any help!

Add “terrain” tag to your terrain.

var hit : RaycastHit;
if (Physics.Raycast (transform.position, -Vector3.up, hit, 10)) {
  if(hit.tag == "terrain") {
    transform.position.y = hit.point.y;

Do this in enemy script:

transform.position.y = playerscript.transform.position.y;

In place of ‘playerscript’ you put the name of the script attached to the player gameObject that you want to use the Y position.