Skip to content

Commit

Permalink
just simplify vertical offset assigning part
Browse files Browse the repository at this point in the history
  • Loading branch information
Ibodan committed Aug 27, 2018
1 parent 7682444 commit 72bbfcf
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions CustomAvatar/PlayerAvatarManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public class PlayerAvatarManager
private SpawnedAvatar _currentSpawnedPlayerAvatar;
private float _prevPlayerHeight = MainSettingsModel.kDefaultPlayerHeight;
private Vector3 _startAvatarLocalScale = Vector3.one;
private float _currentAvatarOffsetY = 0f;
private float? _currentAvatarArmLength = null;
private float _currentPlatformOffsetY = 0f;
private Vector3? _startPlatformPosition = null;
private float? _startAvatarPositionY = null;

public event Action<CustomAvatar> AvatarChanged;

Expand Down Expand Up @@ -132,7 +132,7 @@ private void CustomAvatarLoaded(CustomAvatar loadedAvatar, AvatarLoadResult resu
}

_startAvatarLocalScale = _currentSpawnedPlayerAvatar.GameObject.transform.localScale;
_currentAvatarOffsetY = 0f;
_startAvatarPositionY = null;
_currentAvatarArmLength = null;
_prevPlayerHeight = -1;
FixAvatar();
Expand All @@ -156,7 +156,7 @@ private void SceneManagerOnSceneLoaded(Scene newScene, LoadSceneMode mode)

private const string PlayerArmLengthKey = "AvatarAutoFitting.PlayerArmLength";
private const string PlayerViewPointYKey = "AvatarAutoFitting.PlayerViewPointY";
private float PlayerDefaultViewPortY = BeatSaberUtil.GetPlayerHeight() - 0.11f;
private float PlayerDefaultViewPointY = BeatSaberUtil.GetPlayerHeight() - 0.11f;
private float PlayerDefaultArmLength = BeatSaberUtil.GetPlayerHeight() * 0.92f;

private void ResizePlayerAvatar()
Expand All @@ -165,7 +165,7 @@ private void ResizePlayerAvatar()
if (!_currentSpawnedPlayerAvatar.CustomAvatar.AllowHeightCalibration) return;

float playerArmLength = PlayerPrefs.GetFloat(PlayerArmLengthKey, PlayerDefaultArmLength);
float playerViewPointY = PlayerPrefs.GetFloat(PlayerViewPointYKey, PlayerDefaultViewPortY);
float playerViewPointY = PlayerPrefs.GetFloat(PlayerViewPointYKey, PlayerDefaultViewPointY);

_currentAvatarArmLength = _currentAvatarArmLength ?? AvatarMeasurement.MeasureArmLength(_currentSpawnedPlayerAvatar.GameObject);
var avatarArmLength = _currentAvatarArmLength ?? playerArmLength;
Expand All @@ -182,17 +182,19 @@ private void ResizePlayerAvatar()
_currentSpawnedPlayerAvatar.GameObject.transform.localScale = _startAvatarLocalScale * scale;

// translate root for floor level
_startAvatarPositionY = _startAvatarPositionY ?? animator.transform.position.y;
const float FloorLevelOffset = 0.04f; // a heuristic value from testing on oculus rift
var offset = (playerViewPointY - (avatarViewPointY * scale)) + FloorLevelOffset;
var avatarTranslate = Vector3.up * (offset - _currentAvatarOffsetY);
_currentAvatarOffsetY = offset;

animator.transform.Translate(avatarTranslate);
animator.transform.position = new Vector3(animator.transform.position.x, offset + _startAvatarPositionY ?? 0, animator.transform.position.z);

// translate platform
var platformTranslate = Vector3.up * (offset - _currentPlatformOffsetY);
GameObject.Find("Platform Loader")?.transform.Translate(platformTranslate);
_currentPlatformOffsetY = offset;
var customFloor = GameObject.Find("Platform Loader");
if (customFloor != null)
{
_startPlatformPosition = _startPlatformPosition ?? customFloor.transform.position;
customFloor.transform.position = (Vector3.up * offset) + _startPlatformPosition ?? Vector3.zero;
Plugin.Log("Custom Platform moved to: " + customFloor.transform.position.y);
}

Plugin.Log("Avatar fitted with scale: " + scale + " yoffset: " + offset);
}
Expand Down

0 comments on commit 72bbfcf

Please sign in to comment.