I have a regular Camera and this script is attached to the camera. When I rotate the camera around and the camera hits an object sometimes the camera gets pushed and if the camera distance from the target is 5 then when pushed the distance is almost 0.
The variable distanceFromTarget is set to 5 by default for example the distance from the player is 5. The problem is when the camera should or hit an object the camera in most of the cases move through the objects even if the objects have a collider and in some cases sometimes the camera distance from the player is change from 5 to almost 0.
Not sure if I need to add a collider and how to the camera or to do something else in the script ?
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class CameraController : MonoBehaviour
{
public Transform targetToRotateAround;
public float distanceFromTarget = 5.0f;
public float xSpeed = 120.0f;
public float ySpeed = 120.0f;
public float yMinLimit = -20f;
public float yMaxLimit = 80f;
public float distanceMin = .5f;
public float distanceMax = 15f;
public float cameraHeight;
public bool makeTransition = false;
private Rigidbody rigidbody;
private float x = 0.0f;
private float y = 0.0f;
// Use this for initialization
void Start()
{
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
rigidbody = GetComponent<Rigidbody>();
// Make the rigid body not change rotation
if (rigidbody != null)
{
rigidbody.freezeRotation = true;
}
}
private void Update()
{
}
void LateUpdate()
{
if (targetToRotateAround)
{
x += Input.GetAxis("Mouse X") * xSpeed * distanceFromTarget * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
y = ClampAngle(y, yMinLimit, yMaxLimit);
Quaternion rotation = Quaternion.Euler(y, x, 0);
distanceFromTarget = Mathf.Clamp(distanceFromTarget - Input.GetAxis("Mouse ScrollWheel") * 5, distanceMin, distanceMax);
RaycastHit hit;
if (Physics.Linecast(targetToRotateAround.position, transform.position, out hit))
{
distanceFromTarget -= hit.distance;
}
Vector3 negDistance = new Vector3(0.0f, cameraHeight, -distanceFromTarget);
Vector3 position = rotation * negDistance + targetToRotateAround.position;
transform.rotation = rotation;
transform.position = position;
}
}
public static float ClampAngle(float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp(angle, min, max);
}
}