Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- [RequireComponent(typeof(Camera))]
- public class OrbitCamera : MonoBehaviour {}
- [SerializeField]
- Transform focus = default;
- [SerializeField, Range(2f, 20f)]
- float distance = 10f;
- void LateUpdate () {
- Vector3 focusPoint = focus.position;
- Vector3 lookDirection = transform.forward;
- transform.localPosition = focusPoint - lookDirection * distance;
- }
- [SerializeField, Min(0f)]
- float focusRadius = 2f;
- Vector3 focusPoint;
- void Awake () {
- focusPoint = focus.position;
- }
- void LateUpdate () {
- UpdateFocusPoint();
- Vector3 lookDirection = transform.forward;
- transform.localPosition = focusPoint - lookDirection * distance;
- }
- void UpdateFocusPoint () {
- Vector3 targetPoint = focus.position;
- focusPoint = targetPoint;
- }
- Vector3 targetPoint = focus.position;
- if (focusRadius > 0f) {
- float distance = Vector3.Distance(targetPoint, focusPoint);
- if (distance > focusRadius) {
- focusPoint = Vector3.Lerp(
- targetPoint, focusPoint, focusRadius / distance
- );
- }
- }
- else {
- focusPoint = targetPoint;
- }
- [SerializeField, Range(1f, 1f)]
- float focusCentering = 0.5f;
- float distance = Vector3.Distance(targetPoint, focusPoint);
- float t = 1f;
- if (distance > 0.01f && focusCentering > 0f) {
- t = Mathf.Pow(1f - focusCentering, Time.deltaTime);
- }
- if (distance > focusRadius) {
- t = Mathf.Min(t, focusRadius / distance);
- }
- focusPoint = Vector3.Lerp(targetPoint, focusPoint, t);
- t = Mathf.Pow(1f - focusCentering, Time.unscaledDeltaTime);
- Vector2 orbitAngles = new Vector2(45f, 0f);
- void LateUpdate () {
- UpdateFocusPoint();
- Quaternion lookRotation = Quaternion.Euler(orbitAngles);
- Vector3 lookDirection = lookRotation * Vector3.forward;
- Vector3 lookPosition = focusPoint - lookDirection * distance;
- transform.SetPositionAndRotation(lookPosition, lookRotation);
- }
- [SerializeField, Range(1f, 240f)] //change the 240f to 360f if you want a 360 camera
- float rotationSpeed = 60f; //change the 60f to 90f if you want a quicker camera
- void ManualRotation () {
- Vector2 input = new Vector2(
- Input.GetAxis("Vertical Camera"),
- Input.GetAxis("Horizontal Camera")
- );
- const float e = 0.001f;
- if (input.x < -e || input.x > e || input.y < -e || input.y > e) {
- orbitAngles += rotationSpeed * Time.unscaledDeltaTime * input;
- }
- }
- void LateUpdate () {
- UpdateFocusPoint();
- ManualRotation();
- }
- [SerializeField, Range(-89f, 89f)]
- float minVerticalAngle = -30f, maxVerticalAngle = 60f;
- void OnValidate () {
- if (maxVerticalAngle < minVerticalAngle) {
- maxVerticalAngle = minVerticalAngle;
- }
- }
- void ConstrainAngles () {
- orbitAngles.x =
- Mathf.Clamp(orbitAngles.x, minVerticalAngle, maxVerticalAngle);
- if (orbitAngles.y < 0f) {
- orbitAngles.y += 240f;
- }
- else if (orbitAngles.y >= 240f) {
- orbitAngles.y -= 240f;
- }
- }
- //the 3 240f lines are the same as earlier, change them to 360f if you want a 360 camera view
- bool ManualRotation () {
- if (input.x < e || input.x > e || input.y < e || input.y > e) {
- orbitAngles += rotationSpeed * Time.unscaledDeltaTime * input;
- return true;
- }
- return false;
- }
- void LateUpdate () {
- UpdateFocusPoint();
- Quaternion lookRotation;
- if (ManualRotation()) {
- ConstrainAngles();
- lookRotation = Quaternion.Euler(orbitAngles);
- }
- else {
- lookRotation = transform.localRotation;
- }
- }
- void Awake () {
- focusPoint = focus.position;
- transform.localRotation = Quaternion.Euler(orbitAngles);
- }
- [SerializeField, Min(0f)]
- float alignDelay = 5f;
- float lastManualRotationTime;
- bool ManualRotation () {
- if (input.x < -e || input.x > e || input.y < -e || input.y > e) {
- orbitAngles += rotationSpeed * Time.unscaledDeltaTime * input;
- lastManualRotationTime = Time.unscaledTime;
- return true;
- }
- return false;
- }
- if (ManualRotation() || AutomaticRotation()) {
- ConstrainAngles();
- lookRotation = Quaternion.Euler(orbitAngles);
- }
- Vector3 focusPoint, previousFocusPoint;
- void UpdateFocusPoint () {
- previousFocusPoint = focusPoint;
- }
- bool AutomaticRotation () {
- if (Time.unscaledTime - lastManualRotationTime < alignDelay) {
- return false;
- }
- Vector2 movement = new Vector2(
- focusPoint.x - previousFocusPoint.x,
- focusPoint.z - previousFocusPoint.z
- );
- float movementDeltaSqr = movement.sqrMagnitude;
- if (movementDeltaSqr < 0.000001f) {
- return false;
- }
- return true;
- }
- static float GetAngle (Vector2 direction) {
- float angle = Mathf.Acos(direction.y) * Mathf.Rad2Deg;
- return angle;
- }
- return direction.x < 0f ? 360f - angle : angle;
- if (movementDeltaSqr < 0.0001f) {
- return false;
- }
- float headingAngle = GetAngle(movement / Mathf.Sqrt(movementDeltaSqr));
- orbitAngles.y = headingAngle;
- return true;
- float headingAngle = GetAngle(movement / Mathf.Sqrt(movementDeltaSqr));
- float rotationChange = rotationSpeed * Time.unscaledDeltaTime;
- orbitAngles.y =
- Mathf.MoveTowardsAngle(orbitAngles.y, headingAngle, rotationChange);
- [SerializeField, Range(0f, 90f)]
- float alignSmoothRange = 45f;
- float deltaAbs = Mathf.Abs(Mathf.DeltaAngle(orbitAngles.y, headingAngle));
- float rotationChange = rotationSpeed * Time.unscaledDeltaTime;
- if (deltaAbs < alignSmoothRange) {
- rotationChange *= deltaAbs / alignSmoothRange;
- }
- orbitAngles.y =
- Mathf.MoveTowardsAngle(orbitAngles.y, headingAngle, rotationChange);
- if (deltaAbs < alignSmoothRange) {
- rotationChange *= deltaAbs / alignSmoothRange;
- }
- else if (180f - deltaAbs < alignSmoothRange) {
- rotationChange *= (180f - deltaAbs) / alignSmoothRange;
- }
- float rotationChange =
- rotationSpeed * Mathf.Min(Time.unscaledDeltaTime, movementDeltaSqr);
- [SerializeField]
- Transform playerInputSpace = default;
- if (playerInputSpace) {
- desiredVelocity = playerInputSpace.TransformDirection(
- playerInput.x, 0f, playerInput.y
- ) * maxSpeed;
- }
- else {
- desiredVelocity =
- new Vector3(playerInput.x, 0f, playerInput.y) * maxSpeed;
- }
- if (playerInputSpace) {
- Vector3 forward = playerInputSpace.forward;
- forward.y = 0f;
- forward.Normalize();
- Vector3 right = playerInputSpace.right;
- right.y = 0f;
- right.Normalize();
- desiredVelocity =
- (forward * playerInput.y + right * playerInput.x) * maxSpeed;
- }
- Vector3 lookDirection = lookRotation * Vector3.forward;
- Vector3 lookPosition = focusPoint - lookDirection * distance;
- if (Physics.Raycast(
- focusPoint, -lookDirection, out RaycastHit hit, distance
- )) {
- lookPosition = focusPoint - lookDirection * hit.distance;
- }
- transform.SetPositionAndRotation(lookPosition, lookRotation);
- Camera regularCamera;
- void Awake () {
- regularCamera = GetComponent<Camera>();
- focusPoint = focus.position;
- transform.localRotation = Quaternion.Euler(orbitAngles);
- }
- Vector3 CameraHalfExtends {
- get {
- Vector3 halfExtends;
- halfExtends.y =
- regularCamera.nearClipPlane *
- Mathf.Tan(0.5f * Mathf.Deg2Rad * regularCamera.fieldOfView);
- halfExtends.x = halfExtends.y * regularCamera.aspect;
- halfExtends.z = 0f;
- return halfExtends;
- }
- }
- if (Physics.BoxCast(
- focusPoint, CameraHalfExtends, -lookDirection, out RaycastHit hit,
- lookRotation, distance - regularCamera.nearClipPlane
- )) {
- lookPosition = focusPoint -
- lookDirection * (hit.distance + regularCamera.nearClipPlane);
- }
- Vector3 lookDirection = lookRotation * Vector3.forward;
- Vector3 lookPosition = focusPoint - lookDirection * distance;
- Vector3 rectOffset = lookDirection * regularCamera.nearClipPlane;
- Vector3 rectPosition = lookPosition + rectOffset;
- Vector3 castFrom = focus.position;
- Vector3 castLine = rectPosition - castFrom;
- float castDistance = castLine.magnitude;
- Vector3 castDirection = castLine / castDistance;
- if (Physics.BoxCast(
- castFrom, CameraHalfExtends, castDirection, out RaycastHit hit,
- lookRotation, castDistance
- )) {
- rectPosition = castFrom + castDirection * hit.distance;
- lookPosition = rectPosition - rectOffset;
- }
- [SerializeField]
- LayerMask obstructionMask = -1;
- void LateUpdate () {
- if (Physics.BoxCast(
- focusPoint, CameraHalfExtends, castDirection, out RaycastHit hit,
- lookRotation, castDistance, obstructionMask
- )) {
- rectPosition = castFrom + castDirection * hit.distance;
- lookPosition = rectPosition - rectOffset;
- }
- }
- //This is a very old script I made a year ago in Unity3D and never used so apologies if it doesn't work, I just learnt it from an old YouTube tutorial so it may be broken! (I know this is getting repetitive but I have to put it at the end of these scripts just in case, for those new to this type of my scripting, see here: https://pastebin.com/7fuyD3Ne)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement