Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix camera detaching from body #23

Merged
merged 1 commit into from
Jan 8, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 36 additions & 21 deletions NomaiVR/Modules/PlayerBodyPosition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,26 @@ void Start() {
NomaiVR.Helper.Events.OnEvent += OnWakeUp;
}

private void OnWakeUp(MonoBehaviour behaviour, Events ev) {
_isAwake = true;

_playerBody = GameObject.Find("Player_Body").GetComponent<Rigidbody>();
_playerHead = FindObjectOfType<ToolModeUI>().transform;

MoveCameraToPlayerHead();

// Move helmet forward so it is easier to look at the HUD in VR
FindObjectOfType<HUDHelmetAnimator>().transform.localPosition += Vector3.forward * 0.3f;
}

void OnDisable() {
SceneManager.sceneLoaded -= OnSceneLoaded;
}

void OnSceneLoaded(Scene scene, LoadSceneMode mode) {
updateMainCamera();
}

private void updateMainCamera() {
_mainCamera = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
NomaiVR.Helper.Console.WriteLine("Main camera: " + _mainCamera.name);
Expand All @@ -41,38 +61,33 @@ private void updateMainCamera() {
}
}

private void OnWakeUp(MonoBehaviour behaviour, Events ev) {
_isAwake = true;

_playerBody = GameObject.Find("Player_Body").GetComponent<Rigidbody>();
_playerHead = FindObjectOfType<ToolModeUI>().transform;

// Set initial camera position to player head.
void MoveCameraToPlayerHead(bool ignoreVerticalAxis = false) {
Vector3 movement = _playerHead.position - _mainCamera.transform.position;
_cameraParent.transform.position += movement;

// Move helmet forward so it is easier to look at the HUD in VR
FindObjectOfType<HUDHelmetAnimator>().transform.localPosition += Vector3.forward * 0.3f;
}
float localY = _cameraParent.transform.localPosition.y;
Vector3 cameraMovement = _cameraParent.transform.InverseTransformVector(movement);

void OnDisable() {
SceneManager.sceneLoaded -= OnSceneLoaded;
}
if (ignoreVerticalAxis) {
cameraMovement.y = 0;
}

void OnSceneLoaded(Scene scene, LoadSceneMode mode) {
updateMainCamera();
_cameraParent.transform.localPosition += cameraMovement;
}

void MovePlayerBodyToCamera() {
// Move player to camera position.
Vector3 movement = _prevCameraPosition - (_playerHead.position - _mainCamera.transform.position);
_playerBody.transform.localPosition += movement;

// Since camera is a child of player body, we need to adjust it now.
Vector3 cameraMovement = _cameraParent.transform.InverseTransformVector(movement);
cameraMovement.y = 0;
_cameraParent.transform.localPosition -= cameraMovement;
// Since camera is a child of player body, it also moves when we move the camera.
// So we need to move the camera to the player's head again.
MoveCameraToPlayerHead(true);
//Vector3 cameraMovement = _cameraParent.transform.InverseTransformVector(movement);
//cameraMovement.y = 0;
//_cameraParent.transform.localPosition -= cameraMovement;

//Vector3 cameraMovement = _playerHead.position - _mainCamera.transform.position;
//_cameraParent.transform.position += cameraMovement;

_prevCameraPosition = _playerHead.position - _mainCamera.transform.position;
}

Expand Down