- Home /
Make camera hover at 50 over hilly terrain
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;
function Update () {
if(Physics.Raycast (transform.position, -Vector3.up, hit)) {
if(hit.distance < 50)
transform.Translate(Vector3(0, 1, 0) * Time.deltaTime, Space.World);
}
}
Any help would be much appreciated, Thanks
Answer by matyicsapo · Nov 14, 2010 at 12:11 PM
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.
thanks for the answer but i put the script on my camera and it didn't seem to do anything. i changed expectedHeightFromGroundBasedOnZoom to 50 and tried changing "ground" to "Terrain" but still nothing
put in a few Debug.Log's to see till where it works, like: does the Raycast hit anything at all? what's the tag of what it hits..i think that the problem is with one of the if's
Answer by Vumi · Dec 31, 2012 at 12:44 PM
Use this, there were a few mistakes in that original code. With this script I have also included camera smoothing so that movement is not so jerky.
#pragma strict
var hit : RaycastHit;
var mTransform : Transform;
/*EDIT THESE SETTINGS*/
private var cameraHoverHeight = 50;
private var terrainName = "Terrain";
/*------------------------*/
/*DON'T EDIT THESE SETTINGS*/
private var yVelocity = 0.0;
/*------------------------*/
function Start () {
mTransform = transform;
}
function Update () {
var smoothTime = 0.2/(camera.velocity.magnitude/100);
if(Physics.Raycast(mTransform.position, -Vector3.up, hit))
if (hit.transform.name == terrainName) {
var newPosition : float = Mathf.SmoothDamp(transform.position.y, hit.point.y + cameraHoverHeight,
yVelocity, smoothTime);
transform.position = Vector3(transform.position.x, newPosition, transform.position.z);
}
}
Your answer
Follow this Question
Related Questions
How to detect UI Elements with Raycast 1 Answer
RaycastHit always returns 0 1 Answer
C# raycast shooting script broken 1 Answer
[RESOLVED]Raycast problem 2 Answers
Raycast collision issue 0 Answers