From 163939dab8cb1bb12acf55e5bb1b20bdeab5d8ae Mon Sep 17 00:00:00 2001 From: Caleb Date: Thu, 1 Oct 2026 09:34:10 +0100 Subject: [PATCH] fixed bow cobat, still not 100% on accuracy --- Assets/Scripts/BowAimLayerController.cs | 72 ++++++++++++++++--------- Assets/Scripts/Projectile.cs | 2 + 2 files changed, 49 insertions(+), 25 deletions(-) diff --git a/Assets/Scripts/BowAimLayerController.cs b/Assets/Scripts/BowAimLayerController.cs index 895b44cf..535af3a0 100644 --- a/Assets/Scripts/BowAimLayerController.cs +++ b/Assets/Scripts/BowAimLayerController.cs @@ -323,20 +323,51 @@ public class BowAimLayerController : MonoBehaviour // Laser Sight Gizmo if (_isAiming && _arrowSpawnPoint != null && _camera != null) { - Vector3 aimPoint; - Ray ray = _camera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0)); - if (Physics.Raycast(ray, out RaycastHit hit, 100f, _targetLayers)) - { - aimPoint = hit.point; - } - else - { - aimPoint = ray.GetPoint(100f); - } + Vector3 aimPoint = GetCrosshairAimPoint(); Debug.DrawLine(_arrowSpawnPoint.position, aimPoint, Color.red); } } + private Vector3 GetCrosshairAimPoint() + { + if (_camera == null) return transform.position + transform.forward * 100f; + + Ray ray = _camera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0)); + + // EXTREMELY IMPORTANT: Ignore triggers! Otherwise we might hit invisible lock-on spheres or interact zones! + RaycastHit[] hits = Physics.RaycastAll(ray, 100f, _targetLayers, QueryTriggerInteraction.Ignore); + + float closestDistance = float.MaxValue; + Vector3 aimPoint = ray.GetPoint(100f); + + foreach (var hit in hits) + { + // IGNORE THE PLAYER! If the raycast hits our own body, skip it! + if (hit.collider.transform.root == transform.root) continue; + + // IGNORE ARROWS! + if (hit.collider.GetComponent() != null) continue; + + if (hit.distance < closestDistance) + { + closestDistance = hit.distance; + aimPoint = hit.point; + } + } + + // PARALLAX FIX: + // Because the camera is offset over the right shoulder, if you aim at something very close (like 1m away), + // the arrow (in the left hand) has to violently turn sideways to cross the screen and hit that close point. + // By forcing the aim point to be at least 10 meters away along the same sightline, + // we guarantee the arrow always fires mostly straight forward, completely eliminating wild sideways shots! + if (closestDistance < 4f) + { + aimPoint = ray.GetPoint(15f); + } + + return aimPoint; + } + private void UpdateDrawAndFire() { // The Synty InputReader doesn't have an attack action, so we read @@ -453,31 +484,22 @@ public class BowAimLayerController : MonoBehaviour { // Auto aim directly at the locked on target's chest! aimPoint = playerController.CurrentLockOnTarget.transform.position + (Vector3.up * 1.2f); - } - else if (_camera != null) - { - Ray ray = _camera.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0)); - - if (Physics.Raycast(ray, out RaycastHit hit, 100f, _targetLayers)) - { - aimPoint = hit.point; - } - else - { - // If we are looking into the sky, aim far out into the distance - aimPoint = ray.GetPoint(100f); - } + Debug.Log($"[Bow Debug] Locked on! Auto-aiming at: {aimPoint}"); } else { - aimPoint = _arrowSpawnPoint.position + transform.forward * 100f; + aimPoint = GetCrosshairAimPoint(); + Debug.Log($"[Bow Debug] Calculated AimPoint from Crosshair: {aimPoint}"); } // 2. Unparent the arrow from the hand/string _loadedArrow.transform.SetParent(null); + Debug.Log($"[Bow Debug] Arrow physically starts at: {_loadedArrow.transform.position}. Aim Point is at: {aimPoint}."); + // 3. Point the arrow exactly at the target _loadedArrow.transform.LookAt(aimPoint); + Debug.Log($"[Bow Debug] After LookAt, arrow is rotated to: {_loadedArrow.transform.eulerAngles}."); // 4. Get the damage from the current weapon (default to 10 if none) float damage = _currentWeapon != null ? _currentWeapon.damage : 10f; diff --git a/Assets/Scripts/Projectile.cs b/Assets/Scripts/Projectile.cs index 10b113ba..1ca737ce 100644 --- a/Assets/Scripts/Projectile.cs +++ b/Assets/Scripts/Projectile.cs @@ -40,6 +40,8 @@ public class Projectile : MonoBehaviour Destroy(gameObject, lifetime); _startPosition = transform.position; + + Debug.Log($"[Projectile Debug] Initialized! Final Start Pos: {_startPosition}. Forward Vector: {transform.forward}. BaseSpeed: {speed}. PowerMult: {powerMultiplier:F2}. Final Velocity Magnitude: {_velocity.magnitude:F2}"); } private void Start()