I am trying to create a simple walkthrough using the MainCamera. And I’ve added simple script to move the camera, using transform.Translate.
Problem I’m facing is, the camera passes right through the terrain, trees and what not.
I’ve added the rigidBody component to the camera and with gravity on, the camera falls right through the terrain (has a terrain collider). Also tried Sphere collider to the camera and didn;t work.
Not able to figure out the problem. Please help and thanks in advance.
Hi @abhijit93
You could add the camera to a 3d gameobject (put the cam inside GameObject). then add the Rigidbody to that GameObject. It will not fall through the Terrain
I found another solution for anyone needing. Previously I had suggested putting Rigidbody and Sphere Collider to Camera (Collider has Trigger, Rigidbody is without gravity, freeze rotation X and Z from the constraints too)
But still the camera enters to terrain if player insists to go down the terrain and it clips the terrain (I changed terrain thickness to great numbers, still “sometimes” camera was entering into terrain.
So I developed another solution on the below, this minimizes passing through terrain very much.
Notes: The bottom part is your Mouse Scroll code, and the rest of the script is to limit the camera going below Terrain. I left comments above each section. Please make sure to add Sphere Collider (with Trigger on) and Rigidbody as I mentioned on the above message of mine. Then apply the script below.
Also I considered the Terrain has “Terrain” tag, change it if you use something else, but make sure your Terrain has a tag.
[Tooltip("The time to move Cam away from Terrain")]
public float smoothTime = 0.1f; //Make it 0.1f, 0.3f, 1f or any number according to your preference. This defines movement speed of cam if cam collides with terrain. Smaller number means quicker movement.
[Tooltip("The min permitted Height limit between Cam and Terrain")]
public float minCamTerrainHeight = 0.15f;
[Tooltip("Raycast height from the Cam to Terrain")]
public float terrainDetectionHeight = 2f;
public float zoomSpeed = 350f;
private Vector3 velocity = Vector3.zero;
private bool colliding = false;
void OnTriggerEnter (Collider other){
if (other.gameObject.CompareTag("Terrain"))
colliding = true;
}
void OnTriggerExit (Collider other){
if (other.gameObject.CompareTag("Terrain"))
colliding = false;
}
//If Camera collides with Terrain, move the camera smoothly above the terrain
if (colliding){
transform.localPosition = Vector3.SmoothDamp(transform.localPosition, new Vector3(0,minCamTerrainHeight,0), ref velocity, smoothTime);
}
//Check Distance between Terrain-Camera object
RaycastHit hit;
if(Physics.Raycast(transform.position, -transform.up, out hit, terrainDetectionHeight)) {
//If the camera is at a certain height above the Terrain, limit Mousescroll going down further but don't limit it going up:
if(hit.collider.tag=="Terrain"){
print("Camera is very close to Terrain.");
float moveDown = Input.GetAxis("Mouse ScrollWheel");
if (moveDown < 0)
transform.Translate(new Vector3(0, moveDown) * Time.deltaTime * -zoomSpeed, 0);
}
}
else
//move the camera when you scroll. This happens if it is above the terrainDetectionHeight.
transform.Translate(new Vector3(0, Input.GetAxis("Mouse ScrollWheel")) * Time.deltaTime * -zoomSpeed, 0);
Here is a basic camera follow script for
-
Smooth zoom
-
Ground detection moving closer to the player until the raycast is above the ground.
// Main public bool cameraEnabled = false; public float cameraTargetHeight = 1.5f; public float mouseSensitivity = 3f; public float lerpSpeed = 15f; // Zoom public float zoomSensitivity = 5; public float zoomStartDistance = 2; public float zoomMin = 1.5f; public float zoomMax = 20; // Ground Collision public float maxCollisionDetectionDistance = 0.5f; public float collisionCorrectionIncrement = 0.2f; private float x = 0.0f; private float y = 0.0f; private float distance = 3.0f; private float currentDistance = 3.0f; void Start() { distance = zoomStartDistance; currentDistance = zoomStartDistance; } void LateUpdate() { if (!cameraEnabled) return; if (Input.GetMouseButton(1)) { x += Input.GetAxis("Mouse X") * mouseSensitivity; y -= Input.GetAxis("Mouse Y") * mouseSensitivity; } float targetRotationAngle = cameraTarget.eulerAngles.y + rotateTargetOffsetY; float cameraRotationAngle = transform.eulerAngles.y; x = Mathf.LerpAngle(cameraRotationAngle, targetRotationAngle, lerpSpeed * Time.deltaTime); } y = ClampAngle(y, -50, 50); Quaternion rotation = Quaternion.Euler(y, x, 0); // Extremely basic collision where the current distance is reduced until the camera is above the ground RaycastHit hit; if (Physics.Raycast(transform.position, -transform.up, out hit, maxCollisionDetectionDistance )) { currentDistance -= collisionCorrectionIncrement; } // Smooth zoom currentDistance -= Input.GetAxis("Mouse ScrollWheel") * zoomSensitivity; currentDistance = Mathf.Clamp(currentDistance, zoomMin, zoomMax); distance = Mathf.Lerp(distance, currentDistance, Time.deltaTime * zoomSmooth); Vector3 position = cameraTarget.position - (rotation * Vector3.forward * distance + new Vector3(0, -cameraTargetHeight, 0)); transform.position = position; transform.rotation = rotation; } private float ClampAngle(float angle, float min, float max) { if (angle < -360) { angle += 360; } if (angle > 360) { angle -= 360; } return Mathf.Clamp(angle, min, max); }