Created
August 15, 2020 03:32
-
-
Save fauxnik/1a30def48a6a0b49720e30c7edd90ee0 to your computer and use it in GitHub Desktop.
SmoothDamp not working
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
using UnityEngine; | |
namespace SmoothCamera | |
{ | |
class SmoothTracking : MonoBehaviour | |
{ | |
public static void SetupSmoothedCamera() | |
{ | |
Main.Log(">>> >>> >>> Setting up camera..."); | |
if (GameObject.Find("SmoothCamera") != null) | |
{ | |
Main.LogWarning("SmoothCamera already exists. Skipping camera setup."); | |
return; | |
} | |
var mainCam = Camera.main; | |
var smoothCam = new GameObject { name = "SmoothCamera" }.AddComponent<Camera>(); | |
smoothCam.CopyFrom(mainCam); | |
smoothCam.stereoTargetEye = StereoTargetEyeMask.None; | |
smoothCam.depth = mainCam.depth + 1; | |
smoothCam.gameObject.AddComponent<SmoothTracking>().mainCam = mainCam; | |
} | |
public static void TeardownSmoothedCamera() | |
{ | |
Main.Log(">>> >>> >>> Tearing down camera..."); | |
var smoothCam = GameObject.Find("SmoothCamera"); | |
if (smoothCam == null) | |
{ | |
Main.LogWarning("SmoothCamera not found. Skipping camera teardown."); | |
return; | |
} | |
Destroy(smoothCam); | |
} | |
void OnEnable() | |
{ | |
positionVelo = new Vector3(0, 0, 0); | |
} | |
void LateUpdate() | |
{ | |
float smoothTime = Main.settings.smoothTime; | |
var smoothPosition = Vector3.SmoothDamp(transform.position, mainCam.transform.position, ref positionVelo, smoothTime); | |
//var smoothPosition = new Vector3( | |
// Mathf.SmoothDamp(transform.position.x, mainCam.transform.position.x, ref xVelo, smoothTime), | |
// Mathf.SmoothDamp(transform.position.y, mainCam.transform.position.y, ref yVelo, smoothTime), | |
// Mathf.SmoothDamp(transform.position.z, mainCam.transform.position.z, ref zVelo, smoothTime) | |
//); | |
Main.Log($"from {transform.position} to {mainCam.transform.position} via {smoothPosition} with {positionVelo} over {smoothTime}"); | |
transform.position = smoothPosition; | |
//transform.position = mainCam.transform.position; | |
/* | |
var deviationAngle = Quaternion.Angle(transform.rotation, mainCam.transform.rotation); | |
var catchUpAngle = Mathf.SmoothDampAngle(0, deviationAngle, ref angleVelo, smoothTime); | |
Vector3 thisAxis = transform.rotation.Axis(); | |
Vector3 mainCamAxis = mainCam.transform.rotation.Axis(); | |
var deviationAxis = Vector3.Cross(thisAxis, mainCamAxis); | |
if (catchUpAxis == null) { catchUpAxis = deviationAxis; } | |
catchUpAxis = Vector3.SmoothDamp(catchUpAxis, deviationAxis, ref axisVelo, smoothTime); | |
transform.rotation = Quaternion.AngleAxis(catchUpAngle, catchUpAxis) * transform.rotation; | |
*/ | |
transform.rotation = mainCam.transform.rotation; | |
GetComponent<Camera>().fieldOfView = Main.settings.fieldOfView; | |
} | |
public Camera mainCam; | |
Vector3 positionVelo; | |
float xVelo; | |
float yVelo; | |
float zVelo; | |
float angleVelo; | |
Vector3 axisVelo; | |
Vector3 catchUpAxis; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment