Cannon shooting

Hey!
I have this script i want to use, if its possible.

The first thing i want to ask if its possible to put a delay on when the cannon fired, depending on when the player presses the fire button.
Like a charging time delay.
second.
Do i have to make another script if i want to have two different guns on one player model?
for instance:
I have a sci fi Tank that shoots a slow moving but deadly charged plasma shot that takes out an area of enemies.
but there is a secondary laser minigun you can use.
any pointers on how to make that?

using UnityEngine;

public class PlayerShooting : MonoBehaviour
{
public int damagePerShot = 20;
public float timeBetweenBullets = 0.15f;
public float range = 100f;

float timer;
Ray shootRay;
RaycastHit shootHit;
int shootableMask;
ParticleSystem gunParticles;
LineRenderer gunLine;
AudioSource gunAudio;
Light gunLight;
float effectsDisplayTime = 0.2f;

void Awake ()
{
shootableMask = LayerMask.GetMask (“Shootable”);
gunParticles = GetComponent ();
gunLine = GetComponent ();
gunAudio = GetComponent ();
gunLight = GetComponent ();
}

void Update ()
{
timer += Time.deltaTime;

if(Input.GetButton (“Fire1”) && timer >= timeBetweenBullets && Time.timeScale != 0)
{
Shoot ();
}

if(timer >= timeBetweenBullets * effectsDisplayTime)
{
DisableEffects ();
}
}

public void DisableEffects ()
{
gunLine.enabled = false;
gunLight.enabled = false;
}

void Shoot ()
{
timer = 0f;

gunAudio.Play ();

gunLight.enabled = true;

gunParticles.Stop ();
gunParticles.Play ();

gunLine.enabled = true;
gunLine.SetPosition (0, transform.position);

shootRay.origin = transform.position;
shootRay.direction = transform.forward;

if(Physics.Raycast (shootRay, out shootHit, range, shootableMask))
{
EnemyHealth enemyHealth = shootHit.collider.GetComponent ();
if(enemyHealth != null)
{
enemyHealth.TakeDamage (damagePerShot, shootHit.point);
}
gunLine.SetPosition (1, shootHit.point);
}
else
{
gunLine.SetPosition (1, shootRay.origin + shootRay.direction * range);
}
}
}

You probably could push it all in one script. Also, how you delay, you create a timer variable in the script and every Update increment the value by 1 * time.deltaTime. Here’s a little example:

int timePassed = 0;

void Update()
{
timePassed += 1 * time.deltaTime;
if(timePassed > 3)
{
Debug.Log(“I waited for 4 or more seconds!”);
}
}

Hope it helps! :slight_smile:

Hey!
I have this script i want to use, if its possible.

The first thing i want to ask if its possible to put a delay on when the cannon fired, depending on when the player presses the fire button.
Like a charging time delay.
second.
Do i have to make another script if i want to have two different guns on one player model?
for instance:
I have a sci fi Tank that shoots a slow moving but deadly charged plasma shot that takes out an area of enemies.
but there is a secondary laser minigun you can use.
any pointers on how to make that?

using UnityEngine;

public class PlayerShooting : MonoBehaviour
{
public int damagePerShot = 20;
public float timeBetweenBullets = 0.15f;
public float range = 100f;


float timer;
Ray shootRay;
RaycastHit shootHit;
int shootableMask;
ParticleSystem gunParticles;
LineRenderer gunLine;
AudioSource gunAudio;
Light gunLight;
float effectsDisplayTime = 0.2f;


void Awake ()
{
shootableMask = LayerMask.GetMask ("Shootable");
gunParticles = GetComponent<ParticleSystem> ();
gunLine = GetComponent <LineRenderer> ();
gunAudio = GetComponent<AudioSource> ();
gunLight = GetComponent<Light> ();
}


void Update ()
{
timer += Time.deltaTime;

if(Input.GetButton ("Fire1") && timer >= timeBetweenBullets && Time.timeScale != 0)
{
Shoot ();
}

if(timer >= timeBetweenBullets * effectsDisplayTime)
{
DisableEffects ();
}
}


public void DisableEffects ()
{
gunLine.enabled = false;
gunLight.enabled = false;
}


void Shoot ()
{
timer = 0f;

gunAudio.Play ();

gunLight.enabled = true;

gunParticles.Stop ();
gunParticles.Play ();

gunLine.enabled = true;
gunLine.SetPosition (0, transform.position);

shootRay.origin = transform.position;
shootRay.direction = transform.forward;

if(Physics.Raycast (shootRay, out shootHit, range, shootableMask))
{
EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> ();
if(enemyHealth != null)
{
enemyHealth.TakeDamage (damagePerShot, shootHit.point);
}
gunLine.SetPosition (1, shootHit.point);
}
else
{
gunLine.SetPosition (1, shootRay.origin + shootRay.direction * range);
}
}
}

Better like that?

Look at your post. Does it look right to you?

Think so.
But now i need help in the actual problem.
Can you guys help me?

hey!
i tried it, but i actually didnt know how to implement it into the existing code.
all i get is errors. where should it be? and what should i take away?

Im bumping this cus i still need help with it.
I need help with this script to make a delay from when you press the fire button, and add a 2 second delay until the actuall shot if fired.
Its suppose to simulate a power build up inside my cannon, like its sucking in power, creating an energy shot, then releasing it it.
Please help?

1.You can use WaitForSeconds in order to delay time. Try to work it into your code to get the delay your looking for.

Scripting reference.

  1. There’s no rule whether you need to have one script or two scripts for 2 weapons. Although having separate scripts for each weapon is arguably the best. Create one script that can be used for a variety of weapon types, then duplicate it as many times as you need and then change the variables of each script to fit specific weapons. When switching weapons, keep the weapon scripts currently not in use disabled while keeping the selected weapon enabled.

Im at a loss on what i should do?
Should i make a reference on the WaitForSeconds coroutine script, or should i implement the WaitForSeconds code into the shooting code?
if its the later, should i just make an:

    public int damagePerShot = 20;
    public float timeBetweenBullets = 0.15f;
    public float range = 100f;
    public float WaitForSeconds = 2f;

and then add the WaitForSeconds code before the actual “Shoot” function? Like this:

 void Update ()
    {
        timer += Time.deltaTime;

        if(Input.GetButton ("Fire1") && timer >= timeBetweenBullets && Time.timeScale != 0)
        {
            Shoot ();
            WaitForSeconds ();
        }

        if(timer >= timeBetweenBullets * effectsDisplayTime)
        {
            DisableEffects ();
        }
    }

This should be easy, but its making my brain melt =) frustrating is a small word for such a little problem =)

Make shoot a coroutine then add WaitForSeconds() into it. Something like this. Specify how many seconds you want the pause to last in the brackets.

void Update ()
{
timer += Time.deltaTime;

if(Input.GetButton ("Fire1") && timer >= timeBetweenBullets && Time.timeScale != 0)
{
StartCoroutine ("Shoot")
}

if(timer >= timeBetweenBullets * effectsDisplayTime)
{
DisableEffects ();
}
}


public IEnumerator Shoot ()
{
yield return new WaitForSeconds(5);
timer = 0f;

//Play rest of code...
}

}

Hey again =)
thanx for the reply.
Im trying to implement the code you gave, but its saying that i have a bracket in a wrong place.
I cant see anything wrong with the brackets(curly braces). check it out =) :

using UnityEngine;

public class PlayerShootingCannon : MonoBehaviour
{
    public int damagePerShot = 20;
    public float timeBetweenBullets = 0.15f;
    public float range = 100f;


    float timer;
    Ray shootRay;
    RaycastHit shootHit;
    int shootableMask;
    ParticleSystem gunParticles;
    LineRenderer gunLine;
    AudioSource gunAudio;
    Light gunLight;
    float effectsDisplayTime = 0.2f;


    void Awake ()
    {
        shootableMask = LayerMask.GetMask ("Shootable");
        gunParticles = GetComponent<ParticleSystem> ();
        gunLine = GetComponent <LineRenderer> ();
        gunAudio = GetComponent<AudioSource> ();
        gunLight = GetComponent<Light> ();
    }


    void Update ()
    {
        timer += Time.deltaTime;

        if(Input.GetButton ("Fire2") && timer >= timeBetweenBullets && Time.timeScale != 0)
        {
            StartCoroutine ("Shoot")
        }

        if(timer >= timeBetweenBullets * effectsDisplayTime)
        {
            DisableEffects ();
        }
    }


    public IEnumerator Shoot ()
    {
        yield return new WaitForSeconds(3);
        timer = 0f;
    }
       


    public void DisableEffects ()
    {
        gunLine.enabled = false;
        gunLight.enabled = false;
    }


    void Shoot ()
    {
        timer = 0f;

        gunAudio.Play ();

        gunLight.enabled = true;

        gunParticles.Stop ();
        gunParticles.Play ();

        gunLine.enabled = true;
        gunLine.SetPosition (0, transform.position);

        shootRay.origin = transform.position;
        shootRay.direction = transform.forward;

        if(Physics.Raycast (shootRay, out shootHit, range, shootableMask))
        {
            EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> ();
            if(enemyHealth != null)
            {
                enemyHealth.TakeDamage (damagePerShot, shootHit.point);
            }
            gunLine.SetPosition (1, shootHit.point);
        }
        else
        {
            gunLine.SetPosition (1, shootRay.origin + shootRay.direction * range);
        }
    }
}

unity says this:
Assets/Scripts/Player/PlayerShootingCannon.cs(38,17): error CS1525: Unexpected symbol `}’

You forgot the ’ ; ’ after StartCoroutine(“Shoot”) on the 38th line

thanx bud =)