Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
11b2aa6
fix: the VR hero body knows how fast its own walk, run and sneak step…
BoroBongo Nov 29, 2026
988beb1
fix: the VR hero's feet match the ground - the stick walks and sprint…
BoroBongo Nov 29, 2026
d89c8a9
docs: ADR - the VR hero moves at the speed of his own walk, run and s…
BoroBongo Nov 29, 2026
22feca2
chore: meta file of VrHeroSpeeds (generated by Unity on import)
BoroBongo Nov 29, 2026
bec6969
fix: the VR hero walks and runs 1.4 times as fast as Gothic's loops, …
BoroBongo Nov 29, 2026
c83fb3b
fix: the VR hero's speeds are read again only when the body's scale c…
BoroBongo Nov 29, 2026
2fbde1a
fix: the VR hero's speeds compare the overlay by its name, not its st…
BoroBongo Nov 29, 2026
0ff74aa
style: sort the usings of VrHeroLocomotion and say why BS_WALK starts…
BoroBongo Nov 29, 2026
0a2cd36
refactor: one rule for "the hero sneaks" (IsCrouchSneaking) for the s…
BoroBongo Nov 29, 2026
ffb6e38
refactor: the VR hero's walk/run factor and the speed potion's factor…
BoroBongo Nov 29, 2026
dcd40db
refactor: the hero's land speeds are derived in one place (VRPlayerSe…
BoroBongo Nov 29, 2026
5c22ec9
refactor: VRHeroMovement is the only writer of the rig's speeds - wat…
BoroBongo Nov 29, 2026
4897509
docs: the hero speed decision record - VRHeroMovement owns the rig's …
BoroBongo Nov 29, 2026
d932053
chore: meta files of VrHeroSpeedOverride and VrHeroSpeedSource (gener…
BoroBongo Nov 29, 2026
982b0d4
fix: the water speeds end with the world and with VRSwimDive - a load…
BoroBongo Nov 29, 2026
3d94d31
docs: the VR hero body's walk/run loops play about 1.4 times as fast …
BoroBongo Nov 29, 2026
3b663f4
refactor: the VR hero's speeds have their own service (VRHeroSpeedSer…
BoroBongo Nov 29, 2026
f62eeef
fix: wading and swimming are half the hero's walk without the speed p…
BoroBongo Nov 29, 2026
7e3105f
docs: the hero speed decision record - the speeds in a service of the…
BoroBongo Nov 29, 2026
bac0f40
chore: meta files of VRHeroSpeedService and its test (generated by Un…
BoroBongo Nov 29, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions Assets/Gothic-Tests/EditMode/VR/VRHeroSpeedServiceTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using Gothic.VR.Models;
using Gothic.VR.Services;
using NUnit.Framework;

namespace Gothic.Tests.EditMode.VR
{
/// <summary>
/// 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.
/// </summary>
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);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Assets/Gothic-Tests/PlayMode/VrPhysicsHandTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IVrGrabber> HandGrabbers => Array.Empty<IVrGrabber>();
Expand Down
4 changes: 4 additions & 0 deletions Assets/Gothic-VR/Scripts/Adapters/Player/VRHeroBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ private void OnDestroy()
_hands.RestoreHeldItemAttachPoints();
_meshCutter.DestroyCreatedMeshes();
_hands.Dispose();
_locomotion.ForgetSpeeds();
if (_rig != null)
_rig.CrouchHeightOverride = null;
}
Expand All @@ -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)
Expand Down Expand Up @@ -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;
}

Expand Down
10 changes: 10 additions & 0 deletions Assets/Gothic-VR/Scripts/Adapters/Player/VRHeroBodyAnimator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@ public float GetMovementSpeed(string animationName)
return track != null && track.IsMoving ? track.MovementSpeed.magnitude : 0f;
}

/// <summary>
/// The animation comes from the timed overlay (OverlayOverride, e.g. the speed potion's s_RunL), not from the
/// hero's own MDS.
/// </summary>
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)
Expand Down
135 changes: 66 additions & 69 deletions Assets/Gothic-VR/Scripts/Adapters/Player/VRHeroMovement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,15 +17,17 @@ namespace Gothic.VR.Adapters.Player
{
/// <summary>
/// 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).
/// </summary>
public class VRHeroMovement : MonoBehaviour
{
[Inject] private readonly GameStateService _gameStateService;
[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_<ground> 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).
Expand All @@ -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";
Expand Down Expand Up @@ -94,66 +88,74 @@ private void OnDestroy()
{
GlobalEventDispatcher.ScriptHeroOverlayTimed.RemoveListener(OnHeroOverlayTimed);
SpeedOverlay = null;
_heroSpeedService.IsHeroSpeedBoosted = false;
}

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;
}

/// <summary>
/// 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.
/// </summary>
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);
}

/// <summary>
/// The base speeds times the potion (move and run); sneaking caps the run speed at the move speed.
/// </summary>
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;
/// <summary>
/// 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.
/// </summary>
private bool IsCrouchSneaking()
{
return _rig.IsCrouching && !_vrPlayerService.IsHeroBodyDown;
}

private void OnHeroOverlayTimed(string overlayName, float seconds)
Expand All @@ -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();
}

/// <summary>
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down
Loading