Skip to content

Instantly share code, notes, and snippets.

@jbubriski
Created May 2, 2019 15:25
Show Gist options
  • Save jbubriski/ac466dde8eaede33c0c1502b29c499be to your computer and use it in GitHub Desktop.
Save jbubriski/ac466dde8eaede33c0c1502b29c499be to your computer and use it in GitHub Desktop.
CameraCollision for Unity
public class CameraCollision : MonoBehaviour
{
private Vector3 _dollyDirection;
private Vector3 _dollyDirectionAdjusted;
private float _distance;
public float MinDistance = 1.0f;
public floar MaxDistance = 4.0f;
public float Smooth = 1.0f;
private void Awake()
{
_dollyDirection = transform.localPosition.normalized;
_distance= transform.localPosition.magnitude;
}
private void Update()
{
var desiredCameraPosition = transform.parent.TransformPoint(_dollyDirection * MaxDistance);
RaycastHit hit;
if (Physics.Linecast())
{
_distance = Mathf.Clamp(hit.Distance * 0.9f), MinDistance, MaxDistance);
}
else
{
_distance = MaxDistance;
}
transform.localPosition = Vector3.Lerp(transform.localPosition, _dollyDirection * _distance, Time.deltaTime * Smooth)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment