This commit is contained in:
2026-05-02 21:36:33 +07:00
parent 61467f78d2
commit ed6612affb
11 changed files with 364 additions and 251 deletions

View File

@@ -2,23 +2,40 @@ using UnityEngine;
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 GameObject ballPrefab;
public Transform shootPoint;
[Header("Flick Settings")]
public float forwardMultiplier = 0.05f;
public float arcMultiplier = 0.02f;
public float lateralMultiplier = 0.03f;
public float maxVelocity = 100f;
[Header("Flick Sensitivity")]
[Tooltip("How much the swipe affects forward speed.")]
public float forwardSensitivity = 0.05f;
[Tooltip("How much the swipe affects upward loft.")]
public float upwardSensitivity = 0.02f;
[Tooltip("How much side-to-side swipe affects lateral direction.")]
public float lateralSensitivity = 0.03f;
[Header("Physics Tuning")]
[Tooltip("Minimum swipe velocity required to trigger a shot.")]
public float swipeThreshold = 100f;
[Tooltip("Maximum velocity allowed (prevents physics glitches).")]
public float maxVelocity = 1500f;
[Tooltip("Constant upward force added to every shot for a better arc.")]
public float baseUpwardBias = 2f;
// 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)
// Calculate velocity (pixels per second)
Vector2 swipeVelocity = swipeDelta / swipeTime;
float speed = swipeVelocity.magnitude;
// Giới hạn để tránh "nổ" vật lý nếu flick quá nhanh
if (swipeVelocity.magnitude > maxVelocity)
// 1. Deadzone check: Ignore tiny accidental flicks
if (speed < swipeThreshold) return;
// 2. Clamp: Prevent extreme speeds
if (speed > maxVelocity)
{
swipeVelocity = swipeVelocity.normalized * maxVelocity;
}
@@ -26,17 +43,21 @@ public class BallShooter : MonoBehaviour
GameObject newBall = Instantiate(ballPrefab, shootPoint.position, shootPoint.rotation);
Rigidbody rb = newBall.GetComponent<Rigidbody>();
// 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);
// 3. Translate 2D swipe to 3D force
// swipeVelocity.y is vertical (up is positive)
// swipeVelocity.x is horizontal (right is positive)
// Sử dụng Impulse để lực tác động tức thì
float forwardForce = swipeVelocity.y * forwardSensitivity;
float upwardForce = (swipeVelocity.y * upwardSensitivity) + baseUpwardBias;
float lateralForce = swipeVelocity.x * lateralSensitivity;
Vector3 force = (shootPoint.forward * forwardForce) +
(Vector3.up * upwardForce) +
(shootPoint.right * lateralForce);
// Apply as Impulse for immediate movement
rb.AddForce(force, ForceMode.Impulse);
// Hủy quả bóng sau 5 giây
Destroy(newBall, 5f);
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: a39d896efd271d648ab60642c63b9556