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-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-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/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/VRHeroMovement.cs b/Assets/Gothic-VR/Scripts/Adapters/Player/VRHeroMovement.cs
index 4672d4360..942f1f9e4 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 - their only writer: on land Gothic's walk/run/sneak loops at the VR
+ /// 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
{
@@ -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 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).
@@ -39,32 +42,23 @@ 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).
+ // 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 _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;
+ // 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";
@@ -94,6 +88,7 @@ private void OnDestroy()
{
GlobalEventDispatcher.ScriptHeroOverlayTimed.RemoveListener(OnHeroOverlayTimed);
SpeedOverlay = null;
+ _heroSpeedService.IsHeroSpeedBoosted = false;
}
private void Update()
@@ -101,59 +96,66 @@ 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.
+ /// 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 (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
+ /// (VRHeroSpeedService.HeroSpeedOverride) - the land speeds come back the frame they let go.
///
- private void UpdateSneakSpeed()
+ private void UpdateSpeeds()
{
- _isSneaking = IsHeroSneaking();
- ApplySpeeds();
- }
+ var bodySpeeds = _heroSpeedService.HeroBodySpeeds;
+ var speeds = _heroSpeedService.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 ({speeds}).", LogCat.VR);
+ }
- ///
- /// The base speeds times the potion (move and run); sneaking caps the run speed at the move speed.
- ///
- private void ApplySpeeds()
- {
- var isModified = _isSpeedBoosted || _isSneaking;
- if (!_isSpeedModified && !isModified)
- return;
+ var speedOverride = _heroSpeedService.HeroSpeedOverride;
+ if (speedOverride?.Source != _speedOverrideSource)
+ {
+ _speedOverrideSource = speedOverride?.Source;
+ Logger.Log(speedOverride.HasValue
+ ? $"[VRHeroMovement] Speeds of {speedOverride.Value}."
+ : $"[VRHeroMovement] Land speeds again ({speeds}).", LogCat.VR);
+ }
- if (!_isSpeedModified)
+ float moveSpeed;
+ float runSpeed;
+ if (speedOverride.HasValue)
{
- _baseMoveSpeed = _rig.MoveSpeed;
- _baseRunSpeed = _rig.RunSpeed;
+ (moveSpeed, runSpeed) = _heroSpeedService.GetOverrideSpeeds(speedOverride.Value);
}
- else if (!Mathf.Approximately(_rig.MoveSpeed, _appliedMoveSpeed) ||
- !Mathf.Approximately(_rig.RunSpeed, _appliedRunSpeed))
+ else
{
- // 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;
+ 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))
+ _rig.RunSpeed = runSpeed;
+ }
- var boost = _isSpeedBoosted ? _sprintSpeedFactor : 1f;
- _appliedMoveSpeed = _baseMoveSpeed * boost;
- _appliedRunSpeed = _isSneaking ? _appliedMoveSpeed : _baseRunSpeed * boost;
- _rig.MoveSpeed = _appliedMoveSpeed;
- _rig.RunSpeed = _appliedRunSpeed;
- _isSpeedModified = isModified;
+ ///
+ /// 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)
@@ -163,20 +165,18 @@ private void OnHeroOverlayTimed(string overlayName, float seconds)
return;
_speedOverlay = overlayName;
- _isSpeedBoosted = true;
- ApplySpeeds();
+ _heroSpeedService.IsHeroSpeedBoosted = true;
_speedBoostEndTime = Time.time + seconds;
SpeedOverlay = _speedOverlay;
}
private void UpdateSpeedBoost()
{
- if (!_isSpeedBoosted || Time.time < _speedBoostEndTime)
+ if (!_heroSpeedService.IsHeroSpeedBoosted || Time.time < _speedBoostEndTime)
return;
- _isSpeedBoosted = false;
+ _heroSpeedService.IsHeroSpeedBoosted = false;
SpeedOverlay = null;
- ApplySpeeds();
}
///
@@ -206,8 +206,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;
@@ -216,13 +215,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 >= _heroSpeedService.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/VRSpellTransformPuppet.cs b/Assets/Gothic-VR/Scripts/Adapters/Player/VRSpellTransformPuppet.cs
index d7199a9f8..377eacefe 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;
@@ -33,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;
@@ -94,13 +96,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 +164,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).
+ _heroSpeedService.ClearHeroSpeedOverride(VrHeroSpeedSource.Transformation);
if (_isReady)
_rig?.SetGrabbingEnabled(true);
@@ -215,7 +208,6 @@ private void Update()
aiHandler.enabled = false;
_monster.Props.AnimationQueue.Clear();
- KeepMonsterSpeed();
UpdateAnimation();
UpdateAttack();
UpdateTransformBackInput();
@@ -724,13 +716,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 +738,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;
+ _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 929752dbe..0e0804478 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,8 +47,6 @@ public class VRSwimDive : MonoBehaviour, IMarvinPropertyCollector
// private string _diveBubbleParticleName;
private float _initialGravity;
- private float _initialMoveSpeed;
- private float _initialRunSpeed;
private float _initialFallSpeed;
private VmGothicEnums.WalkMode _mode = VmGothicEnums.WalkMode.Walk;
@@ -92,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 VRHeroSpeedService _heroSpeedService;
[Inject] private readonly IVrInput _vrInput;
[Inject] private readonly IVrSfxPlayer _vrSfxPlayer;
[Inject] private readonly ConfigService _configService;
@@ -157,25 +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;
- _initialMoveSpeed = _rig.MoveSpeed;
- _initialRunSpeed = _rig.RunSpeed;
_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)
@@ -289,9 +300,7 @@ private void ChangeWaterBehavior()
Shader.SetGlobalInt(Constants.ShaderPropertyWaterEffectToggle, 0);
_rig.Gravity = _initialGravity;
- _rig.MoveSpeed = _initialMoveSpeed;
- _rig.RunSpeed = _initialRunSpeed;
- _rig.CanSprint = true;
+ ClearWaterSpeeds();
_rig.MaxFallSpeed = _initialFallSpeed;
_contextInteractionService.SetWalkingControls();
@@ -314,8 +323,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;
@@ -338,6 +347,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();
@@ -351,6 +362,26 @@ private void ChangeWaterBehavior()
UpdateHeroBodyState();
}
+ ///
+ /// 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()
+ {
+ _heroSpeedService.SetHeroSpeedOverride(VrHeroSpeedOverride.ShareOfWalk(VrHeroSpeedSource.Water,
+ VRConstants.Swim.WaterWalkSpeedShare));
+ }
+
+ ///
+ /// The land speeds again (VRHeroMovement) and sprint back.
+ ///
+ private void ClearWaterSpeeds()
+ {
+ _heroSpeedService?.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, ...
diff --git a/Assets/Gothic-VR/Scripts/Adapters/Player/VrHeroLocomotion.cs b/Assets/Gothic-VR/Scripts/Adapters/Player/VrHeroLocomotion.cs
index 88ec2684c..01614b2bc 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.Logging;
using Gothic.Core.Models.Container;
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, 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 VRHeroSpeedService _heroSpeedService;
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,14 @@ 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 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;
+
///
/// The animator of a newly built body.
///
@@ -45,6 +60,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;
+ _heroSpeedService.HeroBodySpeeds = null;
}
///
@@ -57,6 +83,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 +102,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,
+ _heroSpeedService.HeroSpeeds.RunThreshold);
if (!_animator.HasAnimation(animation))
{
@@ -83,7 +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.
+ // 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)
@@ -94,6 +124,47 @@ 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 = !string.Equals(overlay, _speedsOverlay) || _speedsScale < 0f;
+ if (!isNewOverlay && Mathf.Abs(scale - _speedsScale) <= _speedsScale * _speedsScaleTolerance)
+ return;
+ _speedsOverlay = overlay;
+ _speedsScale = scale;
+
+ var walk = _animator.GetMovementSpeed(_walkAnimation);
+ var run = _animator.GetMovementSpeed(_runAnimation);
+ if (walk <= 0f || run <= 0f)
+ {
+ _heroSpeedService.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 factor = scale * VRConstants.Movement.WalkRunFactor;
+ var speeds = new VrHeroSpeeds(walk * factor, run * factor, sneak * scale,
+ _animator.IsFromOverlayOverride(_runAnimation));
+ _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);
+ }
+
///
/// 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 +189,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 +207,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/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/VrHeroSpeedOverride.cs b/Assets/Gothic-VR/Scripts/Models/VrHeroSpeedOverride.cs
new file mode 100644
index 000000000..817be99d5
--- /dev/null
+++ b/Assets/Gothic-VR/Scripts/Models/VrHeroSpeedOverride.cs
@@ -0,0 +1,59 @@
+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 (without the speed potion,
+ /// VRHeroSpeedService.GetOverrideSpeeds) - 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/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 b/Assets/Gothic-VR/Scripts/Models/VrHeroSpeedSource.cs
new file mode 100644
index 000000000..f0eb96b2d
--- /dev/null
+++ b/Assets/Gothic-VR/Scripts/Models/VrHeroSpeedSource.cs
@@ -0,0 +1,16 @@
+namespace Gothic.VR.Models
+{
+ ///
+ /// 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
+ {
+ // Wading, swimming or diving (VRSwimDive).
+ Water,
+
+ // Transformed into a monster (VRSpellTransformPuppet): its own speeds.
+ Transformation
+ }
+}
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
diff --git a/Assets/Gothic-VR/Scripts/Models/VrHeroSpeeds.cs b/Assets/Gothic-VR/Scripts/Models/VrHeroSpeeds.cs
new file mode 100644
index 000000000..96c14ea52
--- /dev/null
+++ b/Assets/Gothic-VR/Scripts/Models/VrHeroSpeeds.cs
@@ -0,0 +1,47 @@
+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
+ {
+ 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()
+ {
+ var factor = VRConstants.Movement.SpeedPotionFactor;
+ return new VrHeroSpeeds(Walk * factor, Run * factor, 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/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
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..3d6ed4cb6
--- /dev/null
+++ b/Assets/Gothic-VR/Scripts/Services/VRHeroSpeedService.cs
@@ -0,0 +1,121 @@
+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 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 = GetSpeedsWithoutPotion();
+ 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/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
diff --git a/Assets/Gothic-VR/Scripts/VRConstants.cs b/Assets/Gothic-VR/Scripts/VRConstants.cs
index ce02d6b1b..6a206fdfe 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,24 @@ 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 without the speed potion (VRHeroSpeedService.GetOverrideSpeeds).
+ public const float WaterWalkSpeedShare = 0.5f;
+ }
+
+ ///
+ /// 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;
}
///
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..7d72af52b
--- /dev/null
+++ b/Docs/adr/2026-11-29-hero-speed-from-animations.md
@@ -0,0 +1,34 @@
+# 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, VRHeroSpeedService (HeroSpeeds, HeroSpeedOverride, its priority list), VrHeroSpeeds, VrHeroSpeedOverride, IVrRig.DefaultMoveSpeed/DefaultRunSpeed, the factors in VRConstants.Movement and VRConstants.Swim (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. **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; 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
+
+- 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.
+- 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.