Fixed the bug

This commit is contained in:
Hazim Bin Ijaz 2025-09-27 01:18:43 +05:00
parent 10a11d5eab
commit 56c8e735bd

View File

@ -56,6 +56,7 @@ namespace Projectiles
private NetworkMecanimAnimator _netAnim; private NetworkMecanimAnimator _netAnim;
private static readonly int HashMoveX = Animator.StringToHash("InputX"); // or "MoveX" private static readonly int HashMoveX = Animator.StringToHash("InputX"); // or "MoveX"
private static readonly int HashMoveY = Animator.StringToHash("InputY"); // or "MoveY" private static readonly int HashMoveY = Animator.StringToHash("InputY"); // or "MoveY"
[Header("Character Facing")] [Header("Character Facing")]
[SerializeField] private float _turnSpeed = 720f; // deg/sec turning toward desired facing [SerializeField] private float _turnSpeed = 720f; // deg/sec turning toward desired facing
[SerializeField] private bool _faceMoveDirection = true; // if false, faces camera forward (aiming) [SerializeField] private bool _faceMoveDirection = true; // if false, faces camera forward (aiming)
@ -93,7 +94,6 @@ namespace Projectiles
ProcessMovementInput(); ProcessMovementInput();
} }
// REMOVED: Don't set camera pivot here - it conflicts with LateUpdate
// Store the look rotation for LateUpdate to use // Store the look rotation for LateUpdate to use
_lastFUNLookRotation = KCC.GetLookRotation(); _lastFUNLookRotation = KCC.GetLookRotation();
} }
@ -195,12 +195,17 @@ namespace Projectiles
Vector3 inputDir = (camForward * input.MoveDirection.y + camRight * input.MoveDirection.x); Vector3 inputDir = (camForward * input.MoveDirection.y + camRight * input.MoveDirection.x);
inputDir.y = 0f; inputDir.y = 0f;
if (inputDir.sqrMagnitude > 1f) inputDir.Normalize(); if (inputDir.sqrMagnitude > 1f) inputDir.Normalize();
// Animation blendtree values
float moveX = Vector3.Dot(inputDir, camRight); float moveX = Vector3.Dot(inputDir, camRight);
float moveY = Vector3.Dot(inputDir, camForward); float moveY = Vector3.Dot(inputDir, camForward);
const float damp = 0.1f; // seconds to smooth toward target const float damp = 0.1f; // seconds to smooth toward target
_anim.SetFloat(HashMoveX, moveX, damp, Time.deltaTime); if (_anim != null)
_anim.SetFloat(HashMoveY, moveY, damp, Time.deltaTime); {
_anim.SetFloat(HashMoveX, moveX, damp, Time.deltaTime);
_anim.SetFloat(HashMoveY, moveY, damp, Time.deltaTime);
}
// Desired velocity and acceleration/deceleration // Desired velocity and acceleration/deceleration
Vector3 desiredVel = inputDir * _moveSpeed; Vector3 desiredVel = inputDir * _moveSpeed;
@ -213,6 +218,7 @@ namespace Projectiles
_moveVelocity = Vector3.Lerp(_moveVelocity, desiredVel, accel * Runner.DeltaTime); _moveVelocity = Vector3.Lerp(_moveVelocity, desiredVel, accel * Runner.DeltaTime);
// 4) Character facing: toward move dir OR toward camera forward (aim) // 4) Character facing: toward move dir OR toward camera forward (aim)
// FIXED: Use transform.rotation instead of interfering with KCC look rotation
Vector3 faceDir = _faceMoveDirection Vector3 faceDir = _faceMoveDirection
? (_moveVelocity.sqrMagnitude > 0.01f ? _moveVelocity : (yawRot * Vector3.forward)) ? (_moveVelocity.sqrMagnitude > 0.01f ? _moveVelocity : (yawRot * Vector3.forward))
: (yawRot * Vector3.forward); : (yawRot * Vector3.forward);
@ -220,12 +226,9 @@ namespace Projectiles
if (faceDir.sqrMagnitude > 0.0001f) if (faceDir.sqrMagnitude > 0.0001f)
{ {
Quaternion target = Quaternion.LookRotation(faceDir, Vector3.up); Quaternion target = Quaternion.LookRotation(faceDir, Vector3.up);
// Use KCC for body yaw rotation, but only set the Y (yaw) component // Use transform rotation for character body - this keeps input and body rotation separate
// This keeps the body rotation networked via KCC Quaternion newRot = Quaternion.RotateTowards(transform.rotation, target, _turnSpeed * Runner.DeltaTime);
Vector2 currentLook = KCC.GetLookRotation(); transform.rotation = newRot;
float targetYaw = target.eulerAngles.y;
float newYaw = Mathf.MoveTowardsAngle(currentLook.y, targetYaw, _turnSpeed * Runner.DeltaTime);
KCC.SetLookRotation(new Vector2(currentLook.x, newYaw), _minPitch, _maxPitch);
} }
// 5) Jump // 5) Jump