Hi. I am a beginner and I collect scripts from websites or use chatgpt, so I would like to ask what the problem with the script could be.
The script should work in such a way that there is a projectile that shoots from where the player is, it shoots as many times as I press the left click, the projectile should be deleted every 3 seconds, the projectile should only be able to move along the y-axis.
I wrote 2 scripts.
WeaponScript.cs
using UnityEngine;
public class WeaponScript : MonoBehaviour
{
public GameObject bulletPrefab;
public Transform firePoint;
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
// Lövedék kilövése, amikor a bal egérgombot lenyomják
Shoot();
}
}
private void Shoot()
{
// Lövedék példányosítása és kilövése
if (firePoint != null)
{
GameObject bullet = Instantiate(bulletPrefab, firePoint.position, Quaternion.identity);
Destroy(bullet, 3f);
}
}
}
BulletController
using UnityEngine;
public class BulletController : MonoBehaviour
{
public float speed = 10f;
public float lifespan = 3f;
private float timer;
private void Start()
{
timer = lifespan;
}
private void Update()
{
// A lövedék felfelé mozog
transform.Translate(Vector3.up * speed * Time.deltaTime);
// Időzítő a lövedék élettartamához
timer -= Time.deltaTime;
if (timer <= 0f)
{
Destroy(gameObject);
}
}
private void OnTriggerEnter(Collider other)
{
// Ellenőrizd, hogy a lövedék más tárgyhoz ért-e
// Itt kapsz alkalmat a karakter követésére vagy bármilyen más tevékenységre
}
}
please help or give me some advice