50 lines
1.4 KiB
C#
50 lines
1.4 KiB
C#
using UnityEngine;
|
|
|
|
public class Shooter : MonoBehaviour
|
|
{
|
|
[Header("Projectile Settings")]
|
|
[SerializeField] private GameObject magicBallPrefab;
|
|
[SerializeField] private float shootSpeed = 20f;
|
|
[SerializeField] private float destroyAfterSeconds = 5f;
|
|
|
|
[Header("Spawn Point")]
|
|
[SerializeField] private Transform shootPoint;
|
|
|
|
[Header("Fire Control")]
|
|
[SerializeField] private float fireCooldown = 0.5f;
|
|
[SerializeField] private float shootForce = 0.5f;
|
|
|
|
private float lastFireTime = 0f;
|
|
|
|
void Update()
|
|
{
|
|
if (Input.GetMouseButtonDown(0) && Time.time > lastFireTime + fireCooldown)
|
|
{
|
|
Shoot();
|
|
lastFireTime = Time.time;
|
|
}
|
|
}
|
|
|
|
private void Shoot()
|
|
{
|
|
if (magicBallPrefab == null || shootPoint == null)
|
|
{
|
|
Debug.LogWarning("Missing magicBallPrefab or shootPoint!");
|
|
return;
|
|
}
|
|
|
|
GameObject ball = Instantiate(magicBallPrefab, shootPoint.position, Quaternion.LookRotation(transform.forward));
|
|
|
|
if (ball.TryGetComponent(out Rigidbody rb))
|
|
{
|
|
rb.useGravity = false;
|
|
rb.linearVelocity = Vector3.zero;
|
|
|
|
Vector3 direction = transform.forward; // same as LookRotation direction
|
|
rb.AddForce(direction * shootForce, ForceMode.VelocityChange);
|
|
}
|
|
|
|
Destroy(ball, destroyAfterSeconds);
|
|
}
|
|
|
|
} |