From 11b2aa6e45265e37c83d2cd21a249749981de0b2 Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Sun, 29 Nov 2026 03:29:39 +0000 Subject: [PATCH 01/20] fix: the VR hero body knows how fast its own walk, run and sneak steps go - read from Gothic's animations at the body's scale, the speed potion's run loop included Co-Authored-By: Claude Opus 5.5 --- .../Scripts/Adapters/Player/VRHeroBody.cs | 4 + .../Adapters/Player/VRHeroBodyAnimator.cs | 10 +++ .../Adapters/Player/VrHeroLocomotion.cs | 83 +++++++++++++++++-- .../Gothic-VR/Scripts/Models/VrHeroSpeeds.cs | 55 ++++++++++++ .../Scripts/Services/VRPlayerService.cs | 12 +++ 5 files changed, 157 insertions(+), 7 deletions(-) create mode 100644 Assets/Gothic-VR/Scripts/Models/VrHeroSpeeds.cs diff --git a/Assets/Gothic-VR/Scripts/Adapters/Player/VRHeroBody.cs b/Assets/Gothic-VR/Scripts/Adapters/Player/VRHeroBody.cs index b713ff5bf..dab6931ab 100644 --- a/Assets/Gothic-VR/Scripts/Adapters/Player/VRHeroBody.cs +++ b/Assets/Gothic-VR/Scripts/Adapters/Player/VRHeroBody.cs @@ -145,6 +145,7 @@ private void OnDestroy() _hands.RestoreHeldItemAttachPoints(); _meshCutter.DestroyCreatedMeshes(); _hands.Dispose(); + _locomotion.ForgetSpeeds(); if (_rig != null) _rig.CrouchHeightOverride = null; } @@ -154,6 +155,8 @@ private void OnDisable() SetHeroHandsShown(false); _downPose.SetEnabled(false); _hands.RestoreHeldItemAttachPoints(); + // The rig moves at its own speeds without the body. + _locomotion.ForgetSpeeds(); } private void OnNpcVisualBodyChanged(NpcContainer npc) @@ -399,6 +402,7 @@ private void EnsureBody(NpcContainer hero) _body = null; _builtVisual = visualKey; // don't retry (only on the next visual change) _downPose.Bind(null, null, null, null, null); + _locomotion.ForgetSpeeds(); return; } diff --git a/Assets/Gothic-VR/Scripts/Adapters/Player/VRHeroBodyAnimator.cs b/Assets/Gothic-VR/Scripts/Adapters/Player/VRHeroBodyAnimator.cs index 673cc4e89..4db26fbe8 100644 --- a/Assets/Gothic-VR/Scripts/Adapters/Player/VRHeroBodyAnimator.cs +++ b/Assets/Gothic-VR/Scripts/Adapters/Player/VRHeroBodyAnimator.cs @@ -143,6 +143,16 @@ public float GetMovementSpeed(string animationName) return track != null && track.IsMoving ? track.MovementSpeed.magnitude : 0f; } + /// + /// The animation comes from the timed overlay (OverlayOverride, e.g. the speed potion's s_RunL), not from the + /// hero's own MDS. + /// + public bool IsFromOverlayOverride(string animationName) + { + return OverlayOverride != null && animationName != null && + _animationService.GetTrack(animationName, null, OverlayOverride) != null; + } + public void Update(float deltaTime) { if (!IsValid || _layers.Count == 0) diff --git a/Assets/Gothic-VR/Scripts/Adapters/Player/VrHeroLocomotion.cs b/Assets/Gothic-VR/Scripts/Adapters/Player/VrHeroLocomotion.cs index 88ec2684c..493dcc1f1 100644 --- a/Assets/Gothic-VR/Scripts/Adapters/Player/VrHeroLocomotion.cs +++ b/Assets/Gothic-VR/Scripts/Adapters/Player/VrHeroLocomotion.cs @@ -1,9 +1,13 @@ using System.Collections.Generic; using Gothic.Core.Models.Container; +using Gothic.Core.Logging; using Gothic.Core.Services.Npc; using Gothic.VR.Interaction.Ports; +using Gothic.VR.Models; +using Gothic.VR.Services; using Reflex.Attributes; using UnityEngine; +using Logger = Gothic.Core.Logging.Logger; namespace Gothic.VR.Adapters.Player { @@ -12,18 +16,21 @@ namespace Gothic.VR.Adapters.Player /// player's velocity, the body state, the body's turn speed, crouching and swimming (VrHeroSwimPose) - idle, /// walk/run (played at the real speed - no sliding feet), walking backwards, strafing, turning on the spot, /// falling, sneaking, swimming and diving. Worker of VRHeroBody (new().Inject(), Bind per built body). + /// Publishes the body's walk/run/sneak speeds (its animations at its scale, VRPlayerService.HeroBodySpeeds) - the + /// rig moves at them (VRHeroMovement), so the feet match the ground. /// public class VrHeroLocomotion { [Inject] private readonly AnimationService _animationService; + [Inject] private readonly VRPlayerService _vrPlayerService; private const float _idleSpeed = 0.15f; - private const float _runSpeed = 1.6f; private const float _turnAnimationSpeed = 60f; // degrees per second private const float _backwardsShare = 0.5f; private const string _idleAnimation = "S_RUN"; private const string _walkAnimation = "S_WALKL"; private const string _runAnimation = "S_RUNL"; + private const string _sneakAnimation = "S_SNEAKL"; private const string _walkBackAnimation = "S_WALKBL"; private const string _fallAnimation = "S_FALLDN"; @@ -38,6 +45,13 @@ public class VrHeroLocomotion // every frame while it's missing (e.g. no S_SNEAKBL). Per built body (Bind). private readonly Dictionary<(string, string), string> _missingAnimationFallbacks = new(); + // The speeds were last read for this overlay and scale (-1: read again). The scale follows the head height + // every frame - only a change by more than the tolerance reads them again. + private const float _speedsScaleTolerance = 0.01f; + private string _speedsOverlay; + private float _speedsScale = -1f; + private bool _isMissingSpeedLogged; + /// /// The animator of a newly built body. /// @@ -45,6 +59,17 @@ public void Bind(VRHeroBodyAnimator animator) { _animator = animator; _missingAnimationFallbacks.Clear(); + _isMissingSpeedLogged = false; + ForgetSpeeds(); + } + + /// + /// No (animated) body anymore: the rig moves at its own speeds until the next Update reads them again. + /// + public void ForgetSpeeds() + { + _speedsScale = -1f; + _vrPlayerService.HeroBodySpeeds = null; } /// @@ -57,6 +82,7 @@ public void Update(NpcContainer hero, Transform body, IVrRig rig, float scale, b return; // The speed potion's overlay (VRHeroMovement): the body runs faster too. _animator.OverlayOverride = VRHeroMovement.SpeedOverlay; + UpdateSpeeds(scale); var yaw = body.eulerAngles.y; if (Time.deltaTime > 0f) @@ -75,7 +101,8 @@ public void Update(NpcContainer hero, Transform body, IVrRig rig, float scale, b var animation = _swimPose.GetAnimation(_animator, bodyState, speed, isMoving, isBackwards, isTurning, _bodyYawSpeed < 0f) - ?? GetWalkAnimation(rig, velocity, speed, local, isMoving, isBackwards, isTurning, isCrouching); + ?? GetWalkAnimation(rig, velocity, speed, local, isMoving, isBackwards, isTurning, isCrouching, + _vrPlayerService.HeroSpeeds.RunThreshold); if (!_animator.HasAnimation(animation)) { @@ -83,7 +110,8 @@ public void Update(NpcContainer hero, Transform body, IVrRig rig, float scale, b animation ??= speed < _idleSpeed ? _idleAnimation : _walkAnimation; } - // Walk/run loops at the real speed, so the feet don't slide. + // Walk/run loops at the real speed, so the feet don't slide. At full stick the rig moves at the loop's own + // speed (UpdateSpeeds) - about 1, the factor is for a half pushed stick and speeding up. var animationSpeed = _animator.GetMovementSpeed(animation) * scale; var playbackSpeed = animationSpeed > 0.1f && speed >= _idleSpeed ? Mathf.Clamp(speed / animationSpeed, 0.5f, 2f) @@ -94,6 +122,46 @@ public void Update(NpcContainer hero, Transform body, IVrRig rig, float scale, b _animator.Update(Time.deltaTime); } + /// + /// The walk, run and sneak loops' own speeds (root motion, the speed potion's overlay first) times the body's + /// scale - read again when the overlay or the scale changes. Without a walk or run loop (a mod's MDS) the rig + /// keeps its own speeds; without a sneak loop the body walks crouched, at walking speed. + /// + private void UpdateSpeeds(float scale) + { + var overlay = _animator.MdsOverlay; + var isNewOverlay = !ReferenceEquals(overlay, _speedsOverlay) || _speedsScale < 0f; + if (!isNewOverlay && Mathf.Abs(scale - _speedsScale) <= _speedsScaleTolerance) + return; + _speedsOverlay = overlay; + _speedsScale = scale; + + var walk = _animator.GetMovementSpeed(_walkAnimation); + var run = _animator.GetMovementSpeed(_runAnimation); + if (walk <= 0f || run <= 0f) + { + _vrPlayerService.HeroBodySpeeds = null; + if (!_isMissingSpeedLogged) + { + _isMissingSpeedLogged = true; + Logger.LogWarning($"[VRHeroBody] No moving {_walkAnimation} ({walk:F2} m/s) or {_runAnimation} " + + $"({run:F2} m/s) in {_animator.MdsBase}/{overlay} - the rig keeps its own " + + "speeds.", LogCat.VR); + } + return; + } + var sneak = _animator.GetMovementSpeed(_sneakAnimation); + if (sneak <= 0f) + sneak = walk; + + var speeds = new VrHeroSpeeds(walk * scale, run * scale, sneak * scale, + _animator.IsFromOverlayOverride(_runAnimation)); + _vrPlayerService.HeroBodySpeeds = speeds; + if (isNewOverlay) + Logger.Log($"[VRHeroBody] Speeds of {_animator.MdsBase}/{overlay} at scale {scale:F2}: {speeds}" + + (speeds.IsSpeedOverlayRun ? " (the overlay's own run)" : ""), LogCat.VR); + } + /// /// e.g. no sneaking backwards: the forward loop played back to front (..BL -> ..L reversed), null without one. /// Remembered per animation and overlay - no string built every frame. @@ -118,10 +186,11 @@ private string GetMissingAnimationFallback(string animation) } /// - /// On land: falling, sneaking, idle/turning on the spot, walking backwards, strafing, walking/running. + /// On land: falling, sneaking, idle/turning on the spot, walking backwards, strafing, walking/running (from the + /// same speed on as BS_RUN, VrHeroSpeeds.RunThreshold). /// private string GetWalkAnimation(IVrRig rig, Vector3 velocity, float speed, Vector3 local, bool isMoving, - bool isBackwards, bool isTurning, bool isCrouching) + bool isBackwards, bool isTurning, bool isCrouching, float runThreshold) { if (rig != null && !rig.IsGrounded && velocity.y < -1f && !_swimPose.IsInWater && _animator.HasAnimation(_fallAnimation)) @@ -135,10 +204,10 @@ private string GetWalkAnimation(IVrRig rig, Vector3 velocity, float speed, Vecto ? _walkBackAnimation : GetReversedAnimationName(_walkAnimation); if (Mathf.Abs(local.x) > Mathf.Abs(local.z)) - return speed >= _runSpeed + return speed >= runThreshold ? (local.x < 0f ? "T_RUNSTRAFEL" : "T_RUNSTRAFER") : (local.x < 0f ? "T_WALKSTRAFEL" : "T_WALKSTRAFER"); - return speed >= _runSpeed ? _runAnimation : _walkAnimation; + return speed >= runThreshold ? _runAnimation : _walkAnimation; } /// diff --git a/Assets/Gothic-VR/Scripts/Models/VrHeroSpeeds.cs b/Assets/Gothic-VR/Scripts/Models/VrHeroSpeeds.cs new file mode 100644 index 000000000..d2cfaa60a --- /dev/null +++ b/Assets/Gothic-VR/Scripts/Models/VrHeroSpeeds.cs @@ -0,0 +1,55 @@ +namespace Gothic.VR.Models +{ + /// + /// The VR hero's speeds on land in m/s: Gothic's walk, run and sneak loops (S_WALKL, S_RUNL, S_SNEAKL) at the VR + /// hero body's scale, so the body's feet match the ground - or the rig's own speeds without an animated body. + /// + public readonly struct VrHeroSpeeds + { + /// + /// Gothic's speed potion (HUMANS_SPRINT.MDS) without a run loop of its own: half again as fast. + /// + public const float SpeedPotionFactor = 1.5f; + + /// + /// Before VRHeroMovement's first frame: the XRI rig's default speeds. + /// + public static readonly VrHeroSpeeds Fallback = new(1.5f, 3.5f, 1.5f); + + public readonly float Walk; + public readonly float Run; + public readonly float Sneak; + + /// + /// Run is the speed potion overlay's own run loop (HUMANS_SPRINT's s_RunL) - the potion needs no factor then. + /// + public readonly bool IsSpeedOverlayRun; + + public VrHeroSpeeds(float walk, float run, float sneak, bool isSpeedOverlayRun = false) + { + Walk = walk; + Run = run; + Sneak = sneak; + IsSpeedOverlayRun = isSpeedOverlayRun; + } + + /// + /// From this speed on the hero runs - BS_RUN for the scripts and the body's run animation: halfway between + /// walking and running, so the stick walks and the sprint runs. + /// + public float RunThreshold => (Walk + Run) * 0.5f; + + /// + /// The speed potion without a run loop of its own: walking and running half again as fast, sneaking not. + /// + public VrHeroSpeeds WithSpeedPotion() + { + return new VrHeroSpeeds(Walk * SpeedPotionFactor, Run * SpeedPotionFactor, Sneak, IsSpeedOverlayRun); + } + + public override string ToString() + { + return $"walk {Walk:F2}, run {Run:F2}, sneak {Sneak:F2} m/s"; + } + } +} diff --git a/Assets/Gothic-VR/Scripts/Services/VRPlayerService.cs b/Assets/Gothic-VR/Scripts/Services/VRPlayerService.cs index 1b4e0b2d1..69821de30 100644 --- a/Assets/Gothic-VR/Scripts/Services/VRPlayerService.cs +++ b/Assets/Gothic-VR/Scripts/Services/VRPlayerService.cs @@ -360,6 +360,18 @@ private void OnPlayerPrefUpdated(string key, object value) /// public bool IsHeroBodyDown { get; set; } + /// + /// The VR hero body's walk, run and sneak speeds from its animations at its scale (VrHeroLocomotion), null + /// without an animated body. VRHeroMovement makes the rig's speeds of them. + /// + public VrHeroSpeeds? HeroBodySpeeds { get; set; } + + /// + /// The speeds the hero moves with on land (VRHeroMovement: the body's, else the rig's own, with the speed + /// potion). Its RunThreshold is BS_RUN for the scripts and the body's run animation. + /// + public VrHeroSpeeds HeroSpeeds { get; set; } = VrHeroSpeeds.Fallback; + public GameObject GrabbedItemLeft; public GameObject GrabbedItemRight; From 988beb1668df36f91a9c370380e10bad6dcabbfb Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Sun, 29 Nov 2026 03:29:47 +0000 Subject: [PATCH 02/20] fix: the VR hero's feet match the ground - the stick walks and sprint runs at Gothic's walk/run loop speeds times the body's scale, crouched at sneak speed, and BS_RUN starts where the run animation does Co-Authored-By: Claude Opus 5.5 --- .../Scripts/Adapters/Player/VRHeroMovement.cs | 120 ++++++++---------- .../Scripts/Adapters/Player/VRSwimDive.cs | 6 + 2 files changed, 61 insertions(+), 65 deletions(-) diff --git a/Assets/Gothic-VR/Scripts/Adapters/Player/VRHeroMovement.cs b/Assets/Gothic-VR/Scripts/Adapters/Player/VRHeroMovement.cs index 4672d4360..d1ba4e0b1 100644 --- a/Assets/Gothic-VR/Scripts/Adapters/Player/VRHeroMovement.cs +++ b/Assets/Gothic-VR/Scripts/Adapters/Player/VRHeroMovement.cs @@ -6,6 +6,7 @@ using Gothic.Core.Services; using Gothic.Core.Services.Audio; using Gothic.VR.Interaction.Ports; +using Gothic.VR.Models; using Gothic.VR.Services; using Reflex.Attributes; using UnityEngine; @@ -16,8 +17,9 @@ namespace Gothic.VR.Adapters.Player { /// /// What the engine derives from the hero's movement, for any VR backend (IVrRig): BS_WALK/BS_RUN/BS_SNEAK, the - /// hero's own footsteps, the speed potion's faster movement and the slower sprint while sneaking. - /// Sits next to VRPlayer (added by VRContextInteractionService). + /// hero's own footsteps and the rig's speeds on land - Gothic's walk/run/sneak loops at the VR hero body's scale + /// (VRPlayerService.HeroBodySpeeds), with the speed potion. Sits next to VRPlayer (added by + /// VRContextInteractionService). /// public class VRHeroMovement : MonoBehaviour { @@ -25,6 +27,7 @@ public class VRHeroMovement : MonoBehaviour [Inject] private readonly AudioService _audioService; [Inject] private readonly GroundSoundService _groundSoundService; [Inject] private readonly VRPlayerService _vrPlayerService; + [Inject] private readonly VRSpellTransformService _vrSpellTransformService; // Gothic.ini footstepSounds: the hero's own steps - Gothic's RUN_ every stride of the way // walked (the VR body plays no step events; a timer stepped on while the run state lingered after a tap). @@ -39,32 +42,26 @@ public class VRHeroMovement : MonoBehaviour // Why the last step was silent (logged once per change). private string _silentStepReason; - // Speed potions (Mdl_ApplyOverlayMDSTimed HUMANS_SPRINT.MDS): faster VR movement for the potion's time. + // Speed potions (Mdl_ApplyOverlayMDSTimed HUMANS_SPRINT.MDS): the overlay's run loop for the potion's time. private const string _sprintOverlayName = "SPRINT"; - private const float _sprintSpeedFactor = 1.5f; private bool _isSpeedBoosted; private string _speedOverlay; private float _speedBoostEndTime; - // Horizontal speed (m/s) for BS_WALK/BS_RUN. Moving in VR equals the hero's default run in Gothic. + // Horizontal speed (m/s) for BS_WALK; BS_RUN from VrHeroSpeeds.RunThreshold on (the stick walks, sprint runs). private const float _bodyStateWalkSpeed = 0.2f; - private const float _bodyStateRunSpeed = 1f; private const float _stickyRunSeconds = 1.5f; private const float _teleportJumpDistance = 1.5f; private float _lastRunTime = float.MinValue; private Vector3 _lastBodyStatePosition; - // Like vanilla Gothic: no sprint while sneaking. The sprint input then moves at the plain move speed. - private bool _isSneaking; - - // The speeds without the potion and the sneak rule, and what we set last - one base, the factor on top (the - // two undoing each other's values left the run speed wrong for good). Changed by someone else meanwhile - // (swimming, a transformation): that's the new base. - private bool _isSpeedModified; - private float _baseMoveSpeed; - private float _baseRunSpeed; - private float _appliedMoveSpeed; - private float _appliedRunSpeed; + // The rig's own speeds (its prefab values) - without an animated VR hero body. + private VrHeroSpeeds _rigSpeeds = VrHeroSpeeds.Fallback; + // Water (VRSwimDive) or a transformation (VRSpellTransformPuppet) sets the rig's speeds meanwhile. + private VRSwimDive _swimDive; + private bool _isSpeedSetElsewhere; + // Which speeds the rig got last (logged once per change): the body's (true), the rig's own (false). + private bool? _isBodySpeedLogged; // NPC_TALENT_SNEAK (G1/G2: 8) - read from the scripts. private const string _sneakTalentSymbol = "NPC_TALENT_SNEAK"; @@ -84,9 +81,12 @@ private void Awake() { this.Inject(); _rig = GetComponentInParent(); + _swimDive = GetComponent(); if (_rig == null) Logger.LogWarning("[VRHeroMovement] No VR rig above - no body states, steps or speed changes.", LogCat.VR); + else + _rigSpeeds = new VrHeroSpeeds(_rig.MoveSpeed, _rig.RunSpeed, _rig.MoveSpeed); GlobalEventDispatcher.ScriptHeroOverlayTimed.AddListener(OnHeroOverlayTimed); } @@ -101,59 +101,53 @@ private void Update() if (_rig == null) return; + UpdateSpeeds(); UpdateHeroBodyState(); - UpdateSneakSpeed(); UpdateHeroFootsteps(); UpdateSpeedBoost(); } - private bool IsHeroSneaking() - { - return _gameStateService.GothicVm?.GlobalHero is NpcInstance heroInstance && - heroInstance.GetUserData()?.Props?.BodyState == VmGothicEnums.BodyState.BsSneak; - } - - /// - /// No sprint while sneaking (BS_SNEAK), like vanilla Gothic: the sprint input does nothing, the hero moves at - /// sneak (move) speed. The body state stays BS_SNEAK (UpdateHeroBodyState), so it is no run for the footsteps - /// and the scripts. - /// - private void UpdateSneakSpeed() - { - _isSneaking = IsHeroSneaking(); - ApplySpeeds(); - } - /// - /// The base speeds times the potion (move and run); sneaking caps the run speed at the move speed. + /// The rig's speeds on land: the stick walks and sprint runs at the VR hero body's walk/run loops (its feet + /// match the ground), the rig's own speeds without an animated body. The speed potion runs with its overlay's + /// run loop, half again as fast without one. Crouched is sneaking speed for both, like vanilla Gothic's "no + /// sprint while sneaking" - also without the sneak talent: the body sneaks then, only the scripts hear a walk. + /// Water (VRSwimDive) and a transformation (VRSpellTransformPuppet) set their own speeds and restore ours. /// - private void ApplySpeeds() + private void UpdateSpeeds() { - var isModified = _isSpeedBoosted || _isSneaking; - if (!_isSpeedModified && !isModified) - return; - - if (!_isSpeedModified) + var bodySpeeds = _vrPlayerService.HeroBodySpeeds; + var speeds = bodySpeeds ?? _rigSpeeds; + if (_isSpeedBoosted && !speeds.IsSpeedOverlayRun) + speeds = speeds.WithSpeedPotion(); + _vrPlayerService.HeroSpeeds = speeds; + if (_isBodySpeedLogged != bodySpeeds.HasValue) { - _baseMoveSpeed = _rig.MoveSpeed; - _baseRunSpeed = _rig.RunSpeed; + _isBodySpeedLogged = bodySpeeds.HasValue; + Logger.Log(bodySpeeds.HasValue + ? "[VRHeroMovement] Moving at the VR hero body's animation speeds." + : $"[VRHeroMovement] No animated VR hero body - the rig's own speeds ({_rigSpeeds}).", LogCat.VR); } - else if (!Mathf.Approximately(_rig.MoveSpeed, _appliedMoveSpeed) || - !Mathf.Approximately(_rig.RunSpeed, _appliedRunSpeed)) + + var isSpeedSetElsewhere = _vrSpellTransformService.IsTransformed || + (_swimDive != null && _swimDive.IsInWater); + if (isSpeedSetElsewhere != _isSpeedSetElsewhere) { - // Someone else set speeds while ours were on: they are the new base, our factors go on top. VRSwimDive - // sets its plain speeds (the ones it found at the start, half in water) - divided by our factors, a - // potion through knee deep water left the hero a third slower, a sneak twice as fast. - _baseMoveSpeed = _rig.MoveSpeed; - _baseRunSpeed = _rig.RunSpeed; + _isSpeedSetElsewhere = isSpeedSetElsewhere; + Logger.Log(isSpeedSetElsewhere + ? "[VRHeroMovement] Water or a transformation sets the speeds." + : $"[VRHeroMovement] Land speeds again ({speeds}).", LogCat.VR); } + if (isSpeedSetElsewhere) + return; - var boost = _isSpeedBoosted ? _sprintSpeedFactor : 1f; - _appliedMoveSpeed = _baseMoveSpeed * boost; - _appliedRunSpeed = _isSneaking ? _appliedMoveSpeed : _baseRunSpeed * boost; - _rig.MoveSpeed = _appliedMoveSpeed; - _rig.RunSpeed = _appliedRunSpeed; - _isSpeedModified = isModified; + var isSneaking = _rig.IsCrouching && !_vrPlayerService.IsHeroBodyDown; + var moveSpeed = isSneaking ? speeds.Sneak : speeds.Walk; + var runSpeed = isSneaking ? speeds.Sneak : speeds.Run; + if (!Mathf.Approximately(_rig.MoveSpeed, moveSpeed)) + _rig.MoveSpeed = moveSpeed; + if (!Mathf.Approximately(_rig.RunSpeed, runSpeed)) + _rig.RunSpeed = runSpeed; } private void OnHeroOverlayTimed(string overlayName, float seconds) @@ -164,7 +158,6 @@ private void OnHeroOverlayTimed(string overlayName, float seconds) _speedOverlay = overlayName; _isSpeedBoosted = true; - ApplySpeeds(); _speedBoostEndTime = Time.time + seconds; SpeedOverlay = _speedOverlay; } @@ -176,7 +169,6 @@ private void UpdateSpeedBoost() _isSpeedBoosted = false; SpeedOverlay = null; - ApplySpeeds(); } /// @@ -216,13 +208,11 @@ or VmGothicEnums.BodyState.BsWalk or VmGothicEnums.BodyState.BsRun // The rig's own movement this frame, not CharacterController.velocity: that is only the last Move() of the // frame, and the XRI rig's ground snap (XriRig.SnapToGround) moves straight down after the walk - the // hero stayed BS_STAND while walking, so NPCs never heard him behind their backs. + // Running from the same speed on as the VR hero body's run animation (VrHeroLocomotion). var speed = isFirstCheck || Time.deltaTime <= 0f ? 0f : horizontalJump / Time.deltaTime; - var bodyState = speed switch - { - >= _bodyStateRunSpeed => VmGothicEnums.BodyState.BsRun, - >= _bodyStateWalkSpeed => VmGothicEnums.BodyState.BsWalk, - _ => VmGothicEnums.BodyState.BsStand - }; + var bodyState = speed >= _vrPlayerService.HeroSpeeds.RunThreshold ? VmGothicEnums.BodyState.BsRun + : speed >= _bodyStateWalkSpeed ? VmGothicEnums.BodyState.BsWalk + : VmGothicEnums.BodyState.BsStand; // Running is a snapshot of the speed - in VR you slow down in every // turn, and a teleport has no speed at all. ZS_Attack_Loop checks BS_RUN every ~2 s and resets its pursuit diff --git a/Assets/Gothic-VR/Scripts/Adapters/Player/VRSwimDive.cs b/Assets/Gothic-VR/Scripts/Adapters/Player/VRSwimDive.cs index 929752dbe..5435c446e 100644 --- a/Assets/Gothic-VR/Scripts/Adapters/Player/VRSwimDive.cs +++ b/Assets/Gothic-VR/Scripts/Adapters/Player/VRSwimDive.cs @@ -49,6 +49,11 @@ public class VRSwimDive : MonoBehaviour, IMarvinPropertyCollector private float _initialRunSpeed; private float _initialFallSpeed; + /// + /// Wading, swimming or diving: the water sets the rig's speeds, VRHeroMovement leaves them alone meanwhile. + /// + public bool IsInWater { get; private set; } + private VmGothicEnums.WalkMode _mode = VmGothicEnums.WalkMode.Walk; // Swim + Dive properties @@ -276,6 +281,7 @@ private void ChangeWaterBehavior() _waterBobbingCoroutine = null; } + IsInWater = _playerAi.WaterLevel != (int)ZenGineConst.WaterLevel.Normal; switch ((ZenGineConst.WaterLevel)_playerAi.WaterLevel) { case ZenGineConst.WaterLevel.Normal: From d89c8a9626616a1024be172365dad4270c6f07e8 Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Sun, 29 Nov 2026 03:30:12 +0000 Subject: [PATCH 03/20] docs: ADR - the VR hero moves at the speed of his own walk, run and sneak animations Co-Authored-By: Claude Opus 5.5 --- .../2026-11-29-hero-speed-from-animations.md | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Docs/adr/2026-11-29-hero-speed-from-animations.md diff --git a/Docs/adr/2026-11-29-hero-speed-from-animations.md b/Docs/adr/2026-11-29-hero-speed-from-animations.md new file mode 100644 index 000000000..095d25f37 --- /dev/null +++ b/Docs/adr/2026-11-29-hero-speed-from-animations.md @@ -0,0 +1,29 @@ +# ADR: The VR hero moves at the speed of his own steps + +- Date: 2026-11-29 +- Replaces: decision 7 of [VR movement, trade and theft](2026-11-03-vr-movement-trade-theft.md) (the speed potion as a factor on one base speed) +- Technical details: VrHeroLocomotion, VRHeroMovement, VrHeroSpeeds (VR) + +## Context + +The VR hero walked at a fixed 1.5 m/s and sprinted at 3.5 m/s. His VR body plays Gothic's walk and run animations, which cover their own distance per second: in Gothic 1 the walk loop moves 1.37 m/s, the run loop 3.28 m/s and the sneak loop 0.75 m/s for a hero of normal height. The animations were sped up or slowed down to fit, so the steps looked too short or too long, and a sneaking hero moved at walking speed with sliding feet. + +The body is scaled to the player's height, so a tall player takes longer steps than a short one. + +In Gothic the speed comes from the animation itself: the hero moves as far as the animation's feet carry him. The speed potion swaps in a faster run animation for its time. + +## Decisions + +1. **The hero moves at the speed of the animation his body plays, times the body's size.** The stick (without sprint) walks with the walk loop's speed, sprint runs with the run loop's speed, crouched he sneaks with the sneak loop's speed. The speeds are read from the hero's animations while the game runs, so Gothic 2, mods and animation overlays bring their own. Not chosen: fixed numbers measured from Gothic 1 (wrong for Gothic 2, mods and the speed potion). +2. **The speed potion runs with its own run animation** (Gothic's sprint animation of the potion). Walking and sneaking stay as they are, like in Gothic, where the potion only has a run animation. A potion whose overlay has no run animation (a mod) keeps the old rule: walking and running half again as fast. Not chosen: half again as fast always (the run animation would not fit its speed). +3. **Running starts halfway between walking and running speed,** the same for the body's run animation and for what the scripts see (running for them). So the stick walks, sprint runs, and NPCs see a walking hero as walking. Running still stays on for a moment after slowing down, and a teleport still counts as running. +4. **Crouched is always sneaking speed,** also without the sneak talent: the body sneaks then, only the scripts see a walking hero, as before. Sprint does nothing while crouched, like in Gothic. +5. **Without an animated VR body** (body switched off, an armor that can't be built, a mod without walk or run animations) the hero moves at the VR rig's own speeds (1.5 and 3.5 m/s), with a warning in the log when animations are missing. + +## Consequences + +- At full stick the walk and run animations play at their normal pace; a half pushed stick still slows them down. +- A hero of normal height is a bit slower than before (1.37 instead of 1.5 m/s walking, 3.28 instead of 3.5 m/s running); a tall player is faster than a short one. +- Water and a transformation into a monster keep setting their own speeds, the land speeds come back afterwards. The speed potion no longer speeds up a transformed hero. +- Moving sideways while sprinting is faster than Gothic's side step animation (the VR rig has one speed for all directions), so the feet can still slide there. +- Nothing is saved: the speeds are read again from the animations. From 22feca2346e83890d7332b879f28d65d52ad64e9 Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Sun, 29 Nov 2026 04:07:57 +0000 Subject: [PATCH 04/20] chore: meta file of VrHeroSpeeds (generated by Unity on import) Co-Authored-By: Claude Opus 5.5 --- Assets/Gothic-VR/Scripts/Models/VrHeroSpeeds.cs.meta | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Assets/Gothic-VR/Scripts/Models/VrHeroSpeeds.cs.meta diff --git a/Assets/Gothic-VR/Scripts/Models/VrHeroSpeeds.cs.meta b/Assets/Gothic-VR/Scripts/Models/VrHeroSpeeds.cs.meta new file mode 100644 index 000000000..7ca26e61f --- /dev/null +++ b/Assets/Gothic-VR/Scripts/Models/VrHeroSpeeds.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 76ef27f1dc0f3b44283dd9da78848cd5 \ No newline at end of file From bec6969dd4ecde0f626ed2bcaeaeb3e7ef6a7c2e Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Sun, 29 Nov 2026 04:26:13 +0000 Subject: [PATCH 05/20] fix: the VR hero walks and runs 1.4 times as fast as Gothic's loops, which play as much faster - the feet still match the ground, sneaking stays Co-Authored-By: Claude Opus 5.5 --- .../Gothic-VR/Scripts/Adapters/Player/VrHeroLocomotion.cs | 3 ++- Assets/Gothic-VR/Scripts/Models/VrHeroSpeeds.cs | 6 ++++++ Docs/adr/2026-11-29-hero-speed-from-animations.md | 7 ++++--- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Assets/Gothic-VR/Scripts/Adapters/Player/VrHeroLocomotion.cs b/Assets/Gothic-VR/Scripts/Adapters/Player/VrHeroLocomotion.cs index 493dcc1f1..eb4cfa2c9 100644 --- a/Assets/Gothic-VR/Scripts/Adapters/Player/VrHeroLocomotion.cs +++ b/Assets/Gothic-VR/Scripts/Adapters/Player/VrHeroLocomotion.cs @@ -154,7 +154,8 @@ private void UpdateSpeeds(float scale) if (sneak <= 0f) sneak = walk; - var speeds = new VrHeroSpeeds(walk * scale, run * scale, sneak * scale, + var factor = scale * VrHeroSpeeds.WalkRunFactor; + var speeds = new VrHeroSpeeds(walk * factor, run * factor, sneak * scale, _animator.IsFromOverlayOverride(_runAnimation)); _vrPlayerService.HeroBodySpeeds = speeds; if (isNewOverlay) diff --git a/Assets/Gothic-VR/Scripts/Models/VrHeroSpeeds.cs b/Assets/Gothic-VR/Scripts/Models/VrHeroSpeeds.cs index d2cfaa60a..89c9b7543 100644 --- a/Assets/Gothic-VR/Scripts/Models/VrHeroSpeeds.cs +++ b/Assets/Gothic-VR/Scripts/Models/VrHeroSpeeds.cs @@ -11,6 +11,12 @@ public readonly struct VrHeroSpeeds /// public const float SpeedPotionFactor = 1.5f; + /// + /// Walk and run go this much faster than Gothic's own loops (author's choice after a playtest: at 1x the hero + /// felt slow in VR). The loops play faster by as much, so the feet still match the ground. Sneaking stays. + /// + public const float WalkRunFactor = 1.4f; + /// /// Before VRHeroMovement's first frame: the XRI rig's default speeds. /// diff --git a/Docs/adr/2026-11-29-hero-speed-from-animations.md b/Docs/adr/2026-11-29-hero-speed-from-animations.md index 095d25f37..da7068f1e 100644 --- a/Docs/adr/2026-11-29-hero-speed-from-animations.md +++ b/Docs/adr/2026-11-29-hero-speed-from-animations.md @@ -18,12 +18,13 @@ In Gothic the speed comes from the animation itself: the hero moves as far as th 2. **The speed potion runs with its own run animation** (Gothic's sprint animation of the potion). Walking and sneaking stay as they are, like in Gothic, where the potion only has a run animation. A potion whose overlay has no run animation (a mod) keeps the old rule: walking and running half again as fast. Not chosen: half again as fast always (the run animation would not fit its speed). 3. **Running starts halfway between walking and running speed,** the same for the body's run animation and for what the scripts see (running for them). So the stick walks, sprint runs, and NPCs see a walking hero as walking. Running still stays on for a moment after slowing down, and a teleport still counts as running. 4. **Crouched is always sneaking speed,** also without the sneak talent: the body sneaks then, only the scripts see a walking hero, as before. Sprint does nothing while crouched, like in Gothic. -5. **Without an animated VR body** (body switched off, an armor that can't be built, a mod without walk or run animations) the hero moves at the VR rig's own speeds (1.5 and 3.5 m/s), with a warning in the log when animations are missing. +5. **Walking and running are 1.4 times as fast as Gothic's loops, and the loops play 1.4 times as fast.** At 1x the hero felt slow in VR (playtest); the feet still match the ground because the steps speed up by as much. Sneaking keeps the loop's own speed. Not chosen: Gothic's speeds exactly (too slow in VR), faster without faster steps (sliding feet). +6. **Without an animated VR body** (body switched off, an armor that can't be built, a mod without walk or run animations) the hero moves at the VR rig's own speeds (1.5 and 3.5 m/s), with a warning in the log when animations are missing. ## Consequences -- At full stick the walk and run animations play at their normal pace; a half pushed stick still slows them down. -- A hero of normal height is a bit slower than before (1.37 instead of 1.5 m/s walking, 3.28 instead of 3.5 m/s running); a tall player is faster than a short one. +- At full stick the walk and run animations play 1.4 times as fast as in Gothic, the sneak animation at its normal pace; a half pushed stick still slows them down. +- A hero of normal height is a bit faster than before (about 1.9 instead of 1.5 m/s walking, 4.6 instead of 3.5 m/s running); a tall player is faster than a short one. - Water and a transformation into a monster keep setting their own speeds, the land speeds come back afterwards. The speed potion no longer speeds up a transformed hero. - Moving sideways while sprinting is faster than Gothic's side step animation (the VR rig has one speed for all directions), so the feet can still slide there. - Nothing is saved: the speeds are read again from the animations. From c83fb3b3e6bd4564cc7c36a9fd7e0acbe9c0245a Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Sun, 29 Nov 2026 05:20:48 +0000 Subject: [PATCH 06/20] fix: the VR hero's speeds are read again only when the body's scale changes by more than 5 % - nodding or a physical duck no longer changes them mid-stride Co-Authored-By: Claude Opus 5.5 --- .../Gothic-VR/Scripts/Adapters/Player/VrHeroLocomotion.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Assets/Gothic-VR/Scripts/Adapters/Player/VrHeroLocomotion.cs b/Assets/Gothic-VR/Scripts/Adapters/Player/VrHeroLocomotion.cs index eb4cfa2c9..1eebaa31b 100644 --- a/Assets/Gothic-VR/Scripts/Adapters/Player/VrHeroLocomotion.cs +++ b/Assets/Gothic-VR/Scripts/Adapters/Player/VrHeroLocomotion.cs @@ -46,8 +46,9 @@ public class VrHeroLocomotion private readonly Dictionary<(string, string), string> _missingAnimationFallbacks = new(); // The speeds were last read for this overlay and scale (-1: read again). The scale follows the head height - // every frame - only a change by more than the tolerance reads them again. - private const float _speedsScaleTolerance = 0.01f; + // every frame - only a change by more than this share (5 %) reads them again, so nodding or a physical duck + // doesn't change the speeds mid-stride. + private const float _speedsScaleTolerance = 0.05f; private string _speedsOverlay; private float _speedsScale = -1f; private bool _isMissingSpeedLogged; @@ -131,7 +132,7 @@ private void UpdateSpeeds(float scale) { var overlay = _animator.MdsOverlay; var isNewOverlay = !ReferenceEquals(overlay, _speedsOverlay) || _speedsScale < 0f; - if (!isNewOverlay && Mathf.Abs(scale - _speedsScale) <= _speedsScaleTolerance) + if (!isNewOverlay && Mathf.Abs(scale - _speedsScale) <= _speedsScale * _speedsScaleTolerance) return; _speedsOverlay = overlay; _speedsScale = scale; From 2fbde1a2a92de77855d903691a1b3e8851cb0d12 Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Sun, 29 Nov 2026 05:20:56 +0000 Subject: [PATCH 07/20] fix: the VR hero's speeds compare the overlay by its name, not its string instance - a rebuilt overlay string no longer re-reads and logs the speeds every frame Co-Authored-By: Claude Opus 5.5 --- Assets/Gothic-VR/Scripts/Adapters/Player/VrHeroLocomotion.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/Gothic-VR/Scripts/Adapters/Player/VrHeroLocomotion.cs b/Assets/Gothic-VR/Scripts/Adapters/Player/VrHeroLocomotion.cs index 1eebaa31b..1c3fb9c95 100644 --- a/Assets/Gothic-VR/Scripts/Adapters/Player/VrHeroLocomotion.cs +++ b/Assets/Gothic-VR/Scripts/Adapters/Player/VrHeroLocomotion.cs @@ -131,7 +131,7 @@ public void Update(NpcContainer hero, Transform body, IVrRig rig, float scale, b private void UpdateSpeeds(float scale) { var overlay = _animator.MdsOverlay; - var isNewOverlay = !ReferenceEquals(overlay, _speedsOverlay) || _speedsScale < 0f; + var isNewOverlay = !string.Equals(overlay, _speedsOverlay) || _speedsScale < 0f; if (!isNewOverlay && Mathf.Abs(scale - _speedsScale) <= _speedsScale * _speedsScaleTolerance) return; _speedsOverlay = overlay; From 0ff74aa44edbf871dedcc3dca52086ab1cc3d384 Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Sun, 29 Nov 2026 05:21:20 +0000 Subject: [PATCH 08/20] style: sort the usings of VrHeroLocomotion and say why BS_WALK starts at 0.2 m/s Co-Authored-By: Claude Opus 5.5 --- Assets/Gothic-VR/Scripts/Adapters/Player/VRHeroMovement.cs | 1 + Assets/Gothic-VR/Scripts/Adapters/Player/VrHeroLocomotion.cs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Assets/Gothic-VR/Scripts/Adapters/Player/VRHeroMovement.cs b/Assets/Gothic-VR/Scripts/Adapters/Player/VRHeroMovement.cs index d1ba4e0b1..066ca0530 100644 --- a/Assets/Gothic-VR/Scripts/Adapters/Player/VRHeroMovement.cs +++ b/Assets/Gothic-VR/Scripts/Adapters/Player/VRHeroMovement.cs @@ -49,6 +49,7 @@ public class VRHeroMovement : MonoBehaviour private float _speedBoostEndTime; // Horizontal speed (m/s) for BS_WALK; BS_RUN from VrHeroSpeeds.RunThreshold on (the stick walks, sprint runs). + // 0.2 m/s: below it he stands (drift, a nudge of the stick) - well below the sneak loop (G1: ~0.75 m/s). private const float _bodyStateWalkSpeed = 0.2f; private const float _stickyRunSeconds = 1.5f; private const float _teleportJumpDistance = 1.5f; diff --git a/Assets/Gothic-VR/Scripts/Adapters/Player/VrHeroLocomotion.cs b/Assets/Gothic-VR/Scripts/Adapters/Player/VrHeroLocomotion.cs index 1c3fb9c95..ff5f1568f 100644 --- a/Assets/Gothic-VR/Scripts/Adapters/Player/VrHeroLocomotion.cs +++ b/Assets/Gothic-VR/Scripts/Adapters/Player/VrHeroLocomotion.cs @@ -1,6 +1,6 @@ using System.Collections.Generic; -using Gothic.Core.Models.Container; using Gothic.Core.Logging; +using Gothic.Core.Models.Container; using Gothic.Core.Services.Npc; using Gothic.VR.Interaction.Ports; using Gothic.VR.Models; From 0a2cd369f0c8e834b06c8022e87e5e0cef5b0935 Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Sun, 29 Nov 2026 05:21:59 +0000 Subject: [PATCH 09/20] refactor: one rule for "the hero sneaks" (IsCrouchSneaking) for the sneak speed and BS_SNEAK, and a note that the rig's own speeds must be read before water or a transformation change them Co-Authored-By: Claude Opus 5.5 --- .../Scripts/Adapters/Player/VRHeroMovement.cs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/Assets/Gothic-VR/Scripts/Adapters/Player/VRHeroMovement.cs b/Assets/Gothic-VR/Scripts/Adapters/Player/VRHeroMovement.cs index 066ca0530..d686b34be 100644 --- a/Assets/Gothic-VR/Scripts/Adapters/Player/VRHeroMovement.cs +++ b/Assets/Gothic-VR/Scripts/Adapters/Player/VRHeroMovement.cs @@ -56,7 +56,9 @@ public class VRHeroMovement : MonoBehaviour private float _lastRunTime = float.MinValue; private Vector3 _lastBodyStatePosition; - // The rig's own speeds (its prefab values) - without an animated VR hero body. + // The rig's own speeds (its prefab values) - without an animated VR hero body. Read once in Awake: it must run + // before water (VRSwimDive) or a transformation (VRSpellTransformPuppet) change the rig's speeds, else theirs + // stay the "own" speeds for the session. Goes away once VRHeroMovement is the rig speeds' only writer. private VrHeroSpeeds _rigSpeeds = VrHeroSpeeds.Fallback; // Water (VRSwimDive) or a transformation (VRSpellTransformPuppet) sets the rig's speeds meanwhile. private VRSwimDive _swimDive; @@ -142,7 +144,7 @@ private void UpdateSpeeds() if (isSpeedSetElsewhere) return; - var isSneaking = _rig.IsCrouching && !_vrPlayerService.IsHeroBodyDown; + var isSneaking = IsCrouchSneaking(); var moveSpeed = isSneaking ? speeds.Sneak : speeds.Walk; var runSpeed = isSneaking ? speeds.Sneak : speeds.Run; if (!Mathf.Approximately(_rig.MoveSpeed, moveSpeed)) @@ -151,6 +153,15 @@ private void UpdateSpeeds() _rig.RunSpeed = runSpeed; } + /// + /// The player crouches and the hero sneaks - one rule for the sneak speed and BS_SNEAK (which also needs the + /// sneak talent). Lying or standing up (the VR hero body's fall camera lowers the head) isn't sneaking. + /// + private bool IsCrouchSneaking() + { + return _rig.IsCrouching && !_vrPlayerService.IsHeroBodyDown; + } + private void OnHeroOverlayTimed(string overlayName, float seconds) { if (_rig == null || overlayName == null || @@ -199,8 +210,7 @@ or VmGothicEnums.BodyState.BsWalk or VmGothicEnums.BodyState.BsRun // are you sneaking around" while the hero got up. // Like the engine (OpenGothic Npc::canSneak) only with the sneak // talent - without it a crouching hero just walks: guards turn to him, Lukis hears him coming. - if (_rig.IsCrouching && !_vrPlayerService.IsHeroBodyDown && - CanHeroSneak(heroInstance)) + if (IsCrouchSneaking() && CanHeroSneak(heroInstance)) { props.BodyState = VmGothicEnums.BodyState.BsSneak; return; From ffb6e3849151330b87ed62d339056ecc06b00226 Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Sun, 29 Nov 2026 05:22:51 +0000 Subject: [PATCH 10/20] refactor: the VR hero's walk/run factor and the speed potion's factor live in VRConstants.Movement with the other VR tunables, not in the VrHeroSpeeds model Co-Authored-By: Claude Opus 5.5 --- .../Scripts/Adapters/Player/VrHeroLocomotion.cs | 2 +- Assets/Gothic-VR/Scripts/Models/VrHeroSpeeds.cs | 14 ++------------ Assets/Gothic-VR/Scripts/VRConstants.cs | 14 ++++++++++++++ Docs/adr/2026-11-29-hero-speed-from-animations.md | 2 +- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/Assets/Gothic-VR/Scripts/Adapters/Player/VrHeroLocomotion.cs b/Assets/Gothic-VR/Scripts/Adapters/Player/VrHeroLocomotion.cs index ff5f1568f..f864e1bbd 100644 --- a/Assets/Gothic-VR/Scripts/Adapters/Player/VrHeroLocomotion.cs +++ b/Assets/Gothic-VR/Scripts/Adapters/Player/VrHeroLocomotion.cs @@ -155,7 +155,7 @@ private void UpdateSpeeds(float scale) if (sneak <= 0f) sneak = walk; - var factor = scale * VrHeroSpeeds.WalkRunFactor; + var factor = scale * VRConstants.Movement.WalkRunFactor; var speeds = new VrHeroSpeeds(walk * factor, run * factor, sneak * scale, _animator.IsFromOverlayOverride(_runAnimation)); _vrPlayerService.HeroBodySpeeds = speeds; diff --git a/Assets/Gothic-VR/Scripts/Models/VrHeroSpeeds.cs b/Assets/Gothic-VR/Scripts/Models/VrHeroSpeeds.cs index 89c9b7543..db7ee182d 100644 --- a/Assets/Gothic-VR/Scripts/Models/VrHeroSpeeds.cs +++ b/Assets/Gothic-VR/Scripts/Models/VrHeroSpeeds.cs @@ -6,17 +6,6 @@ namespace Gothic.VR.Models /// public readonly struct VrHeroSpeeds { - /// - /// Gothic's speed potion (HUMANS_SPRINT.MDS) without a run loop of its own: half again as fast. - /// - public const float SpeedPotionFactor = 1.5f; - - /// - /// Walk and run go this much faster than Gothic's own loops (author's choice after a playtest: at 1x the hero - /// felt slow in VR). The loops play faster by as much, so the feet still match the ground. Sneaking stays. - /// - public const float WalkRunFactor = 1.4f; - /// /// Before VRHeroMovement's first frame: the XRI rig's default speeds. /// @@ -50,7 +39,8 @@ public VrHeroSpeeds(float walk, float run, float sneak, bool isSpeedOverlayRun = /// public VrHeroSpeeds WithSpeedPotion() { - return new VrHeroSpeeds(Walk * SpeedPotionFactor, Run * SpeedPotionFactor, Sneak, IsSpeedOverlayRun); + var factor = VRConstants.Movement.SpeedPotionFactor; + return new VrHeroSpeeds(Walk * factor, Run * factor, Sneak, IsSpeedOverlayRun); } public override string ToString() diff --git a/Assets/Gothic-VR/Scripts/VRConstants.cs b/Assets/Gothic-VR/Scripts/VRConstants.cs index ce02d6b1b..fa49f54d2 100644 --- a/Assets/Gothic-VR/Scripts/VRConstants.cs +++ b/Assets/Gothic-VR/Scripts/VRConstants.cs @@ -316,6 +316,20 @@ public static class Swim public const float WaterSurfaceRayBuffer = 2f; } + /// + /// The VR hero's speeds on land (VrHeroSpeeds, read from his walk, run and sneak loops by VrHeroLocomotion). + /// + public static class Movement + { + // Walk and run go this much faster than Gothic's own loops (author's choice after a playtest: at 1x the + // hero felt slow in VR). The loops play faster by as much, so the feet still match the ground. Sneaking + // stays. + public const float WalkRunFactor = 1.4f; + + // Gothic's speed potion (HUMANS_SPRINT.MDS) without a run loop of its own: half again as fast. + public const float SpeedPotionFactor = 1.5f; + } + /// /// The VR favorites ring (VRFavoritesRing, its slot views and spells, VrFavoritesGestureDomain): sizes in /// meters, times in seconds. Self-tuned for VR. diff --git a/Docs/adr/2026-11-29-hero-speed-from-animations.md b/Docs/adr/2026-11-29-hero-speed-from-animations.md index da7068f1e..0393c6e1d 100644 --- a/Docs/adr/2026-11-29-hero-speed-from-animations.md +++ b/Docs/adr/2026-11-29-hero-speed-from-animations.md @@ -2,7 +2,7 @@ - Date: 2026-11-29 - Replaces: decision 7 of [VR movement, trade and theft](2026-11-03-vr-movement-trade-theft.md) (the speed potion as a factor on one base speed) -- Technical details: VrHeroLocomotion, VRHeroMovement, VrHeroSpeeds (VR) +- Technical details: VrHeroLocomotion, VRHeroMovement, VrHeroSpeeds, the factors in VRConstants.Movement (VR) ## Context From dcd40db17c91f236fa0ab11b7bcfa919d41926a0 Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Sun, 29 Nov 2026 15:42:55 +0000 Subject: [PATCH 11/20] refactor: the hero's land speeds are derived in one place (VRPlayerService.HeroSpeeds) - the body's or the rig's configured speeds (IVrRig.DefaultMoveSpeed/DefaultRunSpeed) with the speed potion, no hand copy of the prefab's 1.5/3.5 and no read-once in Awake Co-Authored-By: Claude Opus 5.5 --- .../PlayMode/VrPhysicsHandTest.cs | 2 ++ .../Scripts/Adapters/Player/VRHeroMovement.cs | 21 +++++----------- .../Interaction/Backends/Xri/XriRig.cs | 25 +++++++++++++------ .../Scripts/Interaction/Ports/IVrRig.cs | 11 ++++++++ .../Gothic-VR/Scripts/Models/VrHeroSpeeds.cs | 6 +---- .../Scripts/Services/VRPlayerService.cs | 24 +++++++++++++++--- 6 files changed, 59 insertions(+), 30 deletions(-) diff --git a/Assets/Gothic-Tests/PlayMode/VrPhysicsHandTest.cs b/Assets/Gothic-Tests/PlayMode/VrPhysicsHandTest.cs index b5de128c2..855d7e653 100644 --- a/Assets/Gothic-Tests/PlayMode/VrPhysicsHandTest.cs +++ b/Assets/Gothic-Tests/PlayMode/VrPhysicsHandTest.cs @@ -331,6 +331,8 @@ private class TestRig : MonoBehaviour, IVrRig public float MaxFallSpeed { get; set; } public float MoveSpeed { get; set; } public float RunSpeed { get; set; } + public float DefaultMoveSpeed => 0f; + public float DefaultRunSpeed => 0f; public float StepOffset { get; set; } public float FadeAlpha => 0f; public IReadOnlyList HandGrabbers => Array.Empty(); diff --git a/Assets/Gothic-VR/Scripts/Adapters/Player/VRHeroMovement.cs b/Assets/Gothic-VR/Scripts/Adapters/Player/VRHeroMovement.cs index d686b34be..f480723a6 100644 --- a/Assets/Gothic-VR/Scripts/Adapters/Player/VRHeroMovement.cs +++ b/Assets/Gothic-VR/Scripts/Adapters/Player/VRHeroMovement.cs @@ -44,7 +44,6 @@ public class VRHeroMovement : MonoBehaviour // Speed potions (Mdl_ApplyOverlayMDSTimed HUMANS_SPRINT.MDS): the overlay's run loop for the potion's time. private const string _sprintOverlayName = "SPRINT"; - private bool _isSpeedBoosted; private string _speedOverlay; private float _speedBoostEndTime; @@ -56,10 +55,6 @@ public class VRHeroMovement : MonoBehaviour private float _lastRunTime = float.MinValue; private Vector3 _lastBodyStatePosition; - // The rig's own speeds (its prefab values) - without an animated VR hero body. Read once in Awake: it must run - // before water (VRSwimDive) or a transformation (VRSpellTransformPuppet) change the rig's speeds, else theirs - // stay the "own" speeds for the session. Goes away once VRHeroMovement is the rig speeds' only writer. - private VrHeroSpeeds _rigSpeeds = VrHeroSpeeds.Fallback; // Water (VRSwimDive) or a transformation (VRSpellTransformPuppet) sets the rig's speeds meanwhile. private VRSwimDive _swimDive; private bool _isSpeedSetElsewhere; @@ -88,8 +83,6 @@ private void Awake() if (_rig == null) Logger.LogWarning("[VRHeroMovement] No VR rig above - no body states, steps or speed changes.", LogCat.VR); - else - _rigSpeeds = new VrHeroSpeeds(_rig.MoveSpeed, _rig.RunSpeed, _rig.MoveSpeed); GlobalEventDispatcher.ScriptHeroOverlayTimed.AddListener(OnHeroOverlayTimed); } @@ -97,6 +90,7 @@ private void OnDestroy() { GlobalEventDispatcher.ScriptHeroOverlayTimed.RemoveListener(OnHeroOverlayTimed); SpeedOverlay = null; + _vrPlayerService.IsHeroSpeedBoosted = false; } private void Update() @@ -120,16 +114,13 @@ private void Update() private void UpdateSpeeds() { var bodySpeeds = _vrPlayerService.HeroBodySpeeds; - var speeds = bodySpeeds ?? _rigSpeeds; - if (_isSpeedBoosted && !speeds.IsSpeedOverlayRun) - speeds = speeds.WithSpeedPotion(); - _vrPlayerService.HeroSpeeds = speeds; + var speeds = _vrPlayerService.HeroSpeeds; if (_isBodySpeedLogged != bodySpeeds.HasValue) { _isBodySpeedLogged = bodySpeeds.HasValue; Logger.Log(bodySpeeds.HasValue ? "[VRHeroMovement] Moving at the VR hero body's animation speeds." - : $"[VRHeroMovement] No animated VR hero body - the rig's own speeds ({_rigSpeeds}).", LogCat.VR); + : $"[VRHeroMovement] No animated VR hero body - the rig's own speeds ({speeds}).", LogCat.VR); } var isSpeedSetElsewhere = _vrSpellTransformService.IsTransformed || @@ -169,17 +160,17 @@ private void OnHeroOverlayTimed(string overlayName, float seconds) return; _speedOverlay = overlayName; - _isSpeedBoosted = true; + _vrPlayerService.IsHeroSpeedBoosted = true; _speedBoostEndTime = Time.time + seconds; SpeedOverlay = _speedOverlay; } private void UpdateSpeedBoost() { - if (!_isSpeedBoosted || Time.time < _speedBoostEndTime) + if (!_vrPlayerService.IsHeroSpeedBoosted || Time.time < _speedBoostEndTime) return; - _isSpeedBoosted = false; + _vrPlayerService.IsHeroSpeedBoosted = false; SpeedOverlay = null; } diff --git a/Assets/Gothic-VR/Scripts/Interaction/Backends/Xri/XriRig.cs b/Assets/Gothic-VR/Scripts/Interaction/Backends/Xri/XriRig.cs index 2402d8cea..af2616168 100644 --- a/Assets/Gothic-VR/Scripts/Interaction/Backends/Xri/XriRig.cs +++ b/Assets/Gothic-VR/Scripts/Interaction/Backends/Xri/XriRig.cs @@ -70,12 +70,20 @@ public class XriRig : MonoBehaviour, IVrRig [Header("Teleport")] [Tooltip("Thumbstick teleport (arc + ring). Child of this GameObject.")] [SerializeField] private XriTeleportAimer _teleportAimer; - [Header("Movement")] [Tooltip("Walking speed in m/s.")] [SerializeField] + [Header("Movement")] + [Tooltip("Walking speed in m/s as configured (DefaultMoveSpeed) - the game's MoveSpeed doesn't change it.")] + [SerializeField] private float _moveSpeed = 1.5f; - [Tooltip("Speed in m/s while sprinting (sprint button, simulator: Left Shift).")] [SerializeField] + [Tooltip("Speed in m/s while sprinting (sprint button, simulator: Left Shift) as configured (DefaultRunSpeed).")] + [SerializeField] private float _runSpeed = 3.5f; + // MoveSpeed/RunSpeed as the game set them, null = the configured ones above - so those stay readable as the + // defaults, whoever sets the speeds first. + private float? _setMoveSpeed; + private float? _setRunSpeed; + [Tooltip("Thumbstick deflection below which a clicked sprint ends.")] [SerializeField] private float _sprintStopThreshold = 0.1f; @@ -290,16 +298,19 @@ public float MaxFallSpeed public float MoveSpeed { - get => _moveSpeed; - set => _moveSpeed = value; + get => _setMoveSpeed ?? _moveSpeed; + set => _setMoveSpeed = value; } public float RunSpeed { - get => _runSpeed; - set => _runSpeed = value; + get => _setRunSpeed ?? _runSpeed; + set => _setRunSpeed = value; } + public float DefaultMoveSpeed => _moveSpeed; + public float DefaultRunSpeed => _runSpeed; + public float StepOffset { get => _characterController.stepOffset; @@ -591,7 +602,7 @@ private void UpdateSprint() _isSprintLatched = false; var isSprinting = _canSprint && (_isSprintLatched || sprintAction.IsActive); - _moveProvider.moveSpeed = isSprinting ? _runSpeed : _moveSpeed; + _moveProvider.moveSpeed = isSprinting ? RunSpeed : MoveSpeed; } private void UpdateCrouch() diff --git a/Assets/Gothic-VR/Scripts/Interaction/Ports/IVrRig.cs b/Assets/Gothic-VR/Scripts/Interaction/Ports/IVrRig.cs index 4039dffba..962b2b776 100644 --- a/Assets/Gothic-VR/Scripts/Interaction/Ports/IVrRig.cs +++ b/Assets/Gothic-VR/Scripts/Interaction/Ports/IVrRig.cs @@ -59,8 +59,19 @@ public interface IVrRig bool CanSprint { get; set; } float Gravity { get; set; } float MaxFallSpeed { get; set; } + /// + /// Walking and sprinting speed (m/s) right now. VRHeroMovement is the only writer. + /// float MoveSpeed { get; set; } float RunSpeed { get; set; } + + /// + /// The backend's own walking and sprinting speed (m/s) as configured - setting MoveSpeed/RunSpeed never changes + /// them. The hero moves at them without an animated VR hero body. + /// + float DefaultMoveSpeed { get; } + float DefaultRunSpeed { get; } + float StepOffset { get; set; } /// diff --git a/Assets/Gothic-VR/Scripts/Models/VrHeroSpeeds.cs b/Assets/Gothic-VR/Scripts/Models/VrHeroSpeeds.cs index db7ee182d..96c14ea52 100644 --- a/Assets/Gothic-VR/Scripts/Models/VrHeroSpeeds.cs +++ b/Assets/Gothic-VR/Scripts/Models/VrHeroSpeeds.cs @@ -3,14 +3,10 @@ namespace Gothic.VR.Models /// /// The VR hero's speeds on land in m/s: Gothic's walk, run and sneak loops (S_WALKL, S_RUNL, S_SNEAKL) at the VR /// hero body's scale, so the body's feet match the ground - or the rig's own speeds without an animated body. + /// All 0 (default) before the VR rig exists. /// public readonly struct VrHeroSpeeds { - /// - /// Before VRHeroMovement's first frame: the XRI rig's default speeds. - /// - public static readonly VrHeroSpeeds Fallback = new(1.5f, 3.5f, 1.5f); - public readonly float Walk; public readonly float Run; public readonly float Sneak; diff --git a/Assets/Gothic-VR/Scripts/Services/VRPlayerService.cs b/Assets/Gothic-VR/Scripts/Services/VRPlayerService.cs index 69821de30..100820d62 100644 --- a/Assets/Gothic-VR/Scripts/Services/VRPlayerService.cs +++ b/Assets/Gothic-VR/Scripts/Services/VRPlayerService.cs @@ -367,10 +367,28 @@ private void OnPlayerPrefUpdated(string key, object value) public VrHeroSpeeds? HeroBodySpeeds { get; set; } /// - /// The speeds the hero moves with on land (VRHeroMovement: the body's, else the rig's own, with the speed - /// potion). Its RunThreshold is BS_RUN for the scripts and the body's run animation. + /// The speed potion works (Mdl_ApplyOverlayMDSTimed HUMANS_SPRINT.MDS) - VRHeroMovement times it. /// - public VrHeroSpeeds HeroSpeeds { get; set; } = VrHeroSpeeds.Fallback; + public bool IsHeroSpeedBoosted { get; set; } + + /// + /// The speeds the hero moves with on land (GetHeroSpeeds). VRHeroMovement makes the rig's speeds of them, their + /// RunThreshold is BS_RUN for the scripts and the body's run animation. + /// + public VrHeroSpeeds HeroSpeeds => GetHeroSpeeds(); + + /// + /// The VR hero body's speeds, else the rig's own (all 0 before the rig exists). The speed potion runs with its + /// overlay's own run loop, without one walking and running are half again as fast. + /// + private VrHeroSpeeds GetHeroSpeeds() + { + var rig = Rig; + var speeds = HeroBodySpeeds ?? (rig == null + ? default + : new VrHeroSpeeds(rig.DefaultMoveSpeed, rig.DefaultRunSpeed, rig.DefaultMoveSpeed)); + return IsHeroSpeedBoosted && !speeds.IsSpeedOverlayRun ? speeds.WithSpeedPotion() : speeds; + } public GameObject GrabbedItemLeft; public GameObject GrabbedItemRight; From 5c22ec9e5ec7b77ea4006d167df24d2e4e894653 Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Sun, 29 Nov 2026 15:45:23 +0000 Subject: [PATCH 12/20] refactor: VRHeroMovement is the only writer of the rig's speeds - water and a transformation hand it an override (VRPlayerService.SetHeroSpeedOverride, water first), wading and swimming at half the body's walk speed, no land restores of their own and no blip of the prefab speeds leaving the water Co-Authored-By: Claude Opus 5.5 --- .../Scripts/Adapters/Player/VRHeroMovement.cs | 55 +++++++++-------- .../Adapters/Player/VRSpellTransformPuppet.cs | 61 ++++--------------- .../Scripts/Adapters/Player/VRSwimDive.cs | 33 +++++----- .../Scripts/Models/VrHeroSpeedOverride.cs | 58 ++++++++++++++++++ .../Scripts/Models/VrHeroSpeedSource.cs | 15 +++++ .../Scripts/Services/VRPlayerService.cs | 35 +++++++++++ Assets/Gothic-VR/Scripts/VRConstants.cs | 6 +- 7 files changed, 175 insertions(+), 88 deletions(-) create mode 100644 Assets/Gothic-VR/Scripts/Models/VrHeroSpeedOverride.cs create mode 100644 Assets/Gothic-VR/Scripts/Models/VrHeroSpeedSource.cs diff --git a/Assets/Gothic-VR/Scripts/Adapters/Player/VRHeroMovement.cs b/Assets/Gothic-VR/Scripts/Adapters/Player/VRHeroMovement.cs index f480723a6..c6ccaffd7 100644 --- a/Assets/Gothic-VR/Scripts/Adapters/Player/VRHeroMovement.cs +++ b/Assets/Gothic-VR/Scripts/Adapters/Player/VRHeroMovement.cs @@ -17,9 +17,9 @@ namespace Gothic.VR.Adapters.Player { /// /// What the engine derives from the hero's movement, for any VR backend (IVrRig): BS_WALK/BS_RUN/BS_SNEAK, the - /// hero's own footsteps and the rig's speeds on land - Gothic's walk/run/sneak loops at the VR hero body's scale - /// (VRPlayerService.HeroBodySpeeds), with the speed potion. Sits next to VRPlayer (added by - /// VRContextInteractionService). + /// hero's own footsteps and the rig's speeds - their only writer: on land Gothic's walk/run/sneak loops at the VR + /// hero body's scale with the speed potion (VRPlayerService.HeroSpeeds), in water or transformed what those hand + /// over (VRPlayerService.HeroSpeedOverride). Sits next to VRPlayer (added by VRContextInteractionService). /// public class VRHeroMovement : MonoBehaviour { @@ -27,7 +27,6 @@ public class VRHeroMovement : MonoBehaviour [Inject] private readonly AudioService _audioService; [Inject] private readonly GroundSoundService _groundSoundService; [Inject] private readonly VRPlayerService _vrPlayerService; - [Inject] private readonly VRSpellTransformService _vrSpellTransformService; // Gothic.ini footstepSounds: the hero's own steps - Gothic's RUN_ every stride of the way // walked (the VR body plays no step events; a timer stepped on while the run state lingered after a tap). @@ -55,11 +54,10 @@ public class VRHeroMovement : MonoBehaviour private float _lastRunTime = float.MinValue; private Vector3 _lastBodyStatePosition; - // Water (VRSwimDive) or a transformation (VRSpellTransformPuppet) sets the rig's speeds meanwhile. - private VRSwimDive _swimDive; - private bool _isSpeedSetElsewhere; - // Which speeds the rig got last (logged once per change): the body's (true), the rig's own (false). + // Which speeds the rig got last (logged once per change): the body's (true), the rig's own (false) - and who + // sets them instead of the land speeds (null: nobody). private bool? _isBodySpeedLogged; + private VrHeroSpeedSource? _speedOverrideSource; // NPC_TALENT_SNEAK (G1/G2: 8) - read from the scripts. private const string _sneakTalentSymbol = "NPC_TALENT_SNEAK"; @@ -79,7 +77,6 @@ private void Awake() { this.Inject(); _rig = GetComponentInParent(); - _swimDive = GetComponent(); if (_rig == null) Logger.LogWarning("[VRHeroMovement] No VR rig above - no body states, steps or speed changes.", LogCat.VR); @@ -105,11 +102,12 @@ private void Update() } /// - /// The rig's speeds on land: the stick walks and sprint runs at the VR hero body's walk/run loops (its feet - /// match the ground), the rig's own speeds without an animated body. The speed potion runs with its overlay's - /// run loop, half again as fast without one. Crouched is sneaking speed for both, like vanilla Gothic's "no - /// sprint while sneaking" - also without the sneak talent: the body sneaks then, only the scripts hear a walk. - /// Water (VRSwimDive) and a transformation (VRSpellTransformPuppet) set their own speeds and restore ours. + /// The rig's speeds - VRHeroMovement is their only writer. On land the stick walks and sprint runs at the VR + /// hero body's walk/run loops (its feet match the ground), the rig's own speeds without an animated body, the + /// speed potion included (VRPlayerService.HeroSpeeds). Crouched is sneaking speed for both, like vanilla + /// Gothic's "no sprint while sneaking" - also without the sneak talent: the body sneaks then, only the scripts + /// hear a walk. Water (VRSwimDive) and a transformation (VRSpellTransformPuppet) hand over their own speeds + /// (VRPlayerService.HeroSpeedOverride) - the land speeds come back the frame they let go. /// private void UpdateSpeeds() { @@ -123,21 +121,28 @@ private void UpdateSpeeds() : $"[VRHeroMovement] No animated VR hero body - the rig's own speeds ({speeds}).", LogCat.VR); } - var isSpeedSetElsewhere = _vrSpellTransformService.IsTransformed || - (_swimDive != null && _swimDive.IsInWater); - if (isSpeedSetElsewhere != _isSpeedSetElsewhere) + var speedOverride = _vrPlayerService.HeroSpeedOverride; + if (speedOverride?.Source != _speedOverrideSource) { - _isSpeedSetElsewhere = isSpeedSetElsewhere; - Logger.Log(isSpeedSetElsewhere - ? "[VRHeroMovement] Water or a transformation sets the speeds." + _speedOverrideSource = speedOverride?.Source; + Logger.Log(speedOverride.HasValue + ? $"[VRHeroMovement] Speeds of {speedOverride.Value}." : $"[VRHeroMovement] Land speeds again ({speeds}).", LogCat.VR); } - if (isSpeedSetElsewhere) - return; - var isSneaking = IsCrouchSneaking(); - var moveSpeed = isSneaking ? speeds.Sneak : speeds.Walk; - var runSpeed = isSneaking ? speeds.Sneak : speeds.Run; + float moveSpeed; + float runSpeed; + if (speedOverride.HasValue) + { + moveSpeed = speedOverride.Value.GetMoveSpeed(speeds); + runSpeed = speedOverride.Value.GetRunSpeed(speeds); + } + else + { + var isSneaking = IsCrouchSneaking(); + moveSpeed = isSneaking ? speeds.Sneak : speeds.Walk; + runSpeed = isSneaking ? speeds.Sneak : speeds.Run; + } if (!Mathf.Approximately(_rig.MoveSpeed, moveSpeed)) _rig.MoveSpeed = moveSpeed; if (!Mathf.Approximately(_rig.RunSpeed, runSpeed)) diff --git a/Assets/Gothic-VR/Scripts/Adapters/Player/VRSpellTransformPuppet.cs b/Assets/Gothic-VR/Scripts/Adapters/Player/VRSpellTransformPuppet.cs index d7199a9f8..c80a9cf33 100644 --- a/Assets/Gothic-VR/Scripts/Adapters/Player/VRSpellTransformPuppet.cs +++ b/Assets/Gothic-VR/Scripts/Adapters/Player/VRSpellTransformPuppet.cs @@ -11,6 +11,7 @@ using Gothic.Core.Services.Context; using Gothic.Core.Services.Npc; using Gothic.VR.Interaction.Ports; +using Gothic.VR.Models; using Gothic.VR.Services; using Gothic.VR.Interaction.Models; using Reflex.Attributes; @@ -94,13 +95,7 @@ public class VRSpellTransformPuppet : MonoBehaviour private IVrRig _rig; private float _originalCameraYOffset; - // The player's speeds before, the human run loop to compare with. - private float _originalMoveSpeed; - private float _originalRunSpeed; - private bool _hasSpeedScale; - // The monster's speeds, set again after the water changed them. - private float _monsterMoveSpeed; - private float _monsterRunSpeed; + // The human run loop to compare with. private const string _humanRunAnimation = "S_RUNL"; private const float _waterRayAboveEyes = 1f; // Sprinting as a monster, times its run loop's speed. @@ -168,11 +163,8 @@ public void Release() _isReleased = true; if (_hasCameraOffset && _rig != null) _rig.CameraYOffset = _originalCameraYOffset; - if (_hasSpeedScale && _rig != null) - { - _rig.MoveSpeed = _originalMoveSpeed; - _rig.RunSpeed = _originalRunSpeed; - } + // The hero's land speeds again (VRHeroMovement). + _vrPlayerService.ClearHeroSpeedOverride(VrHeroSpeedSource.Transformation); if (_isReady) _rig?.SetGrabbingEnabled(true); @@ -215,7 +207,6 @@ private void Update() aiHandler.enabled = false; _monster.Props.AnimationQueue.Clear(); - KeepMonsterSpeed(); UpdateAnimation(); UpdateAttack(); UpdateTransformBackInput(); @@ -724,13 +715,11 @@ private void UpdateTransformBackInput() /// /// The player moves as fast as the monster runs - its run loop's /// root motion against the human one (HUMANS S_RUNL). A wolf is faster than a human, a meatbug slower; with the - /// camera at its eyes the human speed felt like crawling. + /// camera at its eyes the human speed felt like crawling. Handed to VRHeroMovement (the rig speeds' only + /// writer) until Release; water still goes first (VRSwimDive). /// private void ScaleSpeedToMonster() { - if (_rig == null) - return; - var walkMode = _monster.Vob.AiHuman.WalkMode; _monster.Vob.AiHuman.WalkMode = (int)VmGothicEnums.WalkMode.Run; var run = _animationService.GetAnimationName(VmGothicEnums.AnimationType.Move, _monster); @@ -748,37 +737,13 @@ private void ScaleSpeedToMonster() return; } - _originalMoveSpeed = _rig.MoveSpeed; - _originalRunSpeed = _rig.RunSpeed; - _hasSpeedScale = true; - - // The monster's own speeds - moving is its run loop (Gothic's default walk mode, a wolf 4.6 m/s), sprinting half again; a light stick is its walk. Scaling the VR speeds - // (1.5 m/s moving) left a wolf at 2.1 m/s. - _rig.MoveSpeed = monsterSpeed; - _rig.RunSpeed = monsterSpeed * _monsterSprintFactor; - Logger.Log($"[VRTransform] Speed {monsterSpeed:F2} m/s, sprint {_rig.RunSpeed:F2} m/s " + - $"({run}).", LogCat.VR); - _monsterMoveSpeed = _rig.MoveSpeed; - _monsterRunSpeed = _rig.RunSpeed; - } - - /// - /// Water sets the player's speeds (swimming, wading) and back on - /// land the human ones it saved at the start - a meatbug left the water faster than it should, a shadowbeast - /// slower. On land the monster's speeds are set again. - /// - private void KeepMonsterSpeed() - { - if (!_hasSpeedScale || _rig == null || - IsInWater() || _waterDepth > 0f) - return; - if (Mathf.Approximately(_rig.MoveSpeed, _monsterMoveSpeed) && - Mathf.Approximately(_rig.RunSpeed, _monsterRunSpeed)) - return; - - _rig.MoveSpeed = _monsterMoveSpeed; - _rig.RunSpeed = _monsterRunSpeed; - Logger.Log($"[VRTransform] Back on land - speed {_monsterMoveSpeed:F2} m/s again.", LogCat.VR); + // The monster's own speeds - moving is its run loop (Gothic's default walk mode, a wolf 4.6 m/s), + // sprinting half again; a light stick is its walk. Scaling the VR speeds (1.5 m/s moving) left a wolf at + // 2.1 m/s. No speed potion: its overlay is a human MDS. + var sprintSpeed = monsterSpeed * _monsterSprintFactor; + _vrPlayerService.SetHeroSpeedOverride(VrHeroSpeedOverride.Fixed(VrHeroSpeedSource.Transformation, + monsterSpeed, sprintSpeed)); + Logger.Log($"[VRTransform] Speed {monsterSpeed:F2} m/s, sprint {sprintSpeed:F2} m/s ({run}).", LogCat.VR); } /// diff --git a/Assets/Gothic-VR/Scripts/Adapters/Player/VRSwimDive.cs b/Assets/Gothic-VR/Scripts/Adapters/Player/VRSwimDive.cs index 5435c446e..564b37df0 100644 --- a/Assets/Gothic-VR/Scripts/Adapters/Player/VRSwimDive.cs +++ b/Assets/Gothic-VR/Scripts/Adapters/Player/VRSwimDive.cs @@ -20,6 +20,8 @@ using Gothic.Core.Services.Vm; using Gothic.VR.Interaction.Models; using Gothic.VR.Interaction.Ports; +using Gothic.VR.Models; +using Gothic.VR.Services; using Reflex.Attributes; using UnityEngine; using ZenKit.Daedalus; @@ -45,15 +47,8 @@ public class VRSwimDive : MonoBehaviour, IMarvinPropertyCollector // private string _diveBubbleParticleName; private float _initialGravity; - private float _initialMoveSpeed; - private float _initialRunSpeed; private float _initialFallSpeed; - /// - /// Wading, swimming or diving: the water sets the rig's speeds, VRHeroMovement leaves them alone meanwhile. - /// - public bool IsInWater { get; private set; } - private VmGothicEnums.WalkMode _mode = VmGothicEnums.WalkMode.Walk; // Swim + Dive properties @@ -97,6 +92,7 @@ public class VRSwimDive : MonoBehaviour, IMarvinPropertyCollector [Inject] private readonly ContextInteractionService _contextInteractionService; [Inject] private readonly MeshService _meshService; [Inject] private readonly PlayerService _playerService; + [Inject] private readonly VRPlayerService _vrPlayerService; [Inject] private readonly IVrInput _vrInput; [Inject] private readonly IVrSfxPlayer _vrSfxPlayer; [Inject] private readonly ConfigService _configService; @@ -176,8 +172,6 @@ SfxModel FindSwimSfx(string animName) }); _initialGravity = _rig.Gravity; - _initialMoveSpeed = _rig.MoveSpeed; - _initialRunSpeed = _rig.RunSpeed; _initialFallSpeed = _rig.MaxFallSpeed; } @@ -281,7 +275,6 @@ private void ChangeWaterBehavior() _waterBobbingCoroutine = null; } - IsInWater = _playerAi.WaterLevel != (int)ZenGineConst.WaterLevel.Normal; switch ((ZenGineConst.WaterLevel)_playerAi.WaterLevel) { case ZenGineConst.WaterLevel.Normal: @@ -295,8 +288,8 @@ private void ChangeWaterBehavior() Shader.SetGlobalInt(Constants.ShaderPropertyWaterEffectToggle, 0); _rig.Gravity = _initialGravity; - _rig.MoveSpeed = _initialMoveSpeed; - _rig.RunSpeed = _initialRunSpeed; + // The land speeds again (VRHeroMovement). + _vrPlayerService.ClearHeroSpeedOverride(VrHeroSpeedSource.Water); _rig.CanSprint = true; _rig.MaxFallSpeed = _initialFallSpeed; @@ -320,8 +313,8 @@ private void ChangeWaterBehavior() Shader.SetGlobalInt(Constants.ShaderPropertyWaterEffectToggle, 0); _rig.Gravity = _initialGravity; - _rig.MoveSpeed = _initialMoveSpeed / 2; - _rig.RunSpeed = _initialMoveSpeed / 2; // No running, but it might be, that we run into deep water, then we need to slow it down like walking. + // Wading: no running, also when running into the water - half the land walk speed. + SetWaterSpeeds(); _rig.CanSprint = false; _rig.MaxFallSpeed = _initialFallSpeed; @@ -344,6 +337,8 @@ private void ChangeWaterBehavior() _mode = VmGothicEnums.WalkMode.Swim; _rig.Gravity = 0f; _rig.MaxFallSpeed = 0f; + // Also straight into deep water (a teleport, a jump) - not the land speeds. + SetWaterSpeeds(); _contextInteractionService.SetSwimmingControls(); @@ -357,6 +352,16 @@ private void ChangeWaterBehavior() UpdateHeroBodyState(); } + /// + /// Wading, swimming and diving move at a share of the land walk speed (VrHeroSpeedOverride follows the VR hero + /// body's speeds) - VRHeroMovement sets the rig's speeds of it. + /// + private void SetWaterSpeeds() + { + _vrPlayerService.SetHeroSpeedOverride(VrHeroSpeedOverride.ShareOfWalk(VrHeroSpeedSource.Water, + VRConstants.Swim.WaterWalkSpeedShare)); + } + /// /// Scripts check C_BodyStateContains(other, BS_SWIM/BS_DIVE): monsters stop chasing a swimming hero /// (ZS_MM_Attack, AIV_MM_FollowInWater), no sleep/freeze spells in water, ... diff --git a/Assets/Gothic-VR/Scripts/Models/VrHeroSpeedOverride.cs b/Assets/Gothic-VR/Scripts/Models/VrHeroSpeedOverride.cs new file mode 100644 index 000000000..c5b583f2a --- /dev/null +++ b/Assets/Gothic-VR/Scripts/Models/VrHeroSpeedOverride.cs @@ -0,0 +1,58 @@ +namespace Gothic.VR.Models +{ + /// + /// The VR hero's walking and sprinting speed (m/s) while water or a transformation sets them instead of the land + /// speeds (VrHeroSpeeds) - fixed, or a share of the land walk speed, so it follows the body's speeds. + /// VRHeroMovement makes the rig's speeds of it. + /// + public readonly struct VrHeroSpeedOverride + { + public readonly VrHeroSpeedSource Source; + + private readonly float _move; + private readonly float _run; + // _move and _run are shares of the land walk speed, not m/s. + private readonly bool _isShareOfWalk; + + private VrHeroSpeedOverride(VrHeroSpeedSource source, float move, float run, bool isShareOfWalk) + { + Source = source; + _move = move; + _run = run; + _isShareOfWalk = isShareOfWalk; + } + + /// + /// Walking and sprinting at these speeds (m/s). + /// + public static VrHeroSpeedOverride Fixed(VrHeroSpeedSource source, float move, float run) + { + return new VrHeroSpeedOverride(source, move, run, false); + } + + /// + /// Walking and sprinting both at this share of the land walk speed - no sprint. + /// + public static VrHeroSpeedOverride ShareOfWalk(VrHeroSpeedSource source, float share) + { + return new VrHeroSpeedOverride(source, share, share, true); + } + + public float GetMoveSpeed(in VrHeroSpeeds land) + { + return _isShareOfWalk ? land.Walk * _move : _move; + } + + public float GetRunSpeed(in VrHeroSpeeds land) + { + return _isShareOfWalk ? land.Walk * _run : _run; + } + + public override string ToString() + { + return _isShareOfWalk + ? $"{Source}: {_move:F2} of the walk speed" + : $"{Source}: move {_move:F2}, sprint {_run:F2} m/s"; + } + } +} diff --git a/Assets/Gothic-VR/Scripts/Models/VrHeroSpeedSource.cs b/Assets/Gothic-VR/Scripts/Models/VrHeroSpeedSource.cs new file mode 100644 index 000000000..f8b774a84 --- /dev/null +++ b/Assets/Gothic-VR/Scripts/Models/VrHeroSpeedSource.cs @@ -0,0 +1,15 @@ +namespace Gothic.VR.Models +{ + /// + /// Who moves the VR hero at other speeds than his land speeds (VRPlayerService.SetHeroSpeedOverride). The first + /// one set in this order wins. + /// + public enum VrHeroSpeedSource + { + // Wading, swimming or diving (VRSwimDive). + Water, + + // Transformed into a monster (VRSpellTransformPuppet): its own speeds. + Transformation + } +} diff --git a/Assets/Gothic-VR/Scripts/Services/VRPlayerService.cs b/Assets/Gothic-VR/Scripts/Services/VRPlayerService.cs index 100820d62..0a3e308ea 100644 --- a/Assets/Gothic-VR/Scripts/Services/VRPlayerService.cs +++ b/Assets/Gothic-VR/Scripts/Services/VRPlayerService.cs @@ -390,6 +390,41 @@ private VrHeroSpeeds GetHeroSpeeds() return IsHeroSpeedBoosted && !speeds.IsSpeedOverlayRun ? speeds.WithSpeedPotion() : speeds; } + // Per VrHeroSpeedSource (its value is the index), null = that source doesn't set the speeds. + private readonly VrHeroSpeedOverride?[] _heroSpeedOverrides = + new VrHeroSpeedOverride?[Enum.GetValues(typeof(VrHeroSpeedSource)).Length]; + + /// + /// The speeds instead of the land speeds (HeroSpeeds) - the first source set in VrHeroSpeedSource's order + /// (water before a transformation), null on land. VRHeroMovement makes the rig's speeds of it. + /// + public VrHeroSpeedOverride? HeroSpeedOverride + { + get + { + foreach (var speedOverride in _heroSpeedOverrides) + { + if (speedOverride.HasValue) + return speedOverride; + } + return null; + } + } + + /// + /// Its source (water, a transformation) moves the hero at these speeds until ClearHeroSpeedOverride - the rig's + /// speeds are VRHeroMovement's alone. + /// + public void SetHeroSpeedOverride(VrHeroSpeedOverride speedOverride) + { + _heroSpeedOverrides[(int)speedOverride.Source] = speedOverride; + } + + public void ClearHeroSpeedOverride(VrHeroSpeedSource source) + { + _heroSpeedOverrides[(int)source] = null; + } + public GameObject GrabbedItemLeft; public GameObject GrabbedItemRight; diff --git a/Assets/Gothic-VR/Scripts/VRConstants.cs b/Assets/Gothic-VR/Scripts/VRConstants.cs index fa49f54d2..1c62bfd3c 100644 --- a/Assets/Gothic-VR/Scripts/VRConstants.cs +++ b/Assets/Gothic-VR/Scripts/VRConstants.cs @@ -300,7 +300,7 @@ public static class Magic /// /// VR swim values. Deliberately not the guild's water depths (GetWaterDepthKnee/Chest from Daedalus): those /// decide whether the hero walks, wades or swims (VRSwimDive). The values here only decide how the VR hero body - /// looks in the water (VrHeroSwimPose) and how far VRSwimDive looks for the surface. + /// looks in the water (VrHeroSwimPose), how far VRSwimDive looks for the surface and how fast he moves in it. /// public static class Swim { @@ -314,6 +314,10 @@ public static class Swim // VRSwimDive: the ray for the water surface goes this far (m) past the guild's chest depth, so the surface // is still found when the player is a bit deeper than chest deep. public const float WaterSurfaceRayBuffer = 2f; + + // VRSwimDive: wading, swimming and diving move (stick and sprint alike) at this share of the hero's land + // walk speed (VRPlayerService.HeroSpeeds.Walk). + public const float WaterWalkSpeedShare = 0.5f; } /// From 4897509f118cd8a94b4bab38fac5bda89cb4357f Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Sun, 29 Nov 2026 15:45:49 +0000 Subject: [PATCH 13/20] docs: the hero speed decision record - VRHeroMovement owns the rig's speeds, water and a transformation hand it theirs (water first), the rig says its own speeds, wading and swimming at half the hero's walk speed Co-Authored-By: Claude Opus 5.5 --- Docs/adr/2026-11-29-hero-speed-from-animations.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Docs/adr/2026-11-29-hero-speed-from-animations.md b/Docs/adr/2026-11-29-hero-speed-from-animations.md index 0393c6e1d..1f110e653 100644 --- a/Docs/adr/2026-11-29-hero-speed-from-animations.md +++ b/Docs/adr/2026-11-29-hero-speed-from-animations.md @@ -2,7 +2,7 @@ - Date: 2026-11-29 - Replaces: decision 7 of [VR movement, trade and theft](2026-11-03-vr-movement-trade-theft.md) (the speed potion as a factor on one base speed) -- Technical details: VrHeroLocomotion, VRHeroMovement, VrHeroSpeeds, the factors in VRConstants.Movement (VR) +- Technical details: VrHeroLocomotion, VRHeroMovement, VRPlayerService.HeroSpeeds and HeroSpeedOverride, VrHeroSpeeds, VrHeroSpeedOverride, IVrRig.DefaultMoveSpeed/DefaultRunSpeed, the factors in VRConstants.Movement and VRConstants.Swim (VR) ## Context @@ -19,12 +19,15 @@ In Gothic the speed comes from the animation itself: the hero moves as far as th 3. **Running starts halfway between walking and running speed,** the same for the body's run animation and for what the scripts see (running for them). So the stick walks, sprint runs, and NPCs see a walking hero as walking. Running still stays on for a moment after slowing down, and a teleport still counts as running. 4. **Crouched is always sneaking speed,** also without the sneak talent: the body sneaks then, only the scripts see a walking hero, as before. Sprint does nothing while crouched, like in Gothic. 5. **Walking and running are 1.4 times as fast as Gothic's loops, and the loops play 1.4 times as fast.** At 1x the hero felt slow in VR (playtest); the feet still match the ground because the steps speed up by as much. Sneaking keeps the loop's own speed. Not chosen: Gothic's speeds exactly (too slow in VR), faster without faster steps (sliding feet). -6. **Without an animated VR body** (body switched off, an armor that can't be built, a mod without walk or run animations) the hero moves at the VR rig's own speeds (1.5 and 3.5 m/s), with a warning in the log when animations are missing. +6. **Without an animated VR body** (body switched off, an armor that can't be built, a mod without walk or run animations) the hero moves at the VR rig's own speeds (1.5 and 3.5 m/s), with a warning in the log when animations are missing. The rig says what its own speeds are (its configured ones, which setting its speeds never changes). Not chosen: a copy of the numbers in our code, or reading them from the rig once at the start (wrong if water or a transformation changed them first). +7. **One owner of the rig's speeds: VRHeroMovement.** Water and a transformation no longer set the rig's speeds and put back their own copy of the land speeds afterwards. They tell VRHeroMovement which speeds they want instead, and take that back when they end. When both want something, water goes first. The land speeds (the body's or the rig's own, with the speed potion) are worked out in one place. Not chosen: each one setting the rig itself (four copies of the land speeds, the prefab speeds for a moment after leaving the water, and every new speed source would touch all of them). +8. **Wading and swimming move at half the hero's walk speed,** and follow it, so a tall hero wades faster than a short one. Before, it was half the rig's own 1.5 m/s, also when he walked at 1.9 m/s on land. Running is still off in the water. ## Consequences - At full stick the walk and run animations play 1.4 times as fast as in Gothic, the sneak animation at its normal pace; a half pushed stick still slows them down. - A hero of normal height is a bit faster than before (about 1.9 instead of 1.5 m/s walking, 4.6 instead of 3.5 m/s running); a tall player is faster than a short one. -- Water and a transformation into a monster keep setting their own speeds, the land speeds come back afterwards. The speed potion no longer speeds up a transformed hero. +- Leaving the water or turning back from a monster, the hero gets his land speeds again at once, without a short moment at the rig's 1.5/3.5 m/s. +- A transformed hero moves at the monster's speeds, the speed potion doesn't speed him up. In knee-deep or deeper water the water's speed counts, also for a transformed hero, and also when he transformed while wading (before, the monster's speeds counted then). - Moving sideways while sprinting is faster than Gothic's side step animation (the VR rig has one speed for all directions), so the feet can still slide there. - Nothing is saved: the speeds are read again from the animations. From d932053844f5730088322bd719d48f544f6d5d93 Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Sun, 29 Nov 2026 16:11:35 +0000 Subject: [PATCH 14/20] chore: meta files of VrHeroSpeedOverride and VrHeroSpeedSource (generated by Unity on import) Co-Authored-By: Claude Opus 5.5 --- Assets/Gothic-VR/Scripts/Models/VrHeroSpeedOverride.cs.meta | 2 ++ Assets/Gothic-VR/Scripts/Models/VrHeroSpeedSource.cs.meta | 2 ++ 2 files changed, 4 insertions(+) create mode 100644 Assets/Gothic-VR/Scripts/Models/VrHeroSpeedOverride.cs.meta create mode 100644 Assets/Gothic-VR/Scripts/Models/VrHeroSpeedSource.cs.meta diff --git a/Assets/Gothic-VR/Scripts/Models/VrHeroSpeedOverride.cs.meta b/Assets/Gothic-VR/Scripts/Models/VrHeroSpeedOverride.cs.meta new file mode 100644 index 000000000..d792a625d --- /dev/null +++ b/Assets/Gothic-VR/Scripts/Models/VrHeroSpeedOverride.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 2789c1e785e36af448e0f6ff836ba71e \ No newline at end of file diff --git a/Assets/Gothic-VR/Scripts/Models/VrHeroSpeedSource.cs.meta b/Assets/Gothic-VR/Scripts/Models/VrHeroSpeedSource.cs.meta new file mode 100644 index 000000000..9f32df23c --- /dev/null +++ b/Assets/Gothic-VR/Scripts/Models/VrHeroSpeedSource.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 6e22843f4e01f2c408ae27fec1661d28 \ No newline at end of file From 982b0d4b9f6fb9d5321b747baa008d4acbe39332 Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Sun, 29 Nov 2026 16:24:32 +0000 Subject: [PATCH 15/20] fix: the water speeds end with the world and with VRSwimDive - a loaded save or a world change while wading no longer leaves the hero walking at half speed without sprint on land (cleared on WorldSceneLoaded always and in OnDestroy, the listener removed) Co-Authored-By: Claude Opus 5.5 --- .../Scripts/Adapters/Player/VRSwimDive.cs | 52 +++++++++++++------ 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/Assets/Gothic-VR/Scripts/Adapters/Player/VRSwimDive.cs b/Assets/Gothic-VR/Scripts/Adapters/Player/VRSwimDive.cs index 564b37df0..d7281a96b 100644 --- a/Assets/Gothic-VR/Scripts/Adapters/Player/VRSwimDive.cs +++ b/Assets/Gothic-VR/Scripts/Adapters/Player/VRSwimDive.cs @@ -158,23 +158,35 @@ SfxModel FindSwimSfx(string animName) // } }); - GlobalEventDispatcher.WorldSceneLoaded.AddListener(() => - { - _heroContainer = ((NpcInstance)_gameStateService.GothicVm.GlobalHero).GetUserData()!; - _playerVob = _heroContainer.Vob; - _playerAi = (IAiHuman)_playerVob.Ai; - - // A loaded save (or new world) starts on land - leftovers of swimming/diving would keep the slow - // water movement. The next FixedUpdate detects water again if we spawned in it. - if ((_playerAi.WaterLevel != (int)ZenGineConst.WaterLevel.Normal || - _mode != VmGothicEnums.WalkMode.Walk)) - LeaveWater(); - }); - + GlobalEventDispatcher.WorldSceneLoaded.AddListener(OnWorldSceneLoaded); + _initialGravity = _rig.Gravity; _initialFallSpeed = _rig.MaxFallSpeed; } + private void OnDestroy() + { + GlobalEventDispatcher.WorldSceneLoaded.RemoveListener(OnWorldSceneLoaded); + // The water speeds live in a service that outlives this rig - a new rig would walk at them on land. + ClearWaterSpeeds(); + } + + private void OnWorldSceneLoaded() + { + _heroContainer = ((NpcInstance)_gameStateService.GothicVm.GlobalHero).GetUserData()!; + _playerVob = _heroContainer.Vob; + _playerAi = (IAiHuman)_playerVob.Ai; + + // A loaded save (or new world) starts on land - leftovers of swimming/diving would keep the slow + // water movement. The next FixedUpdate detects water again if we spawned in it. Always the land speeds + // and sprint again: a wading hero is in walk mode and the new hero vob on land, so LeaveWater isn't + // called for him. + ClearWaterSpeeds(); + if ((_playerAi.WaterLevel != (int)ZenGineConst.WaterLevel.Normal || + _mode != VmGothicEnums.WalkMode.Walk)) + LeaveWater(); + } + private void Update() { if (_playerAi == null) @@ -288,9 +300,7 @@ private void ChangeWaterBehavior() Shader.SetGlobalInt(Constants.ShaderPropertyWaterEffectToggle, 0); _rig.Gravity = _initialGravity; - // The land speeds again (VRHeroMovement). - _vrPlayerService.ClearHeroSpeedOverride(VrHeroSpeedSource.Water); - _rig.CanSprint = true; + ClearWaterSpeeds(); _rig.MaxFallSpeed = _initialFallSpeed; _contextInteractionService.SetWalkingControls(); @@ -362,6 +372,16 @@ private void SetWaterSpeeds() VRConstants.Swim.WaterWalkSpeedShare)); } + /// + /// The land speeds again (VRHeroMovement) and sprint back. + /// + private void ClearWaterSpeeds() + { + _vrPlayerService?.ClearHeroSpeedOverride(VrHeroSpeedSource.Water); + if (_rig != null) + _rig.CanSprint = true; + } + /// /// Scripts check C_BodyStateContains(other, BS_SWIM/BS_DIVE): monsters stop chasing a swimming hero /// (ZS_MM_Attack, AIV_MM_FollowInWater), no sleep/freeze spells in water, ... From 3d94d31e967d301e0c2252207f980da81c522989 Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Sun, 29 Nov 2026 16:24:54 +0000 Subject: [PATCH 16/20] docs: the VR hero body's walk/run loops play about 1.4 times as fast at full stick (WalkRunFactor), not about 1 - the playback comment in VrHeroLocomotion said otherwise Co-Authored-By: Claude Opus 5.5 --- Assets/Gothic-VR/Scripts/Adapters/Player/VrHeroLocomotion.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Assets/Gothic-VR/Scripts/Adapters/Player/VrHeroLocomotion.cs b/Assets/Gothic-VR/Scripts/Adapters/Player/VrHeroLocomotion.cs index f864e1bbd..b5a8143a1 100644 --- a/Assets/Gothic-VR/Scripts/Adapters/Player/VrHeroLocomotion.cs +++ b/Assets/Gothic-VR/Scripts/Adapters/Player/VrHeroLocomotion.cs @@ -111,8 +111,9 @@ public void Update(NpcContainer hero, Transform body, IVrRig rig, float scale, b animation ??= speed < _idleSpeed ? _idleAnimation : _walkAnimation; } - // Walk/run loops at the real speed, so the feet don't slide. At full stick the rig moves at the loop's own - // speed (UpdateSpeeds) - about 1, the factor is for a half pushed stick and speeding up. + // Walk/run loops at the real speed, so the feet don't slide. At full stick the rig moves at the loop's + // speed times VRConstants.Movement.WalkRunFactor (UpdateSpeeds) - they play about 1.4 times as fast + // (sneaking about 1); slower for a half pushed stick and while speeding up. var animationSpeed = _animator.GetMovementSpeed(animation) * scale; var playbackSpeed = animationSpeed > 0.1f && speed >= _idleSpeed ? Mathf.Clamp(speed / animationSpeed, 0.5f, 2f) From 3b663f4a9a4d51eb037ea3ac3b83277fd4fae522 Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Sun, 29 Nov 2026 16:27:45 +0000 Subject: [PATCH 17/20] refactor: the VR hero's speeds have their own service (VRHeroSpeedService, PlayerScope) - body speeds, speed potion, land speeds and the overrides move out of VRPlayerService, which overrides wins is an explicit priority list (water first) instead of the enum's order, with an EditMode test Co-Authored-By: Claude Opus 5.5 --- .../EditMode/VR/VRHeroSpeedServiceTest.cs | 41 ++++++ .../Scripts/Adapters/Player/VRHeroMovement.cs | 28 ++--- .../Adapters/Player/VRSpellTransformPuppet.cs | 5 +- .../Scripts/Adapters/Player/VRSwimDive.cs | 6 +- .../Adapters/Player/VrHeroLocomotion.cs | 14 +-- .../Scripts/Models/VrHeroSpeedSource.cs | 5 +- .../Scripts/ReflexVRSceneInstaller.cs | 1 + .../Scripts/Services/VRHeroSpeedService.cs | 119 ++++++++++++++++++ .../Scripts/Services/VRPlayerService.cs | 65 ---------- Assets/Gothic-VR/Scripts/VRConstants.cs | 2 +- 10 files changed, 192 insertions(+), 94 deletions(-) create mode 100644 Assets/Gothic-Tests/EditMode/VR/VRHeroSpeedServiceTest.cs create mode 100644 Assets/Gothic-VR/Scripts/Services/VRHeroSpeedService.cs diff --git a/Assets/Gothic-Tests/EditMode/VR/VRHeroSpeedServiceTest.cs b/Assets/Gothic-Tests/EditMode/VR/VRHeroSpeedServiceTest.cs new file mode 100644 index 000000000..310317650 --- /dev/null +++ b/Assets/Gothic-Tests/EditMode/VR/VRHeroSpeedServiceTest.cs @@ -0,0 +1,41 @@ +using System; +using Gothic.VR.Models; +using Gothic.VR.Services; +using NUnit.Framework; + +namespace Gothic.Tests.EditMode.VR +{ + /// + /// Who sets the VR hero's speeds instead of his land speeds (VRHeroSpeedService.HeroSpeedOverride): every source + /// has a place in the priority list, and water goes before a transformation whatever was set first. + /// + public class VRHeroSpeedServiceTest + { + [Test] + public void EverySourceCanSetTheSpeeds() + { + foreach (VrHeroSpeedSource source in Enum.GetValues(typeof(VrHeroSpeedSource))) + { + var service = new VRHeroSpeedService(); + service.SetHeroSpeedOverride(VrHeroSpeedOverride.Fixed(source, 1f, 2f)); + + Assert.AreEqual(source, service.HeroSpeedOverride?.Source, $"{source} has no place in the priority."); + } + } + + [Test] + public void WaterGoesBeforeATransformation() + { + var service = new VRHeroSpeedService(); + service.SetHeroSpeedOverride(VrHeroSpeedOverride.Fixed(VrHeroSpeedSource.Transformation, 4.6f, 6.9f)); + service.SetHeroSpeedOverride(VrHeroSpeedOverride.ShareOfWalk(VrHeroSpeedSource.Water, 0.5f)); + Assert.AreEqual(VrHeroSpeedSource.Water, service.HeroSpeedOverride?.Source); + + service.ClearHeroSpeedOverride(VrHeroSpeedSource.Water); + Assert.AreEqual(VrHeroSpeedSource.Transformation, service.HeroSpeedOverride?.Source); + + service.ClearHeroSpeedOverride(VrHeroSpeedSource.Transformation); + Assert.IsNull(service.HeroSpeedOverride); + } + } +} diff --git a/Assets/Gothic-VR/Scripts/Adapters/Player/VRHeroMovement.cs b/Assets/Gothic-VR/Scripts/Adapters/Player/VRHeroMovement.cs index c6ccaffd7..942f1f9e4 100644 --- a/Assets/Gothic-VR/Scripts/Adapters/Player/VRHeroMovement.cs +++ b/Assets/Gothic-VR/Scripts/Adapters/Player/VRHeroMovement.cs @@ -18,8 +18,8 @@ namespace Gothic.VR.Adapters.Player /// /// What the engine derives from the hero's movement, for any VR backend (IVrRig): BS_WALK/BS_RUN/BS_SNEAK, the /// hero's own footsteps and the rig's speeds - their only writer: on land Gothic's walk/run/sneak loops at the VR - /// hero body's scale with the speed potion (VRPlayerService.HeroSpeeds), in water or transformed what those hand - /// over (VRPlayerService.HeroSpeedOverride). Sits next to VRPlayer (added by VRContextInteractionService). + /// hero body's scale with the speed potion (VRHeroSpeedService.HeroSpeeds), in water or transformed what those + /// hand over (VRHeroSpeedService.HeroSpeedOverride). Sits next to VRPlayer (added by VRContextInteractionService). /// public class VRHeroMovement : MonoBehaviour { @@ -27,6 +27,7 @@ public class VRHeroMovement : MonoBehaviour [Inject] private readonly AudioService _audioService; [Inject] private readonly GroundSoundService _groundSoundService; [Inject] private readonly VRPlayerService _vrPlayerService; + [Inject] private readonly VRHeroSpeedService _heroSpeedService; // Gothic.ini footstepSounds: the hero's own steps - Gothic's RUN_ every stride of the way // walked (the VR body plays no step events; a timer stepped on while the run state lingered after a tap). @@ -87,7 +88,7 @@ private void OnDestroy() { GlobalEventDispatcher.ScriptHeroOverlayTimed.RemoveListener(OnHeroOverlayTimed); SpeedOverlay = null; - _vrPlayerService.IsHeroSpeedBoosted = false; + _heroSpeedService.IsHeroSpeedBoosted = false; } private void Update() @@ -104,15 +105,15 @@ private void Update() /// /// The rig's speeds - VRHeroMovement is their only writer. On land the stick walks and sprint runs at the VR /// hero body's walk/run loops (its feet match the ground), the rig's own speeds without an animated body, the - /// speed potion included (VRPlayerService.HeroSpeeds). Crouched is sneaking speed for both, like vanilla + /// speed potion included (VRHeroSpeedService.HeroSpeeds). Crouched is sneaking speed for both, like vanilla /// Gothic's "no sprint while sneaking" - also without the sneak talent: the body sneaks then, only the scripts /// hear a walk. Water (VRSwimDive) and a transformation (VRSpellTransformPuppet) hand over their own speeds - /// (VRPlayerService.HeroSpeedOverride) - the land speeds come back the frame they let go. + /// (VRHeroSpeedService.HeroSpeedOverride) - the land speeds come back the frame they let go. /// private void UpdateSpeeds() { - var bodySpeeds = _vrPlayerService.HeroBodySpeeds; - var speeds = _vrPlayerService.HeroSpeeds; + var bodySpeeds = _heroSpeedService.HeroBodySpeeds; + var speeds = _heroSpeedService.HeroSpeeds; if (_isBodySpeedLogged != bodySpeeds.HasValue) { _isBodySpeedLogged = bodySpeeds.HasValue; @@ -121,7 +122,7 @@ private void UpdateSpeeds() : $"[VRHeroMovement] No animated VR hero body - the rig's own speeds ({speeds}).", LogCat.VR); } - var speedOverride = _vrPlayerService.HeroSpeedOverride; + var speedOverride = _heroSpeedService.HeroSpeedOverride; if (speedOverride?.Source != _speedOverrideSource) { _speedOverrideSource = speedOverride?.Source; @@ -134,8 +135,7 @@ private void UpdateSpeeds() float runSpeed; if (speedOverride.HasValue) { - moveSpeed = speedOverride.Value.GetMoveSpeed(speeds); - runSpeed = speedOverride.Value.GetRunSpeed(speeds); + (moveSpeed, runSpeed) = _heroSpeedService.GetOverrideSpeeds(speedOverride.Value); } else { @@ -165,17 +165,17 @@ private void OnHeroOverlayTimed(string overlayName, float seconds) return; _speedOverlay = overlayName; - _vrPlayerService.IsHeroSpeedBoosted = true; + _heroSpeedService.IsHeroSpeedBoosted = true; _speedBoostEndTime = Time.time + seconds; SpeedOverlay = _speedOverlay; } private void UpdateSpeedBoost() { - if (!_vrPlayerService.IsHeroSpeedBoosted || Time.time < _speedBoostEndTime) + if (!_heroSpeedService.IsHeroSpeedBoosted || Time.time < _speedBoostEndTime) return; - _vrPlayerService.IsHeroSpeedBoosted = false; + _heroSpeedService.IsHeroSpeedBoosted = false; SpeedOverlay = null; } @@ -217,7 +217,7 @@ or VmGothicEnums.BodyState.BsWalk or VmGothicEnums.BodyState.BsRun // hero stayed BS_STAND while walking, so NPCs never heard him behind their backs. // Running from the same speed on as the VR hero body's run animation (VrHeroLocomotion). var speed = isFirstCheck || Time.deltaTime <= 0f ? 0f : horizontalJump / Time.deltaTime; - var bodyState = speed >= _vrPlayerService.HeroSpeeds.RunThreshold ? VmGothicEnums.BodyState.BsRun + var bodyState = speed >= _heroSpeedService.HeroSpeeds.RunThreshold ? VmGothicEnums.BodyState.BsRun : speed >= _bodyStateWalkSpeed ? VmGothicEnums.BodyState.BsWalk : VmGothicEnums.BodyState.BsStand; diff --git a/Assets/Gothic-VR/Scripts/Adapters/Player/VRSpellTransformPuppet.cs b/Assets/Gothic-VR/Scripts/Adapters/Player/VRSpellTransformPuppet.cs index c80a9cf33..377eacefe 100644 --- a/Assets/Gothic-VR/Scripts/Adapters/Player/VRSpellTransformPuppet.cs +++ b/Assets/Gothic-VR/Scripts/Adapters/Player/VRSpellTransformPuppet.cs @@ -34,6 +34,7 @@ namespace Gothic.VR.Adapters.Player public class VRSpellTransformPuppet : MonoBehaviour { [Inject] private readonly VRPlayerService _vrPlayerService; + [Inject] private readonly VRHeroSpeedService _heroSpeedService; [Inject] private readonly IVrInput _vrInput; [Inject] private readonly AnimationService _animationService; [Inject] private readonly ContextInteractionService _contextInteractionService; @@ -164,7 +165,7 @@ public void Release() if (_hasCameraOffset && _rig != null) _rig.CameraYOffset = _originalCameraYOffset; // The hero's land speeds again (VRHeroMovement). - _vrPlayerService.ClearHeroSpeedOverride(VrHeroSpeedSource.Transformation); + _heroSpeedService.ClearHeroSpeedOverride(VrHeroSpeedSource.Transformation); if (_isReady) _rig?.SetGrabbingEnabled(true); @@ -741,7 +742,7 @@ private void ScaleSpeedToMonster() // sprinting half again; a light stick is its walk. Scaling the VR speeds (1.5 m/s moving) left a wolf at // 2.1 m/s. No speed potion: its overlay is a human MDS. var sprintSpeed = monsterSpeed * _monsterSprintFactor; - _vrPlayerService.SetHeroSpeedOverride(VrHeroSpeedOverride.Fixed(VrHeroSpeedSource.Transformation, + _heroSpeedService.SetHeroSpeedOverride(VrHeroSpeedOverride.Fixed(VrHeroSpeedSource.Transformation, monsterSpeed, sprintSpeed)); Logger.Log($"[VRTransform] Speed {monsterSpeed:F2} m/s, sprint {sprintSpeed:F2} m/s ({run}).", LogCat.VR); } diff --git a/Assets/Gothic-VR/Scripts/Adapters/Player/VRSwimDive.cs b/Assets/Gothic-VR/Scripts/Adapters/Player/VRSwimDive.cs index d7281a96b..db2bd610a 100644 --- a/Assets/Gothic-VR/Scripts/Adapters/Player/VRSwimDive.cs +++ b/Assets/Gothic-VR/Scripts/Adapters/Player/VRSwimDive.cs @@ -92,7 +92,7 @@ public class VRSwimDive : MonoBehaviour, IMarvinPropertyCollector [Inject] private readonly ContextInteractionService _contextInteractionService; [Inject] private readonly MeshService _meshService; [Inject] private readonly PlayerService _playerService; - [Inject] private readonly VRPlayerService _vrPlayerService; + [Inject] private readonly VRHeroSpeedService _heroSpeedService; [Inject] private readonly IVrInput _vrInput; [Inject] private readonly IVrSfxPlayer _vrSfxPlayer; [Inject] private readonly ConfigService _configService; @@ -368,7 +368,7 @@ private void ChangeWaterBehavior() /// private void SetWaterSpeeds() { - _vrPlayerService.SetHeroSpeedOverride(VrHeroSpeedOverride.ShareOfWalk(VrHeroSpeedSource.Water, + _heroSpeedService.SetHeroSpeedOverride(VrHeroSpeedOverride.ShareOfWalk(VrHeroSpeedSource.Water, VRConstants.Swim.WaterWalkSpeedShare)); } @@ -377,7 +377,7 @@ private void SetWaterSpeeds() /// private void ClearWaterSpeeds() { - _vrPlayerService?.ClearHeroSpeedOverride(VrHeroSpeedSource.Water); + _heroSpeedService?.ClearHeroSpeedOverride(VrHeroSpeedSource.Water); if (_rig != null) _rig.CanSprint = true; } diff --git a/Assets/Gothic-VR/Scripts/Adapters/Player/VrHeroLocomotion.cs b/Assets/Gothic-VR/Scripts/Adapters/Player/VrHeroLocomotion.cs index b5a8143a1..01614b2bc 100644 --- a/Assets/Gothic-VR/Scripts/Adapters/Player/VrHeroLocomotion.cs +++ b/Assets/Gothic-VR/Scripts/Adapters/Player/VrHeroLocomotion.cs @@ -16,13 +16,13 @@ namespace Gothic.VR.Adapters.Player /// player's velocity, the body state, the body's turn speed, crouching and swimming (VrHeroSwimPose) - idle, /// walk/run (played at the real speed - no sliding feet), walking backwards, strafing, turning on the spot, /// falling, sneaking, swimming and diving. Worker of VRHeroBody (new().Inject(), Bind per built body). - /// Publishes the body's walk/run/sneak speeds (its animations at its scale, VRPlayerService.HeroBodySpeeds) - the - /// rig moves at them (VRHeroMovement), so the feet match the ground. + /// Publishes the body's walk/run/sneak speeds (its animations at its scale, VRHeroSpeedService.HeroBodySpeeds) - + /// the rig moves at them (VRHeroMovement), so the feet match the ground. /// public class VrHeroLocomotion { [Inject] private readonly AnimationService _animationService; - [Inject] private readonly VRPlayerService _vrPlayerService; + [Inject] private readonly VRHeroSpeedService _heroSpeedService; private const float _idleSpeed = 0.15f; private const float _turnAnimationSpeed = 60f; // degrees per second @@ -70,7 +70,7 @@ public void Bind(VRHeroBodyAnimator animator) public void ForgetSpeeds() { _speedsScale = -1f; - _vrPlayerService.HeroBodySpeeds = null; + _heroSpeedService.HeroBodySpeeds = null; } /// @@ -103,7 +103,7 @@ public void Update(NpcContainer hero, Transform body, IVrRig rig, float scale, b var animation = _swimPose.GetAnimation(_animator, bodyState, speed, isMoving, isBackwards, isTurning, _bodyYawSpeed < 0f) ?? GetWalkAnimation(rig, velocity, speed, local, isMoving, isBackwards, isTurning, isCrouching, - _vrPlayerService.HeroSpeeds.RunThreshold); + _heroSpeedService.HeroSpeeds.RunThreshold); if (!_animator.HasAnimation(animation)) { @@ -142,7 +142,7 @@ private void UpdateSpeeds(float scale) var run = _animator.GetMovementSpeed(_runAnimation); if (walk <= 0f || run <= 0f) { - _vrPlayerService.HeroBodySpeeds = null; + _heroSpeedService.HeroBodySpeeds = null; if (!_isMissingSpeedLogged) { _isMissingSpeedLogged = true; @@ -159,7 +159,7 @@ private void UpdateSpeeds(float scale) var factor = scale * VRConstants.Movement.WalkRunFactor; var speeds = new VrHeroSpeeds(walk * factor, run * factor, sneak * scale, _animator.IsFromOverlayOverride(_runAnimation)); - _vrPlayerService.HeroBodySpeeds = speeds; + _heroSpeedService.HeroBodySpeeds = speeds; if (isNewOverlay) Logger.Log($"[VRHeroBody] Speeds of {_animator.MdsBase}/{overlay} at scale {scale:F2}: {speeds}" + (speeds.IsSpeedOverlayRun ? " (the overlay's own run)" : ""), LogCat.VR); diff --git a/Assets/Gothic-VR/Scripts/Models/VrHeroSpeedSource.cs b/Assets/Gothic-VR/Scripts/Models/VrHeroSpeedSource.cs index f8b774a84..f0eb96b2d 100644 --- a/Assets/Gothic-VR/Scripts/Models/VrHeroSpeedSource.cs +++ b/Assets/Gothic-VR/Scripts/Models/VrHeroSpeedSource.cs @@ -1,8 +1,9 @@ namespace Gothic.VR.Models { /// - /// Who moves the VR hero at other speeds than his land speeds (VRPlayerService.SetHeroSpeedOverride). The first - /// one set in this order wins. + /// Who moves the VR hero at other speeds than his land speeds (VRHeroSpeedService.SetHeroSpeedOverride). Which one + /// wins while several are set is VRHeroSpeedService's priority list, not this order - a new source needs its place + /// there. /// public enum VrHeroSpeedSource { diff --git a/Assets/Gothic-VR/Scripts/ReflexVRSceneInstaller.cs b/Assets/Gothic-VR/Scripts/ReflexVRSceneInstaller.cs index 879990d38..1a119517e 100644 --- a/Assets/Gothic-VR/Scripts/ReflexVRSceneInstaller.cs +++ b/Assets/Gothic-VR/Scripts/ReflexVRSceneInstaller.cs @@ -25,6 +25,7 @@ public void InstallBindings(ContainerBuilder containerBuilder) containerBuilder.AddSingleton(backend, typeof(IVrBackend)); containerBuilder.AddSingleton(typeof(VRPlayerService)); + containerBuilder.AddSingleton(typeof(VRHeroSpeedService)); containerBuilder.AddSingleton(typeof(VRMobUseService)); containerBuilder.AddSingleton(typeof(VRWeaponService)); containerBuilder.AddSingleton(typeof(VRHeldItemSyncService)); diff --git a/Assets/Gothic-VR/Scripts/Services/VRHeroSpeedService.cs b/Assets/Gothic-VR/Scripts/Services/VRHeroSpeedService.cs new file mode 100644 index 000000000..945806f6c --- /dev/null +++ b/Assets/Gothic-VR/Scripts/Services/VRHeroSpeedService.cs @@ -0,0 +1,119 @@ +using Gothic.Core.Logging; +using Gothic.VR.Models; +using Reflex.Attributes; +using Logger = Gothic.Core.Logging.Logger; + +namespace Gothic.VR.Services +{ + /// + /// The VR hero's speeds: on land the VR hero body's (VrHeroLocomotion) or the rig's own, with the speed potion + /// (VRHeroMovement times it), and who sets other speeds instead (water, a transformation) - which of them wins is + /// _overridePriority. VRHeroMovement makes the rig's speeds of them, it is their only writer. + /// + public class VRHeroSpeedService + { + [Inject] private readonly VRPlayerService _vrPlayerService; + + // Which override counts while several are set: the first one in this list. Water first - a transformed hero + // in the water swims at the water's speed. A new VrHeroSpeedSource needs its place here. + private static readonly VrHeroSpeedSource[] _overridePriority = + { + VrHeroSpeedSource.Water, + VrHeroSpeedSource.Transformation + }; + + // Per entry of _overridePriority, null = that source doesn't set the speeds. + private readonly VrHeroSpeedOverride?[] _overrides = new VrHeroSpeedOverride?[_overridePriority.Length]; + + /// + /// The VR hero body's walk, run and sneak speeds from its animations at its scale (VrHeroLocomotion), null + /// without an animated body. + /// + public VrHeroSpeeds? HeroBodySpeeds { get; set; } + + /// + /// The speed potion works (Mdl_ApplyOverlayMDSTimed HUMANS_SPRINT.MDS) - VRHeroMovement times it. + /// + public bool IsHeroSpeedBoosted { get; set; } + + /// + /// The speeds the hero moves with on land: the VR hero body's, else the rig's own (all 0 before the rig + /// exists). The speed potion runs with its overlay's own run loop, without one walking and running are half + /// again as fast. Their RunThreshold is BS_RUN for the scripts and the body's run animation. + /// + public VrHeroSpeeds HeroSpeeds + { + get + { + var speeds = GetSpeedsWithoutPotion(); + return IsHeroSpeedBoosted && !speeds.IsSpeedOverlayRun ? speeds.WithSpeedPotion() : speeds; + } + } + + /// + /// The speeds instead of the land speeds (HeroSpeeds) - the first source set in _overridePriority (water + /// before a transformation), null on land. + /// + public VrHeroSpeedOverride? HeroSpeedOverride + { + get + { + foreach (var speedOverride in _overrides) + { + if (speedOverride.HasValue) + return speedOverride; + } + return null; + } + } + + /// + /// The override's walking and sprinting speeds (m/s) - a share of the walk is of the land walk (HeroSpeeds). + /// + public (float Move, float Run) GetOverrideSpeeds(in VrHeroSpeedOverride speedOverride) + { + var land = HeroSpeeds; + return (speedOverride.GetMoveSpeed(land), speedOverride.GetRunSpeed(land)); + } + + /// + /// Its source (water, a transformation) moves the hero at these speeds until ClearHeroSpeedOverride - the rig's + /// speeds are VRHeroMovement's alone. + /// + public void SetHeroSpeedOverride(VrHeroSpeedOverride speedOverride) + { + var index = GetPriorityIndex(speedOverride.Source); + if (index >= 0) + _overrides[index] = speedOverride; + } + + public void ClearHeroSpeedOverride(VrHeroSpeedSource source) + { + var index = GetPriorityIndex(source); + if (index >= 0) + _overrides[index] = null; + } + + private VrHeroSpeeds GetSpeedsWithoutPotion() + { + if (HeroBodySpeeds.HasValue) + return HeroBodySpeeds.Value; + var rig = _vrPlayerService.Rig; + return rig == null + ? default + : new VrHeroSpeeds(rig.DefaultMoveSpeed, rig.DefaultRunSpeed, rig.DefaultMoveSpeed); + } + + private static int GetPriorityIndex(VrHeroSpeedSource source) + { + for (var i = 0; i < _overridePriority.Length; i++) + { + if (_overridePriority[i] == source) + return i; + } + Logger.LogError($"[VRHeroSpeedService] {source} has no place in the speed override priority - ignored.", + LogCat.VR); + return -1; + } + } +} diff --git a/Assets/Gothic-VR/Scripts/Services/VRPlayerService.cs b/Assets/Gothic-VR/Scripts/Services/VRPlayerService.cs index 0a3e308ea..1b4e0b2d1 100644 --- a/Assets/Gothic-VR/Scripts/Services/VRPlayerService.cs +++ b/Assets/Gothic-VR/Scripts/Services/VRPlayerService.cs @@ -360,71 +360,6 @@ private void OnPlayerPrefUpdated(string key, object value) /// public bool IsHeroBodyDown { get; set; } - /// - /// The VR hero body's walk, run and sneak speeds from its animations at its scale (VrHeroLocomotion), null - /// without an animated body. VRHeroMovement makes the rig's speeds of them. - /// - public VrHeroSpeeds? HeroBodySpeeds { get; set; } - - /// - /// The speed potion works (Mdl_ApplyOverlayMDSTimed HUMANS_SPRINT.MDS) - VRHeroMovement times it. - /// - public bool IsHeroSpeedBoosted { get; set; } - - /// - /// The speeds the hero moves with on land (GetHeroSpeeds). VRHeroMovement makes the rig's speeds of them, their - /// RunThreshold is BS_RUN for the scripts and the body's run animation. - /// - public VrHeroSpeeds HeroSpeeds => GetHeroSpeeds(); - - /// - /// The VR hero body's speeds, else the rig's own (all 0 before the rig exists). The speed potion runs with its - /// overlay's own run loop, without one walking and running are half again as fast. - /// - private VrHeroSpeeds GetHeroSpeeds() - { - var rig = Rig; - var speeds = HeroBodySpeeds ?? (rig == null - ? default - : new VrHeroSpeeds(rig.DefaultMoveSpeed, rig.DefaultRunSpeed, rig.DefaultMoveSpeed)); - return IsHeroSpeedBoosted && !speeds.IsSpeedOverlayRun ? speeds.WithSpeedPotion() : speeds; - } - - // Per VrHeroSpeedSource (its value is the index), null = that source doesn't set the speeds. - private readonly VrHeroSpeedOverride?[] _heroSpeedOverrides = - new VrHeroSpeedOverride?[Enum.GetValues(typeof(VrHeroSpeedSource)).Length]; - - /// - /// The speeds instead of the land speeds (HeroSpeeds) - the first source set in VrHeroSpeedSource's order - /// (water before a transformation), null on land. VRHeroMovement makes the rig's speeds of it. - /// - public VrHeroSpeedOverride? HeroSpeedOverride - { - get - { - foreach (var speedOverride in _heroSpeedOverrides) - { - if (speedOverride.HasValue) - return speedOverride; - } - return null; - } - } - - /// - /// Its source (water, a transformation) moves the hero at these speeds until ClearHeroSpeedOverride - the rig's - /// speeds are VRHeroMovement's alone. - /// - public void SetHeroSpeedOverride(VrHeroSpeedOverride speedOverride) - { - _heroSpeedOverrides[(int)speedOverride.Source] = speedOverride; - } - - public void ClearHeroSpeedOverride(VrHeroSpeedSource source) - { - _heroSpeedOverrides[(int)source] = null; - } - public GameObject GrabbedItemLeft; public GameObject GrabbedItemRight; diff --git a/Assets/Gothic-VR/Scripts/VRConstants.cs b/Assets/Gothic-VR/Scripts/VRConstants.cs index 1c62bfd3c..42806fd0a 100644 --- a/Assets/Gothic-VR/Scripts/VRConstants.cs +++ b/Assets/Gothic-VR/Scripts/VRConstants.cs @@ -316,7 +316,7 @@ public static class Swim public const float WaterSurfaceRayBuffer = 2f; // VRSwimDive: wading, swimming and diving move (stick and sprint alike) at this share of the hero's land - // walk speed (VRPlayerService.HeroSpeeds.Walk). + // walk speed (VRHeroSpeedService.HeroSpeeds.Walk). public const float WaterWalkSpeedShare = 0.5f; } From f62eeeface4530d71017dc0539e1f85a8ece1c20 Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Sun, 29 Nov 2026 16:28:27 +0000 Subject: [PATCH 18/20] fix: wading and swimming are half the hero's walk without the speed potion - a potion without a run loop of its own (a mod) no longer speeds up the water by half again Co-Authored-By: Claude Opus 5.5 --- Assets/Gothic-VR/Scripts/Adapters/Player/VRSwimDive.cs | 4 ++-- Assets/Gothic-VR/Scripts/Models/VrHeroSpeedOverride.cs | 3 ++- Assets/Gothic-VR/Scripts/Services/VRHeroSpeedService.cs | 6 ++++-- Assets/Gothic-VR/Scripts/VRConstants.cs | 2 +- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/Assets/Gothic-VR/Scripts/Adapters/Player/VRSwimDive.cs b/Assets/Gothic-VR/Scripts/Adapters/Player/VRSwimDive.cs index db2bd610a..0e0804478 100644 --- a/Assets/Gothic-VR/Scripts/Adapters/Player/VRSwimDive.cs +++ b/Assets/Gothic-VR/Scripts/Adapters/Player/VRSwimDive.cs @@ -363,8 +363,8 @@ private void ChangeWaterBehavior() } /// - /// Wading, swimming and diving move at a share of the land walk speed (VrHeroSpeedOverride follows the VR hero - /// body's speeds) - VRHeroMovement sets the rig's speeds of it. + /// Wading, swimming and diving move at a share of the land walk speed without the speed potion + /// (VrHeroSpeedOverride follows the VR hero body's speeds) - VRHeroMovement sets the rig's speeds of it. /// private void SetWaterSpeeds() { diff --git a/Assets/Gothic-VR/Scripts/Models/VrHeroSpeedOverride.cs b/Assets/Gothic-VR/Scripts/Models/VrHeroSpeedOverride.cs index c5b583f2a..817be99d5 100644 --- a/Assets/Gothic-VR/Scripts/Models/VrHeroSpeedOverride.cs +++ b/Assets/Gothic-VR/Scripts/Models/VrHeroSpeedOverride.cs @@ -31,7 +31,8 @@ public static VrHeroSpeedOverride Fixed(VrHeroSpeedSource source, float move, fl } /// - /// Walking and sprinting both at this share of the land walk speed - no sprint. + /// Walking and sprinting both at this share of the land walk speed (without the speed potion, + /// VRHeroSpeedService.GetOverrideSpeeds) - no sprint. /// public static VrHeroSpeedOverride ShareOfWalk(VrHeroSpeedSource source, float share) { diff --git a/Assets/Gothic-VR/Scripts/Services/VRHeroSpeedService.cs b/Assets/Gothic-VR/Scripts/Services/VRHeroSpeedService.cs index 945806f6c..3d6ed4cb6 100644 --- a/Assets/Gothic-VR/Scripts/Services/VRHeroSpeedService.cs +++ b/Assets/Gothic-VR/Scripts/Services/VRHeroSpeedService.cs @@ -68,11 +68,13 @@ public VrHeroSpeedOverride? HeroSpeedOverride } /// - /// The override's walking and sprinting speeds (m/s) - a share of the walk is of the land walk (HeroSpeeds). + /// The override's walking and sprinting speeds (m/s) - a share of the walk is of the land walk without the + /// speed potion: it doesn't speed up wading and swimming (a potion without a run loop of its own sped up the + /// walk). /// public (float Move, float Run) GetOverrideSpeeds(in VrHeroSpeedOverride speedOverride) { - var land = HeroSpeeds; + var land = GetSpeedsWithoutPotion(); return (speedOverride.GetMoveSpeed(land), speedOverride.GetRunSpeed(land)); } diff --git a/Assets/Gothic-VR/Scripts/VRConstants.cs b/Assets/Gothic-VR/Scripts/VRConstants.cs index 42806fd0a..6a206fdfe 100644 --- a/Assets/Gothic-VR/Scripts/VRConstants.cs +++ b/Assets/Gothic-VR/Scripts/VRConstants.cs @@ -316,7 +316,7 @@ public static class Swim public const float WaterSurfaceRayBuffer = 2f; // VRSwimDive: wading, swimming and diving move (stick and sprint alike) at this share of the hero's land - // walk speed (VRHeroSpeedService.HeroSpeeds.Walk). + // walk speed without the speed potion (VRHeroSpeedService.GetOverrideSpeeds). public const float WaterWalkSpeedShare = 0.5f; } From 7e3105f644f8e00a4b06942babde3357be56d41d Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Sun, 29 Nov 2026 16:28:49 +0000 Subject: [PATCH 19/20] docs: the hero speed decision record - the speeds in a service of their own with the water-first order as a written list, the water at half the walk without the speed potion, land speeds and sprint back after a load or world change in the water Co-Authored-By: Claude Opus 5.5 --- Docs/adr/2026-11-29-hero-speed-from-animations.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Docs/adr/2026-11-29-hero-speed-from-animations.md b/Docs/adr/2026-11-29-hero-speed-from-animations.md index 1f110e653..7d72af52b 100644 --- a/Docs/adr/2026-11-29-hero-speed-from-animations.md +++ b/Docs/adr/2026-11-29-hero-speed-from-animations.md @@ -2,7 +2,7 @@ - Date: 2026-11-29 - Replaces: decision 7 of [VR movement, trade and theft](2026-11-03-vr-movement-trade-theft.md) (the speed potion as a factor on one base speed) -- Technical details: VrHeroLocomotion, VRHeroMovement, VRPlayerService.HeroSpeeds and HeroSpeedOverride, VrHeroSpeeds, VrHeroSpeedOverride, IVrRig.DefaultMoveSpeed/DefaultRunSpeed, the factors in VRConstants.Movement and VRConstants.Swim (VR) +- Technical details: VrHeroLocomotion, VRHeroMovement, VRHeroSpeedService (HeroSpeeds, HeroSpeedOverride, its priority list), VrHeroSpeeds, VrHeroSpeedOverride, IVrRig.DefaultMoveSpeed/DefaultRunSpeed, the factors in VRConstants.Movement and VRConstants.Swim (VR) ## Context @@ -20,8 +20,8 @@ In Gothic the speed comes from the animation itself: the hero moves as far as th 4. **Crouched is always sneaking speed,** also without the sneak talent: the body sneaks then, only the scripts see a walking hero, as before. Sprint does nothing while crouched, like in Gothic. 5. **Walking and running are 1.4 times as fast as Gothic's loops, and the loops play 1.4 times as fast.** At 1x the hero felt slow in VR (playtest); the feet still match the ground because the steps speed up by as much. Sneaking keeps the loop's own speed. Not chosen: Gothic's speeds exactly (too slow in VR), faster without faster steps (sliding feet). 6. **Without an animated VR body** (body switched off, an armor that can't be built, a mod without walk or run animations) the hero moves at the VR rig's own speeds (1.5 and 3.5 m/s), with a warning in the log when animations are missing. The rig says what its own speeds are (its configured ones, which setting its speeds never changes). Not chosen: a copy of the numbers in our code, or reading them from the rig once at the start (wrong if water or a transformation changed them first). -7. **One owner of the rig's speeds: VRHeroMovement.** Water and a transformation no longer set the rig's speeds and put back their own copy of the land speeds afterwards. They tell VRHeroMovement which speeds they want instead, and take that back when they end. When both want something, water goes first. The land speeds (the body's or the rig's own, with the speed potion) are worked out in one place. Not chosen: each one setting the rig itself (four copies of the land speeds, the prefab speeds for a moment after leaving the water, and every new speed source would touch all of them). -8. **Wading and swimming move at half the hero's walk speed,** and follow it, so a tall hero wades faster than a short one. Before, it was half the rig's own 1.5 m/s, also when he walked at 1.9 m/s on land. Running is still off in the water. +7. **One owner of the rig's speeds: VRHeroMovement.** Water and a transformation no longer set the rig's speeds and put back their own copy of the land speeds afterwards. They tell VRHeroMovement which speeds they want instead, and take that back when they end. When both want something, water goes first; that order is written down as a list in one place, so adding a third source (a mount, a slowing spell) can't swap it by accident. The land speeds (the body's or the rig's own, with the speed potion) and these wishes are kept in one place of their own, not in the big player service. Not chosen: each one setting the rig itself (four copies of the land speeds, the prefab speeds for a moment after leaving the water, and every new speed source would touch all of them). +8. **Wading and swimming move at half the hero's walk speed without the speed potion,** and follow it, so a tall hero wades faster than a short one. The potion doesn't make him faster in the water, also not a mod's potion that speeds up walking. Before, it was half the rig's own 1.5 m/s, also when he walked at 1.9 m/s on land. Running is still off in the water. ## Consequences @@ -30,4 +30,5 @@ In Gothic the speed comes from the animation itself: the hero moves as far as th - Leaving the water or turning back from a monster, the hero gets his land speeds again at once, without a short moment at the rig's 1.5/3.5 m/s. - A transformed hero moves at the monster's speeds, the speed potion doesn't speed him up. In knee-deep or deeper water the water's speed counts, also for a transformed hero, and also when he transformed while wading (before, the monster's speeds counted then). - Moving sideways while sprinting is faster than Gothic's side step animation (the VR rig has one speed for all directions), so the feet can still slide there. +- Loading a save or changing the world while wading or swimming gives the hero his land speeds and sprint back; the water's speeds come back only if he is in water again. - Nothing is saved: the speeds are read again from the animations. From bac0f407bdf908aed7b3e793727c330641216fd2 Mon Sep 17 00:00:00 2001 From: BoroBongo Date: Sun, 29 Nov 2026 17:13:01 +0000 Subject: [PATCH 20/20] chore: meta files of VRHeroSpeedService and its test (generated by Unity on import) Co-Authored-By: Claude Opus 5.5 --- Assets/Gothic-Tests/EditMode/VR/VRHeroSpeedServiceTest.cs.meta | 2 ++ Assets/Gothic-VR/Scripts/Services/VRHeroSpeedService.cs.meta | 2 ++ 2 files changed, 4 insertions(+) create mode 100644 Assets/Gothic-Tests/EditMode/VR/VRHeroSpeedServiceTest.cs.meta create mode 100644 Assets/Gothic-VR/Scripts/Services/VRHeroSpeedService.cs.meta diff --git a/Assets/Gothic-Tests/EditMode/VR/VRHeroSpeedServiceTest.cs.meta b/Assets/Gothic-Tests/EditMode/VR/VRHeroSpeedServiceTest.cs.meta new file mode 100644 index 000000000..973cdf198 --- /dev/null +++ b/Assets/Gothic-Tests/EditMode/VR/VRHeroSpeedServiceTest.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 9280948cdc55a8a449a19a5374a8cb89 \ No newline at end of file diff --git a/Assets/Gothic-VR/Scripts/Services/VRHeroSpeedService.cs.meta b/Assets/Gothic-VR/Scripts/Services/VRHeroSpeedService.cs.meta new file mode 100644 index 000000000..671fdac53 --- /dev/null +++ b/Assets/Gothic-VR/Scripts/Services/VRHeroSpeedService.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 0604ef113e6db0543a7cdffc77c90386 \ No newline at end of file