From b61e95afb9c7bb6051aaea65afa59c94220c1609 Mon Sep 17 00:00:00 2001 From: Mister_Nebula <41904486+misternebula@users.noreply.github.com> Date: Sun, 1 Nov 2020 11:52:31 +0000 Subject: [PATCH] add clip prevention to camera --- QSB/Instruments/InstrumentsManager.cs | 4 -- QSB/Instruments/QSBCamera/CameraController.cs | 42 ++++++++++++++++--- QSB/Instruments/QSBCamera/CameraManager.cs | 8 +--- QSB/Instruments/QSBCamera/CameraMode.cs | 7 +--- 4 files changed, 39 insertions(+), 22 deletions(-) diff --git a/QSB/Instruments/InstrumentsManager.cs b/QSB/Instruments/InstrumentsManager.cs index 49cfd6df..05378ae2 100644 --- a/QSB/Instruments/InstrumentsManager.cs +++ b/QSB/Instruments/InstrumentsManager.cs @@ -1,8 +1,4 @@ using QSB.Instruments.QSBCamera; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using UnityEngine; namespace QSB.Instruments diff --git a/QSB/Instruments/QSBCamera/CameraController.cs b/QSB/Instruments/QSBCamera/CameraController.cs index f766412f..6780da10 100644 --- a/QSB/Instruments/QSBCamera/CameraController.cs +++ b/QSB/Instruments/QSBCamera/CameraController.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using UnityEngine; +using UnityEngine; namespace QSB.Instruments.QSBCamera { @@ -13,16 +9,50 @@ namespace QSB.Instruments.QSBCamera private Quaternion _rotationX; private Quaternion _rotationY; + private const float PercentToMove = 0.75f; + + public GameObject CameraObject; + void FixedUpdate() { if (CameraManager.Instance.Mode != CameraMode.ThirdPerson) { return; } + UpdatePosition(); UpdateInput(); UpdateRotation(); } + private void UpdatePosition() + { + var origin = transform.position; + var localDirection = CameraObject.transform.localPosition.normalized; + Vector3 localTargetPoint; + if (Physics.Raycast(origin, transform.TransformDirection(localDirection), out RaycastHit outRay, 10, LayerMask.GetMask("Default"))) + { + localTargetPoint = transform.InverseTransformPoint(outRay.point) * PercentToMove; + } + else + { + localTargetPoint = localDirection * 10 * PercentToMove; + } + var targetDistance = Vector3.Distance(origin, transform.TransformPoint(localTargetPoint)); + var currentDistance = Vector3.Distance(origin, CameraObject.transform.position); + Vector3 movement; + if (targetDistance < currentDistance) + { + // Snap to target to avoid clipping + movement = localTargetPoint; + } + else + { + // Move camera out slowly + movement = Vector3.MoveTowards(CameraObject.transform.localPosition, localTargetPoint, Time.fixedDeltaTime * 2f); + } + CameraObject.transform.localPosition = movement; + } + private void UpdateInput() { var input = OWInput.GetValue(InputLibrary.look, false, InputMode.All); @@ -38,7 +68,7 @@ namespace QSB.Instruments.QSBCamera _rotationX = Quaternion.AngleAxis(_degreesX, Vector3.up); _rotationY = Quaternion.AngleAxis(_degreesY, -Vector3.right); var localRotation = _rotationX * _rotationY * Quaternion.identity; - gameObject.transform.parent.localRotation = localRotation; + transform.localRotation = localRotation; } } } diff --git a/QSB/Instruments/QSBCamera/CameraManager.cs b/QSB/Instruments/QSBCamera/CameraManager.cs index cc741827..2fe55614 100644 --- a/QSB/Instruments/QSBCamera/CameraManager.cs +++ b/QSB/Instruments/QSBCamera/CameraManager.cs @@ -1,11 +1,6 @@ using OWML.Common; using QSB.Utility; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using UnityEngine; -using UnityEngine.PostProcessing; namespace QSB.Instruments.QSBCamera { @@ -59,7 +54,8 @@ namespace QSB.Instruments.QSBCamera Camera.enabled = false; OWCamera = CameraObj.AddComponent(); OWCamera.renderSkybox = true; - CameraObj.AddComponent(); + + CameraBase.AddComponent().CameraObject = CameraObj; var screenGrab = CameraObj.AddComponent(); screenGrab._downsampleShader = Locator.GetPlayerCamera().gameObject.GetComponent()._downsampleShader; diff --git a/QSB/Instruments/QSBCamera/CameraMode.cs b/QSB/Instruments/QSBCamera/CameraMode.cs index 1121015a..738ca214 100644 --- a/QSB/Instruments/QSBCamera/CameraMode.cs +++ b/QSB/Instruments/QSBCamera/CameraMode.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -namespace QSB.Instruments.QSBCamera +namespace QSB.Instruments.QSBCamera { public enum CameraMode {