Keeping the camera above the ground

Howdy All,

Just a quick question. Anybody have any suggestions on keeping the third person follow camera from going “underground” when moving around the terrain? I’d appreciate ANY help at all.

Thank you,
–Richard

if(transform.position.y <= 0)
transform.position.y = 0;

Thanks for the post,

I plugged that bit into the script but it doesn’t seem to be having any effect. I’m not getting any errors either. Any thoughts?

By the way… I love that picture.

the previous example assumes you have flat ground everywhere for y = 0.

To do what he suggested you really need to do a raycast from above on your terrain then use that y value. You might need to add a small amount to it to ensure it is above the terrain.

Thanks a lot Raider!

How exactly would I go about accomplishing that raycast task?

Expert from a previous post

			// raycast test for distance
			dist=distance;
			var hit : RaycastHit;
			if(Physics.Raycast(camera.position, currentRotation * -Vector3.forward, hit, distance)){
				dist=(hit.point-camera.position).magnitude - 1;
				
			}

Look into that code for a better explanation of what it all does, but that is the code that does a raycast from the target (focalpoint) to where the camera should be and gives the distance to that point. (minus 1 unit to prevent clipping)

http://forum.unity3d.com/threads/76973-Car-Veiw-script-problem?p=493081&viewfull=1#post493081

Thanks, BigMisterB