Files
VR-GAME/Assets/Script/BallShooter.cs

42 lines
1.6 KiB
C#

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
[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<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);
// 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);
}
}