Created
May 2, 2019 15:25
-
-
Save jbubriski/ac466dde8eaede33c0c1502b29c499be to your computer and use it in GitHub Desktop.
CameraCollision for Unity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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