Skip to content

Commit

Permalink
Revert "Revert "Decouple body from head rotation""
Browse files Browse the repository at this point in the history
  • Loading branch information
DaXcess authored Jan 20, 2024
1 parent db5ea22 commit d282237
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions Player/VRPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ internal class VRPlayer : MonoBehaviour
private float crouchOffset = 0f;
private float realHeight = 2.3f;

private readonly float sqrMoveThreshold = 1E-5f;
private readonly float turnAngleThreshold = 120.0f;
private readonly float turnWeightSharp = 15.0f;

private bool isDead = false;
private bool isSprinting = false;

Expand Down Expand Up @@ -408,6 +412,8 @@ private void Update()
if (!ShipBuildModeManager.Instance.InBuildMode)
turningProvider.Update();

var lastOriginPos = xrOrigin.position;

// If we are in special animation allow 6 DOF but don't update player position
if (!playerController.inSpecialInteractAnimation)
xrOrigin.position = new Vector3(transform.position.x - cameraPosAccounted.x * SCALE_FACTOR, transform.position.y, transform.position.z - cameraPosAccounted.z * SCALE_FACTOR);
Expand All @@ -434,8 +440,12 @@ private void Update()

//Logger.LogDebug($"{transform.position} {xrOrigin.position} {leftHandVRTarget.transform.position} {rightHandVRTarget.transform.position} {cameraFloorOffset} {cameraPosAccounted}");

if (!playerController.inSpecialInteractAnimation)
transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, mainCamera.transform.eulerAngles.y, transform.rotation.eulerAngles.z);
if ((xrOrigin.position - lastOriginPos).sqrMagnitude > sqrMoveThreshold) // player moved
// Rotate body sharply but still smoothly
TurnBodyToCamera(turnWeightSharp);
else if (!playerController.inSpecialInteractAnimation && GetBodyToCameraAngle() is var angle && angle > turnAngleThreshold)
// Rotate body as smoothly as possible but prevent 360 deg head twists on quick rotations
TurnBodyToCamera(turnWeightSharp * Mathf.InverseLerp(turnAngleThreshold, 170f, angle));

if (!playerController.inSpecialInteractAnimation)
lastFrameHMDPosition = mainCamera.transform.localPosition;
Expand Down Expand Up @@ -632,6 +642,17 @@ private void SwitchToGameCamera()
mainCamera.enabled = true;
}

private void TurnBodyToCamera(float turnWeight)
{
var newRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, mainCamera.transform.eulerAngles.y, transform.rotation.eulerAngles.z);
transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, Time.deltaTime * turnWeight);
}

private float GetBodyToCameraAngle()
{
return Quaternion.Angle(Quaternion.Euler(0, transform.eulerAngles.y, 0), Quaternion.Euler(0, mainCamera.transform.eulerAngles.y, 0));
}

private Transform Find(string name, bool resetLocalPosition = false)
{
var transform = base.transform.Find(name);
Expand Down

0 comments on commit d282237

Please sign in to comment.