Skip to content

Instantly share code, notes, and snippets.

@kalineh
Last active September 5, 2020 02:36
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kalineh/1eccb28c600ae98516ea577c0afb9339 to your computer and use it in GitHub Desktop.
Save kalineh/1eccb28c600ae98516ea577c0afb9339 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public class CameraSmooth
: MonoBehaviour
{
public Camera cameraTarget;
public Camera cameraSelf;
public bool enableSmooth = true;
[Range(0.0f, 12.0f)]
public float lerpPositionRate = 8.0f;
[Range(1.0f, 12.0f)]
public float lerpRotationRate = 4.0f;
public void Start()
{
if (!cameraSelf)
cameraSelf = GetComponent<Camera>();
cameraTarget = PlayerCameraSupport.GetCamera();
// just make sure smooth camera set to None (Main Display)
// vive will render the both eyes camera, and main game window will show smooth
cameraSelf.stereoTargetEye = StereoTargetEyeMask.None;
cameraSelf.targetDisplay = 0;
cameraSelf.fieldOfView = GameSettings.Instance.OptionEnableCameraSmoothingFOV;
cameraSelf.nearClipPlane = cameraTarget.nearClipPlane;
cameraSelf.farClipPlane = cameraTarget.farClipPlane;
cameraSelf.transform.position = cameraTarget.transform.position;
cameraSelf.transform.rotation = cameraTarget.transform.rotation;
cameraTarget.targetDisplay = 0;
}
public void FixedUpdate()
{
if (!cameraTarget)
return;
var posRate = lerpPositionRate;
var rotRate = lerpRotationRate;
if (enableSmooth)
{
transform.position = Vector3.Lerp(transform.position, cameraTarget.transform.position, Mathf.Clamp01(posRate * Time.fixedDeltaTime));
transform.rotation = Quaternion.Slerp(transform.rotation, cameraTarget.transform.rotation, Mathf.Clamp01(rotRate * Time.fixedDeltaTime));
}
else
{
transform.position = cameraTarget.transform.position;
transform.rotation = cameraTarget.transform.rotation;
}
}
}
@jorgeolothar
Copy link

Are you using external classes? My unity (2017.1) says context does not include PlayerCameraSupport or GameSettings.

@eric29
Copy link

eric29 commented May 16, 2019

For anyone else having issues with with "PlayerCameraSupport" and "GameSettings", simply comment out line 21, and for line 27 (fov), just put whatever value you want for the camera field of view (I put 60) instead of "GameSettings.Instance.OptionEnableCameraSmoothingFOV".

@HugoCortell
Copy link

What is the licence for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment