Problem with a shooting script.
Hello, I'll start by saying that I'm a begginer so if you have any suggestion and/or criticism it would be very helpful if you did it in a more datailed way, thank you in advance.
So, the way i want to make this script work is that i have 2 cannons (emiters) and they don't shot in sync (first shot of one of the cannons is delayed by some time). But the code below doesn't work as intented - cannons shoot but synced up.
Second thing, how could i add some kind of mouse button spam "protection" so the player cannot surpass the fire rate by spamming the shooting button?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shooting : MonoBehaviour
{
public Transform bulletTransform;
public GameObject bullet;
public float bulletSpeed;
float shotTimer;
float shotTimerElapsed;
public float shotsPerSecond;
public GameObject emiter;
void Start()
{
shotTimer = 1 / shotsPerSecond;
shotTimerElapsed = 0;
}
IEnumerator shooting(float time)
{
if (emiter.name == "emiterR_mech")
{
Update();
}
if (emiter.name == "emiterL_mech")
{
yield return new WaitForSeconds(1);
Update();
}
}
void Update()
{
shotTimerElapsed = shotTimerElapsed + Time.deltaTime;
Quaternion rotation = bulletTransform.rotation;
if (Input.GetMouseButton(0) && shotTimerElapsed >= shotTimer)
{
shotTimerElapsed = 0;
GameObject instBullet = Instantiate(bullet, transform.position, rotation) as GameObject;
Rigidbody instBulletRigidbody = instBullet.GetComponent<Rigidbody>();
instBulletRigidbody.AddRelativeForce(Vector3.forward * bulletSpeed);
Destroy(instBullet, 3f);
}
if (Input.GetMouseButtonUp(0))
{
shotTimerElapsed = shotTimer;
}
}
}
Your answer
Follow this Question
Related Questions
Please help me with my shooting script. 0 Answers
How to make a shooting bow? 0 Answers
Need help with Nav Mesh Agent getting "stuck" at high speeds 0 Answers
How do I get a reference to a script that is on another gameobject 0 Answers
How to move a object from point A to point B and back again (3D) 1 Answer