I am wanting to make a shooting script like binding of isacc. So if i face right pressing D on my computer and i hold down my mouse button it shoots in that direction. I also need help with destroying the bullet clone. I still want to control delay and speed.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerShoot : MonoBehaviour
{
[SerializeField] private float fireDelay;
private float lastTime;
public float bulletSpeed;
public GameObject projectilePrefab;
void Update()
{
if (Input.GetMouseButton(0))
{
Shoot();
}
}
void Shoot()
{
if (Time.time > lastTime + fireDelay)
{
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 dir = mousePos - transform.position;
dir.Normalize();
GameObject projectile = Instantiate(projectilePrefab, transform.position, Quaternion.identity);
projectile.GetComponent().velocity = dir * bulletSpeed;
lastTime = Time.time;
}
}
}