hey guys. i have a camera movement c# script that follows the player and also has mouse orbit. however, the camera goes through the terrain and buildings and i want to fix that. here’s the script:
using UnityEngine;
using System.Collections;
public class CameraMovement : MonoBehaviour {
public Transform target;
public float walkDistance;
public float runDistance;
public float height;
public float xSpeed = 250.0f;
public float ySpeed = 120.0f;
public float heightDamping = 2.0f;
public float rotationDamping = 3.0f;
private Transform _myTransform;
private float _x;
private float _y;
private bool _camButtonDown = false;
void Awake() {
_myTransform = transform;
}
// Use this for initialization
void Start () {
if(target == null)
Debug.LogWarning("there is no target assigned to the camera");
else {
CameraSetUp();
}
}
void Update() {
if(Input.GetMouseButtonDown(1)) { //Use the Input Manager to make this user selectable button
_camButtonDown = true;
}
if(Input.GetMouseButtonUp(1)) {
_camButtonDown = false;
}
}
void LateUpdate() {
// _myTransform.position = new Vector3(target.position.x, target.position.y + height, target.position.z - walkDistance);
// _myTransform.LookAt(target);
if(target != null) {
if(_camButtonDown) {
_x += Input.GetAxis(“Mouse X”) * xSpeed * 0.02f;
_y -= Input.GetAxis(“Mouse Y”) * ySpeed * 0.02f;
// y = ClampAngle(y, yMinLimit, yMaxLimit);
Quaternion rotation = Quaternion.Euler(_y, _x, 0);
Vector3 position = rotation * new Vector3(0.0f, 0.0f, -walkDistance) + target.position;
_myTransform.rotation = rotation;
_myTransform.position = position;
}
else {
// _myTransform.position = new Vector3(target.position.x, target.position.y + height, target.position.z - walkDistance);
// _myTransform.LookAt(target);
_x = 0;
_y = 0;
// Calculate the current rotation angles
float wantedRotationAngle = target.eulerAngles.y;
float wantedHeight = target.position.y + height;
float currentRotationAngle = _myTransform.eulerAngles.y;
float currentHeight = _myTransform.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
Quaternion currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);
// Set the position of the camera on the x-z plane to:
// distance meters behind the target
_myTransform.position = target.position;
_myTransform.position -= currentRotation * Vector3.forward * walkDistance;
// Set the height of the camera
_myTransform.position = new Vector3(_myTransform.position.x, currentHeight, _myTransform.position.z);
// Always look at the target
_myTransform.LookAt (target);
}
}
}
public void CameraSetUp() {
_myTransform.position = new Vector3(target.position.x, target.position.y + height, target.position.z - walkDistance);
_myTransform.LookAt(target);
}
}
i know its a long script, but maybe someone can give some code in c# that keeps the camera from going through the terrain. thanks.