11using Basis . Scripts . BasisSdk . Interactions ;
2+ using Basis . Scripts . BasisSdk . Players ;
23using Basis . Scripts . Device_Management . Devices ;
34using Basis . Scripts . Drivers ;
45using System . Collections . Generic ;
@@ -10,7 +11,9 @@ namespace Basis.Scripts.UI
1011{
1112 public class BasisPointRaycaster : BaseRaycaster
1213 {
14+ private const float MaxDistanceScaleThresholdMeters = 100f ;
1315 public float MaxDistance = 120 ;
16+ public float EffectiveMaxDistance = 120 ;
1417 public bool UseWorldPosition = true ;
1518
1619 /// <summary>
@@ -122,11 +125,30 @@ public void Initialize(BasisInput basisInput)
122125 BasisInput = basisInput ;
123126 PhysicHits = new RaycastHit [ BasisPlayerInteract . k_MaxPhysicHitCount ] ;
124127 PhysicBackcastHits = new RaycastHit [ 4 ] ; // We don't need as many backcast hits.
128+ RefreshEffectiveMaxDistance ( ) ;
129+ BasisLocalPlayer . OnPlayersHeightChangedNextFrame -= OnPlayersHeightChangedNextFrame ;
130+ BasisLocalPlayer . OnPlayersHeightChangedNextFrame += OnPlayersHeightChangedNextFrame ;
125131
126132 // Create the ray with the adjusted starting position and direction
127133 UpdateRay ( ) ;
128134 }
129135
136+ protected override void OnDestroy ( )
137+ {
138+ base . OnDestroy ( ) ;
139+ BasisLocalPlayer . OnPlayersHeightChangedNextFrame -= OnPlayersHeightChangedNextFrame ;
140+ }
141+
142+ private void OnPlayersHeightChangedNextFrame ( BasisHeightDriver . HeightModeChange _ )
143+ {
144+ RefreshEffectiveMaxDistance ( ) ;
145+ }
146+
147+ private void RefreshEffectiveMaxDistance ( )
148+ {
149+ EffectiveMaxDistance = MaxDistance + Mathf . Max ( 0f , BasisHeightDriver . SelectedScaledAvatarHeight - MaxDistanceScaleThresholdMeters ) ;
150+ }
151+
130152 public void UpdateRay ( )
131153 {
132154 ray = GetUpdatedRay ( ) ;
@@ -153,7 +175,7 @@ public void UpdateRaycast()
153175 PhysicHitCount = Physics . RaycastNonAlloc (
154176 ray ,
155177 PhysicHits ,
156- MaxDistance ,
178+ EffectiveMaxDistance ,
157179 BasisPlayerInteract . Mask ,
158180 BasisPlayerInteract . TriggerInteraction ) ;
159181
@@ -243,7 +265,7 @@ private void UpdateClosestHitPreferOverlayUI()
243265
244266 private void DoBackcastFixup ( )
245267 {
246- float backcastDistance = ClosestRayCastHit . distance > 0 ? ClosestRayCastHit . distance : MaxDistance ;
268+ float backcastDistance = ClosestRayCastHit . distance > 0 ? ClosestRayCastHit . distance : EffectiveMaxDistance ;
247269 Ray backcastRay = new Ray ( ray . origin + ray . direction * backcastDistance , - ray . direction ) ;
248270
249271 int backcastHitCount = Physics . RaycastNonAlloc (
@@ -469,7 +491,7 @@ private void OnDrawGizmosSelected()
469491 return ;
470492
471493 Gizmos . color = Color . cyan ;
472- Gizmos . DrawLine ( ray . origin , ray . origin + ray . direction * MaxDistance ) ;
494+ Gizmos . DrawLine ( ray . origin , ray . origin + ray . direction * EffectiveMaxDistance ) ;
473495
474496 if ( _mode == ControlMode . Placement && CurrentPlacement . HasHit )
475497 {
0 commit comments