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

35 lines
1.4 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
public float shootForce = 500f;
public float upwardForce = 200f; // Lực ném vòng cung lên trên
public void ShootBall()
{
// 1. Lấy vị trí ném: Từ Camera lùi xuống dưới một chút (giống tay người cầm bóng)
Vector3 spawnPosition = Camera.main.transform.position
+ Camera.main.transform.forward * 0.5f
- Camera.main.transform.up * 0.2f;
// 2. Tạo quả bóng
GameObject newBall = Instantiate(ballPrefab, spawnPosition, Camera.main.transform.rotation);
// 3. Đảm bảo bóng không bị dính vào Image Target
newBall.transform.SetParent(null);
Rigidbody rb = newBall.GetComponent<Rigidbody>();
if (rb != null)
{
// 4. Lấy hướng nhìn của điện thoại
Vector3 shootDirection = Camera.main.transform.forward;
// 5. Thêm lực ném (Mạnh hơn một chút để bay tới rổ trên bàn)
rb.AddForce(shootDirection * shootForce + Vector3.up * upwardForce);
}
Destroy(newBall, 5f);
}
}