orbiting mouse controlled camera goes thru terrain

The plan is two make this available on PC only.
Use keyboard to control the helicopters, use mouse to orbit view about helicopter.
Problem: The camera is going thru the terrain, it will probably also go thru all meshes
Video here shows current nasty…
link text

Here is my scene showing the assets connected to the camera…

Here is the code … including my failed attempts to keep the camera “in the air”…

using UnityEngine;
using System.Collections;

public class CamMouseOrbit : MonoBehaviour
{

	public Transform target;
	public float distance = 10.0f;
	public float xSpeed = 5f;
	public float ySpeed = 2.5f;
	public float distSpeed = 10.0f;
	public float yMinLimit = -20.0f;
	public float yMaxLimit = 80.0f;
	public float distMinLimit = 5.0f;
	public float distMaxLimit = 50.0f;
	public float orbitDamping = 4.0f;
	public float distDamping = 4.0f;
	private float x = 0.0f;
	private float y = 0.0f;
	private float dist;

	void Start ()
	{
		dist = distance;
		
		Vector3 angles = transform.eulerAngles;
		x = angles.y;
		y = angles.x;
	
		// Make the rigid body not change rotation
	
		if (rigidbody)
			rigidbody.freezeRotation = true;
	}
	
	void LateUpdate ()
	{
		if (!target)
			return;

		Vector3 oPos = transform.position;
	
		x += Input.GetAxis ("Mouse X") * xSpeed;
		y -= Input.GetAxis ("Mouse Y") * ySpeed;
		distance -= Input.GetAxis ("Mouse ScrollWheel") * distSpeed;
	
		y = ClampAngle (y, yMinLimit, yMaxLimit);		   
		distance = Mathf.Clamp (distance, distMinLimit, distMaxLimit);
	
		dist = Mathf.Lerp (dist, distance, distDamping * Time.deltaTime);		
		transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.Euler (y, x, 0), Time.deltaTime * orbitDamping);

		transform.position = transform.rotation * new Vector3 (0.0f, 0.0f, -dist) + target.position;

//		Vector3 newPos = transform.rotation * new Vector3 (0.0f, 0.0f, -dist) + target.position;
//		newPos.y = Mathf.Clamp(newPos.y,Terrain.activeTerrain.SampleHeight(oPos),0.5f);
//		transform.position = newPos;


//		float dist1 = newPos.y - Terrain.activeTerrain.SampleHeight(newPos);
//
//		if(dist1 < 0.5)
//		{
//			newPos.y = newPos.y + 0.7f;
//		}
//		transform.position = newPos;

//		Vector3 wantedPosition = transform.rotation * new Vector3 (0.0f, 0.0f, -dist) + target.position;
//
//		//Vector3 bumperRayOffset;
//		Vector3 back = target.transform.TransformDirection(-1 * Vector3.forward); 
//		RaycastHit hit;
//
//		//if (Physics.Raycast(target.TransformPoint(bumperRayOffset), back, out hit, 2.5f)
//    	if (Physics.Raycast(target.position, back, out hit, 2.5f)
//		    && hit.transform != target) // ignore ray-casts that hit the user. DR
//		{
//			// clamp wanted position to hit position
//			wantedPosition.x = hit.point.x;
//			wantedPosition.z = hit.point.z;
//			wantedPosition.y = Mathf.Lerp(hit.point.y + 1.0f, wantedPosition.y, Time.deltaTime * 5.0f);
//		} 
//		
//		transform.position = Vector3.Lerp(transform.position, wantedPosition, Time.deltaTime * 5.0f);

	}

	float ClampAngle (float a, float min, float max)
	{
		while (max < min)
			max += 360.0f;
		while (a > max)
			a -= 360.0f;
		while (a < min)
			a += 360.0f;
	
		if (a > max) {
			if (a - (max + min) * 0.5f < 180.0f)
				return max;
			else
				return min;
		} else
			return a;
	}

}

How can I stop the camera from penetrating the terrain (and other meshes)?
Thx …

There are several ways to avoid this problems but you’ll need to use collision detection.

You’ll need to add a collider on the camera and check if it is colliding with something.
You can do this manually in code by handling all collision behaviours or you can add a collider material to make the camera slip on the surface.

There are a lot of tutorials that could be easily found about that topic. :slight_smile:

I have a temporary fix : )

add this function to make sure raycast between points hits only the Player…

	bool ViewingPosCheck (Vector3 checkPos)
	{
		RaycastHit hit;
		if(Physics.Raycast(checkPos, target.position - checkPos, out hit, distance))
			if(hit.transform.tag != "Player")
				return false;
		newPos = checkPos;
		return true;
	}

then add it to the update like this…

		if(ViewingPosCheck(checkPos))
			transform.position = newPos;
		else
		{
			Vector3 v = target.position - checkPos;
			Vector3 tenPercent = v / 10;
			transform.position = Vector3.Lerp(transform.position, transform.position + tenPercent, Time.deltaTime);
		}