77 lines
1.4 KiB
C#
77 lines
1.4 KiB
C#
using System;
|
|
using BitStrap;
|
|
using UnityEngine;
|
|
|
|
public class VRCameraConstraint : MonoBehaviour
|
|
{
|
|
public Transform CameraRig;
|
|
|
|
public Transform CenterEyeAnchor;
|
|
|
|
public bool EnableCollision;
|
|
|
|
[ReadOnly]
|
|
public float CurrentDistance;
|
|
|
|
private readonly Action _cameraUpdateAction;
|
|
|
|
private readonly Action _preCharacterMovementAction;
|
|
|
|
private CharacterController _character;
|
|
|
|
private VRCameraConstraint()
|
|
{
|
|
_cameraUpdateAction = CameraUpdate;
|
|
_preCharacterMovementAction = PreCharacterMovement;
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
_character = GetComponent<CharacterController>();
|
|
if (!VRManager.IsVROn())
|
|
{
|
|
UnityEngine.Object.DestroyImmediate(this);
|
|
}
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
PreCharacterMovement();
|
|
}
|
|
|
|
private void CameraUpdate()
|
|
{
|
|
}
|
|
|
|
private void PreCharacterMovement()
|
|
{
|
|
Vector3 position = CameraRig.transform.position;
|
|
Vector3 vector = CenterEyeAnchor.position - base.transform.position;
|
|
vector.y = 0f;
|
|
if (vector.magnitude > 0f)
|
|
{
|
|
_character.Move(vector);
|
|
Vector3 vector2 = base.transform.position - CenterEyeAnchor.position;
|
|
vector2.y = 0f;
|
|
CurrentDistance = vector2.magnitude;
|
|
CameraRig.transform.position = position;
|
|
if (EnableCollision && CurrentDistance > 0f)
|
|
{
|
|
CameraRig.transform.position = position - vector;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
CurrentDistance = 0f;
|
|
}
|
|
}
|
|
}
|