From 61467f78d2af9a5eb342a313cf8af41b2cf18efe Mon Sep 17 00:00:00 2001 From: Scove Date: Sat, 2 May 2026 21:26:42 +0700 Subject: [PATCH] feat: refactor BallShooter to support dynamic flick physics --- Assets/Script/BallShooter.cs | 38 +++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/Assets/Script/BallShooter.cs b/Assets/Script/BallShooter.cs index e7d6d79..68f5ef4 100644 --- a/Assets/Script/BallShooter.cs +++ b/Assets/Script/BallShooter.cs @@ -4,19 +4,39 @@ public class BallShooter : MonoBehaviour { public GameObject ballPrefab; // Kéo prefab quả bóng vào đây public Transform shootPoint; // Kéo điểm ShootPoint vào đây - public float shootForce = 500f; - public float upwardForce = 200f; // Lực ném vòng cung lên trên - // Gọi hàm này khi bấm nút Ném trên UI - public void ShootBall() + [Header("Flick Settings")] + public float forwardMultiplier = 0.05f; + public float arcMultiplier = 0.02f; + public float lateralMultiplier = 0.03f; + public float maxVelocity = 100f; + + // Thay thế logic Ném cũ bằng logic Flick mới + public void FlickShoot(Vector2 swipeDelta, float swipeTime) { + // Tính vận tốc (pixels trên giây) + Vector2 swipeVelocity = swipeDelta / swipeTime; + + // Giới hạn để tránh "nổ" vật lý nếu flick quá nhanh + if (swipeVelocity.magnitude > maxVelocity) + { + swipeVelocity = swipeVelocity.normalized * maxVelocity; + } + GameObject newBall = Instantiate(ballPrefab, shootPoint.position, shootPoint.rotation); Rigidbody rb = newBall.GetComponent(); - - // Thêm lực để quả bóng bay về phía trước và hơi hếch lên trên - rb.AddForce(shootPoint.forward * shootForce + Vector3.up * upwardForce); - - // Hủy quả bóng sau 5 giây để tránh lag game + + // Chuyển đổi swipe 2D sang lực 3D + // Y swipe (lên/xuống) -> Tiến và Lên + // X swipe (trái/phải) -> Sang ngang + Vector3 force = (shootPoint.forward * swipeVelocity.y * forwardMultiplier) + + (Vector3.up * swipeVelocity.y * arcMultiplier) + + (shootPoint.right * swipeVelocity.x * lateralMultiplier); + + // Sử dụng Impulse để lực tác động tức thì + rb.AddForce(force, ForceMode.Impulse); + + // Hủy quả bóng sau 5 giây Destroy(newBall, 5f); } } \ No newline at end of file