update lại về cam và logic

This commit is contained in:
2026-05-04 14:52:40 +07:00
parent 5bc99cd1b6
commit 24866c6c99
14 changed files with 460 additions and 43 deletions

View File

@@ -7,16 +7,29 @@ public class BallShooter : MonoBehaviour
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()
{
GameObject newBall = Instantiate(ballPrefab, shootPoint.position, shootPoint.rotation);
// 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);
}
// 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
Destroy(newBall, 5f);
}
}