Hi i am very new to unity and am currently concentrating on different camera scripts for an RTS type game. Currently when i press tab my camera zooms in and moves from a height of 200 to 50 and changes rotation. However at the height of 50 when i move the camera sometimes it goes into mountains and i was wondering if there was a way of making it hover at a constant height of 50 above the terrain. I started work on a script using Raycast (as seen below) but i don't no if I'm anywhere near the answer
var hit : RaycastHit;
var mTransform : Transform;
function Start () {
mTransform = transform; /* searching for the object's transform component
every update frame especially multiple times is
slow according to the [docs](3rd point)[1]*/
}
function Update () {
if(Physics.Raycast(mTransform.position, -Vector3.up, hit))
if (hit.transform.tag == "ground")) {
mTransform.position.y =
hit.point.y + expectedHeightFromGroundBasedOnZoom;
// expectedHeightFromGroundBasedOnZoom would be 50 in your example
}
}
This way your camera will be exactly `expectedHeightFromGroundBasedOnZoom` distance units away from the ground on the Y axis.