Created
July 9, 2017 20:24
-
-
Save WhiteNoise/63c466e52b9b6a62086ae662794ac7f7 to your computer and use it in GitHub Desktop.
Smooth head following camera for recording VR
This file contains hidden or 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 System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class SmoothedHeadCam : MonoBehaviour { | |
public GameObject head; | |
[Tooltip("Set to true if you want the camera to face the user's head instead")] | |
public bool reverseMode = false; | |
// Update is called once per frame | |
void LateUpdate () { | |
float dist = 0.2f; | |
Quaternion targetRot; | |
Vector3 eulerRot = head.transform.localRotation.eulerAngles; | |
eulerRot.z = 0.0f; | |
if(reverseMode) { | |
eulerRot.y += 180.0f; | |
eulerRot.x = 0.0f; | |
dist = 1.1f; | |
} else { | |
eulerRot.x += 20.0f; | |
dist = 0.2f; | |
} | |
targetRot = Quaternion.Euler(eulerRot); | |
transform.localPosition = Vector3.Slerp(transform.localPosition, head.transform.localPosition - transform.parent.InverseTransformDirection(transform.forward) * dist, 3.0f * Time.unscaledDeltaTime); | |
transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRot, 2.0f * Time.unscaledDeltaTime); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment