Camera Problem (109859)

I use Edy’s Vehicle Physics that has its own camera but this camera goes through terrain and walls. How to prevent the camera from going through terrain?

CameraControl.js

#pragma strict

var Target : Transform;
var Target2 : Transform;

var MapCamera : Camera;
var DefaultCamera : int;

var showMirrors = true;
var MirrorLeftTex : GUITexture;
var MirrorRightTex : GUITexture;
var MirrorRearTex : GUITexture;

var enableImageEffects = true;
var ImageEffects : MonoBehaviour[];

private var m_cameraScript : MonoBehaviour[];
private var m_currCamera = DefaultCamera;		// Cбmara por defecto. NO puede ser la SmoothLookAtFromPos
private var m_numCameras = 6;			// Nъmero de scripts de cбmara controlados (hardcoded). La ъltima debe ser la SmothLookAtFromPos.

// Una variable para cada script que soportemos. 
// Es necesario ajustar cada script individualmente.

private var m_scrFixTo : CamFixTo;
private var m_scrSmoothFollow : CamSmoothFollow;
private var m_scrMouseOrbit : CamMouseOrbit;
private var m_scrSmoothLookAt : CamSmoothLookAt;
private var m_scrFreeView : CamFreeView;
private var m_scrSmoothLookAtFromPos : CamSmoothLookAtFromPos;

private var m_scrDriverFreeView : CamFreeView;
private var m_scrMapFollow : CamSmoothFollow;

private var m_lastTarget : Transform;
private var m_lastShowMirrors : boolean = showMirrors;
private var m_lastImageEffects : boolean = enableImageEffects;

private var m_targetCams : CarCameras;


// Localizar los componentes apropiados del Target y aplicarlos a las cбmaras

private function UpdateTarget()
	{
	// En este punto Target y m_targetCams aъn apuntan al Target anterior. 
	// Desactivar sus cбmaras espejo y el movimiento de la vista de conductor.
	
	if (m_targetCams)
		{
		if (m_targetCams.MirrorLeft) m_targetCams.MirrorLeft.enabled = false;
		if (m_targetCams.MirrorRight) m_targetCams.MirrorRight.enabled = false;
		if (m_targetCams.MirrorRear) m_targetCams.MirrorRear.enabled = false;
		
		if (m_scrDriverFreeView)
			m_scrDriverFreeView.enabled = false;
		}
	
	// Obtener los objetos necesarios del target
	
	m_targetCams = Target.GetComponent(CarCameras) as CarCameras;
	
	if (m_targetCams.DriverFront)
		m_scrDriverFreeView = m_targetCams.DriverFront.GetComponent(CamFreeView) as CamFreeView;
	else
		m_scrDriverFreeView = null;
		
	// Ajustar las camaras al target
	
	m_scrFixTo.Pos = m_targetCams.DriverFront;
	m_scrSmoothFollow.target = m_targetCams.CameraLookAtPoint;
	m_scrSmoothFollow.distance = m_targetCams.viewDistance;
	m_scrSmoothFollow.height = m_targetCams.viewHeight;
	m_scrSmoothFollow.rotationDamping = m_targetCams.viewDamping;
	m_scrSmoothFollow.reset = true;
	
	m_scrMouseOrbit.target = m_targetCams.CameraLookAtPoint;
	m_scrMouseOrbit.distance = m_targetCams.viewDistance;
	m_scrMouseOrbit.distMinLimit = m_targetCams.viewMinDistance;
	m_scrMouseOrbit.yMinLimit = m_targetCams.viewMinHeight;
	
	m_scrSmoothLookAt.target = m_targetCams.CameraLookAtPoint;	

	if (m_scrMapFollow) m_scrMapFollow.target = m_targetCams.CameraLookAtPoint;	
	
	// El script de combinar dos targets se habilita o no segъn Target2 estй establecido.
	// Target2 puede ser un Transform cualquiera. Si lleva un CarCameras, se usarб.	

	if (Target2)
		{
		var Target2Cameras : CarCameras;
		
		m_scrSmoothLookAtFromPos.pos = m_targetCams.CameraLookAtPoint;
		m_scrSmoothLookAtFromPos.positionZ = -m_targetCams.viewDistance;
		m_scrSmoothLookAtFromPos.positionY = m_targetCams.viewHeight / 2.0;
		
		Target2Cameras = Target2.GetComponent(CarCameras) as CarCameras;
		if (Target2Cameras)
			m_scrSmoothLookAtFromPos.target = Target2Cameras.CameraLookAtPoint;
		else
			m_scrSmoothLookAtFromPos.target = Target2;
		}
	else
		{
		// Si estaba seleccionada la cбmara ScrLookAtFromPos pero no tenнa Target2, establecer la cбmara por defecto.
		
		if (m_currCamera == m_numCameras-1)
			SwitchTo(DefaultCamera);
		}
		
	// Actualizar los espejos en el nuevo target
	
	UpdateMirrors();	
	}
	
	
function ClearMirrorTexture(Cam : Camera)
	{
	var oldClearFlags = Cam.clearFlags;
	var oldBackgroundColor = Cam.backgroundColor;
	var oldRect = Cam.rect;
	var oldCullingMask = Cam.cullingMask;
	
	Cam.clearFlags = CameraClearFlags.SolidColor;
	Cam.backgroundColor = Color(1.0, 1.0, 1.0, 1.0);	// Alpha 0.0=transparente, 1.0=opaco
	Cam.rect = Rect(0.0, 0.0, 1.0, 1.0);
	Cam.cullingMask = 0;
	
	Cam.Render();
	
	Cam.clearFlags = oldClearFlags;
	Cam.backgroundColor = oldBackgroundColor;
	Cam.rect = oldRect;
	Cam.cullingMask = oldCullingMask;
	}
	

function UpdateMirrors()
	{
	if (m_scrDriverFreeView)
		m_scrDriverFreeView.enabled = m_currCamera == 0;	
	
	// Determinar es adecuado mostrar u ocultar los espejos. Se muestran si:
	// - Disponemos de la textura corresponiente para mostrarlo.
	// - Estamos en vista del conductor.
	// - Estбn los espejos habilitados.	
	
	if (m_currCamera == 0 && showMirrors)
		{	
		if (MirrorLeftTex)
			if (m_targetCams.MirrorLeft)
				{
				m_targetCams.MirrorLeft.targetTexture = MirrorLeftTex.texture as RenderTexture; 
				m_targetCams.MirrorLeft.enabled = true;
				MirrorLeftTex.enabled = true;
				
				ClearMirrorTexture(m_targetCams.MirrorLeft);
				}
			else
				MirrorLeftTex.enabled = false;
		
		if (MirrorRightTex)
			if (m_targetCams.MirrorRight)
				{
				m_targetCams.MirrorRight.targetTexture = MirrorRightTex.texture as RenderTexture;
				m_targetCams.MirrorRight.enabled = true;
				MirrorRightTex.enabled = true;
				
				ClearMirrorTexture(m_targetCams.MirrorRight);
				}
			else
				MirrorRightTex.enabled = false;
			
		if (MirrorRearTex)
			if (m_targetCams.MirrorRear)
				{
				m_targetCams.MirrorRear.targetTexture = MirrorRearTex.texture as RenderTexture;
				m_targetCams.MirrorRear.enabled = true;
				MirrorRearTex.enabled = true;
				
				ClearMirrorTexture(m_targetCams.MirrorRear);
				}
			else
				MirrorRearTex.enabled = false;
		}
	else
		{
		if (m_targetCams.MirrorLeft) m_targetCams.MirrorLeft.enabled = false;
		if (m_targetCams.MirrorRight) m_targetCams.MirrorRight.enabled = false;
		if (m_targetCams.MirrorRear) m_targetCams.MirrorRear.enabled = false;
		
		if (MirrorLeftTex) MirrorLeftTex.enabled = false;
		if (MirrorRightTex) MirrorRightTex.enabled = false;
		if (MirrorRearTex) MirrorRearTex.enabled = false;
		}
	}
	

function Start ()
	{	
	m_currCamera = DefaultCamera;
	m_lastTarget = Target;
	
	// Obtener los scripts de la cбmara asociados y dejar la cбmara por defecto.
	// Hay que mantener el nъmero de cбmaras ajustado al nъmero de componentes.
	// La ъltima cбmara debe ser SmoothLookAtFromPos (lleva tratamiento especial).
	
	m_scrFixTo = GetComponent(CamFixTo) as CamFixTo;
	m_scrSmoothFollow = GetComponent(CamSmoothFollow) as CamSmoothFollow;
	m_scrMouseOrbit = GetComponent(CamMouseOrbit) as CamMouseOrbit;
	m_scrSmoothLookAt = GetComponent(CamSmoothLookAt) as CamSmoothLookAt;
	m_scrFreeView = GetComponent(CamFreeView) as CamFreeView;
	m_scrSmoothLookAtFromPos = GetComponent(CamSmoothLookAtFromPos) as CamSmoothLookAtFromPos;
	
	m_cameraScript = new MonoBehaviour[m_numCameras];
	m_cameraScript[0] = m_scrFixTo;
	m_cameraScript[1] = m_scrSmoothFollow;
	m_cameraScript[2] = m_scrMouseOrbit;
	m_cameraScript[3] = m_scrSmoothLookAt;
	m_cameraScript[4] = m_scrFreeView;
	m_cameraScript[5] = m_scrSmoothLookAtFromPos;
	
	if (MapCamera)
		m_scrMapFollow = MapCamera.GetComponent(CamSmoothFollow) as CamSmoothFollow;
		
	// Desactivar las texturas para las cбmaras espejo
	
	if (MirrorLeftTex) MirrorLeftTex.enabled = false;
	if (MirrorRightTex) MirrorRightTex.enabled = false;
	if (MirrorRearTex) MirrorRearTex.enabled = false;
	
	// Asignar las propiedades del Target a las cбmaras. Puede no haber Target asignado al inicio.
	
	if (Target) UpdateTarget();
	
	// Establecer los efectos de imagen
	
	for (var i=0; i<ImageEffects.length; i++)
		ImageEffects*.enabled = enableImageEffects;*
  • // Establecer la cбmara inicial.*

  • for (i=0; i<m_numCameras; i++)*
    m_cameraScript*.enabled = false;
    m_cameraScript[m_currCamera].enabled = true;
    _
    }*_

function Update ()
* {*
* if (m_lastTarget != Target)
_
{ _
_
// Ajustar las cбmaras al nuevo target*_

* UpdateTarget();*
* m_lastTarget = Target;
_
}*_

* if (m_lastShowMirrors != showMirrors)
_
{_
_
UpdateMirrors();_
m_lastShowMirrors = showMirrors;
_
}*_

* if (m_lastImageEffects != enableImageEffects)
_
{_
_
for (var i=0; i<ImageEffects.length; i++)_
_ ImageEffects.enabled = enableImageEffects;_
m_lastImageEffects = enableImageEffects;
_ }*_

* }*

function Next ()
* {*
* m_cameraScript[m_currCamera++].enabled = false;
if (m_currCamera >= m_numCameras || (m_currCamera == m_numCameras-1 && !Target2)) m_currCamera = 0;
m_cameraScript[m_currCamera].enabled = true;*

* m_scrSmoothFollow.reset = true;
_ UpdateMirrors();
}*_

function SwitchTo (Cam : int)
* {*
* if (Cam < m_numCameras)
_ {
// Si es la cбmara del conductor, ya estaba seleccionada, y ademбs tiene un FreeView, reiniciar su rotaciуn a la original.
// NOTA: Se usa FreeView con movimiento=0 y no MouseLook porque con FreeView se puede reiniciar la rotaciуn del transform, pero con MouseLook no.*_

* if (Cam == 0 && Cam == m_currCamera)
_ {_
var DriverCam : CamFreeView = m_targetCams.DriverFront.GetComponent(CamFreeView) as CamFreeView;
_ if (DriverCam)_
DriverCam.SetLocalEulerAngles(m_targetCams.getDriverViewAngles());
_ }*_

* // Establecer la cбmara indicada*

* m_cameraScript[m_currCamera].enabled = false;
m_cameraScript[Cam].enabled = true;
m_currCamera = Cam;*

* m_scrSmoothFollow.reset = true;
_ UpdateMirrors();
}
}*_

function ToggleMap ()
* {*
* if (MapCamera)*
* MapCamera.enabled = !MapCamera.enabled;*
* }*

CamSmoothFollow.js

#pragma strict

// The target we are following
var target : Transform;
// The distance in the x-z plane to the target
var distance = 10.0;
// the height we want the camera to be above the target
var height = 5.0;
// Look above the target (height * this ratio)
var targetHeightRatio = 0.5;
// How fast we reach the target values
var heightDamping = 2.0;
var rotationDamping = 3.0;

var followVelocity = true;
var velocityDamping = 5.0;

private var lastPos = Vector3.zero;
private var currentVelocity = Vector3.zero;
private var wantedRotationAngle = 0.0;

@HideInInspector
var reset = true; // Make true from scripting for resetting the direction settings

function LateUpdate () {
* // Early out if we don’t have a target*
* if (!target) return;*

* if (reset)*
* {*
* lastPos = target.position;*
* wantedRotationAngle = target.eulerAngles.y;*
_ currentVelocity = target.forward * 2.0;
* reset = false;
}*_

* var updatedVelocity = (target.position - lastPos) / Time.deltaTime;*
* updatedVelocity.y = 0.0;*

* if (updatedVelocity.magnitude > 1.0)*
* {*
_ currentVelocity = Vector3.Lerp(currentVelocity, updatedVelocity, velocityDamping * Time.deltaTime);
wantedRotationAngle = Mathf.Atan2(currentVelocity.x, currentVelocity.z) * Mathf.Rad2Deg;
* }
lastPos = target.position;*_

* if (!followVelocity)*
* wantedRotationAngle = target.transform.eulerAngles.y;*

_ /
var velocity = (target.position - lastPos) / Time.deltaTime;
velocity.y = 0.0;*_

// AQUI: Hacer un Damp con velocity para evitar brusquedades (updatedVelocity, currentVelocity, velocityDamping)

* var wantedRotationAngle = target.eulerAngles.y;*
* if (velocity.magnitude > 1.0)*
_ wantedRotationAngle = Mathf.Atan2(velocity.x, velocity.z) * Mathf.Rad2Deg;
* lastPos = target.position;
/_

* // Calculate the current rotation angles*
* //var wantedRotationAngle = target.eulerAngles.y;*
* var wantedHeight = target.position.y + height;*

* var currentRotationAngle = transform.eulerAngles.y;*
* var currentHeight = transform.position.y;*

* // Damp the rotation around the y-axis*
_ currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);_

* // Damp the height*
_ currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);_

* // Convert the angle into a rotation*
* var currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);*

* // Set the position of the camera on the x-z plane to:*
* // distance meters behind the target*
* transform.position = target.position;*
_ transform.position -= currentRotation * Vector3.forward * distance;_

* // Set the height of the camera*
* transform.position.y = currentHeight;*

* if (target.rigidbody)*
* {*
* // We use centerOfMass instead of worldCenterOfMass because the first one is interpolated.*

* var CoM = Vector3.Scale(target.rigidbody.centerOfMass, Vector3(1.0/target.transform.localScale.x, 1.0/target.transform.localScale.y, 1.0/target.transform.localScale.z));*
* CoM = target.transform.TransformPoint(CoM);*

_ transform.LookAt (CoM + Vector3.upheighttargetHeightRatio);
* }
else*
transform.LookAt (target.position + Vector3.upheighttargetHeightRatio);_

}

I think you need to use wow camera script . i found it here.
http://forum.unity3d.com/threads/wow-camera-movement.16949/

I took an exact phrase from your question “camera from going through terrain” and added unity3d. Now here’s the crucial part. I put that into the search box on google.com. It’s a pretty well-known search engine. But they’re also making driverless cars and stuff. Anyway, not critical. The search part was critical… I’m not going to paste the results, cause I don’t want to rob you of the experience of using google. Cause you should ALWAYS use google and ask here if you get stumped after rigorous google searching!