Hi guys i have a character that gun dual wield guns, to make the gunshooting more natural i want the guns to not go of simultaneously. How do I make one gun have a slight delay?
void Start() {
secondsBetweenShots = 60 / rpm;
if (GetComponent<LineRenderer> ())
{
//tracer = GetComponent<LineRenderer> ();
}
}
public void Shoot()
{
if (CanShoot ())
{
Ray ray = new Ray (spawn.position, spawn.forward);
Ray ray2 = new Ray (spawn2.position, spawn2.forward);
RaycastHit hit;
float shotDistance = 20;
if (Physics.Raycast (ray, out hit, shotDistance))
{
shotDistance = hit.distance;
}
if (Physics.Raycast (ray2, out hit, shotDistance))
{
shotDistance = hit.distance;
}
nextPossibleShootTime = Time.time + secondsBetweenShots;
if (tracer)
{
StartCoroutine ("RenderTracer",ray.direction * shotDistance);
StartCoroutine ("RenderTracer",ray2.direction * shotDistance);
}
Rigidbody newShell = Instantiate (shell, shellEjectionPoint.position, shellEjectionPoint.rotation) as Rigidbody;
Rigidbody newShell2 = Instantiate (shell, shellEjectionPoint2.position, shellEjectionPoint2.rotation) as Rigidbody;
newShell.AddForce (shellEjectionPoint.forward * Random.Range (150f, 200f) + spawn.forward * Random.Range (-10f, 10f));
newShell2.AddForce (shellEjectionPoint2.forward * Random.Range (150f, 200f) + spawn.forward * Random.Range (-10f, 10f));
}
}
public void ShootContinuous()
{
if (gunType == GunType.Auto)
{
Shoot ();
}
}
private bool CanShoot()
{
bool canShoot = true;
if(Time.time <nextPossibleShootTime)
{
canShoot = false;
}
return canShoot;
}
IEnumerator RenderTracer(Vector3 hitPoint)
{
tracer.enabled = true;
tracer.SetPosition (0, spawn.position);
tracer.SetPosition (1, spawn.position + hitPoint);
tracer2.enabled = true;
tracer2.SetPosition (0, spawn2.position);
tracer2.SetPosition (1, spawn2.position + hitPoint);
yield return null;
tracer.enabled = false;
tracer2.enabled = false;
}
Comment