- Home /
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!
Answer by Lovrenc · Jan 14, 2013 at 06:05 PM
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;
}
}
Thanks, this worked perfectly. Gugu's answer would have worked, as I said I wouldn't $$anonymous$$d if it floated. But after a bit of editing to get this script functional with my script it looks amazing. Thanks again.
Answer by gugu6897 · Jan 14, 2013 at 07:05 PM
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.
If I could accept two answers I would, because this also worked for what I asked for. Sorry about that.