x


Handgun doesn't shoot... need help.

Okay so for some reason my handgun just doesn't shoot. I made an explosive sphere that has hit points. Can't test it out because weapon not working.

The handgun code:

    var range = 100.0;
var fireRate = 0.05;
var force = 10.0;
var damage = 5.0;
var bulletsPerClip = 40;
var clips = 20;
var reloadTime = 0.5;
private var hitParticles : ParticleEmitter;
var muzzleFlash : Renderer;
var bulletsLeft : int = 0;
private var nextFireTime = 0.0;
private var m_LastFrameShot = -1;
function Start ()
{
hitParticles = GetComponentInChildren(ParticleEmitter);
// We don't want to emit particles all the time, only when we hit something.
if (hitParticles)

hitParticles.emit = false;
bulletsLeft = bulletsPerClip;
}

function Update()
{
    if (Input.GetButtonDown("Fire1"))
    {
        FireOneShot();
    }
}

function LateUpdate()
{
if (muzzleFlash)
{
// We shot this frame, enable the muzzle flash
if (m_LastFrameShot == Time.frameCount)
{
muzzleFlash.transform.localRotation =
Quaternion.AngleAxis(Random.Range(0, 359), Vector3.forward);
muzzleFlash.enabled = true;
if (audio)
{
if (!audio.isPlaying)
audio.Play();
audio.loop = true;
}
}
// We didn't, disable the muzzle flash
else
{
muzzleFlash.enabled = false;
enabled = false;
// Play sound
if (audio)
{
audio.loop = false;
}
}
}
}
function Fire ()
{
if (bulletsLeft == 0)
return;
// If there is more than one bullet between the last and this frame
// Reset the nextFireTime
if (Time.time - fireRate > nextFireTime)
nextFireTime = Time.time - Time.deltaTime;
// Keep firing until we used up the fire time
while( nextFireTime < Time.time && bulletsLeft != 0)
{
FireOneShot();
nextFireTime += fireRate;
}
}
function FireOneShot ()
{
var direction = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;
// Did we hit anything?
if (Physics.Raycast (transform.position, direction, hit, range))
{
// Apply a force to the rigidbody we hit
if (hit.rigidbody)
hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
// Place the particle system for spawing out of place where we hit the surface!
// And spawn a couple of particles
if (hitParticles)
{
hitParticles.transform.position = hit.point;
hitParticles.transform.rotation =
Quaternion.FromToRotation(Vector3.up, hit.normal);
hitParticles.Emit();
}
// Send a damage message to the hit object
hit.collider.SendMessageUpwards("ApplyDamage", damage,
SendMessageOptions.DontRequireReceiver);
}
bulletsLeft--;
// Register that we shot this frame,
// so that the LateUpdate function enabled the muzzleflash renderer for one frame
m_LastFrameShot = Time.frameCount;
enabled = true;
// Reload gun in reload Time
if (bulletsLeft == 0)
Reload();
}
function Reload () {
// Wait for reload time first - then add more bullets!
yield WaitForSeconds(reloadTime);
// We have a clip left reload
if (clips > 0)
{
clips--;
bulletsLeft = bulletsPerClip;
}
}
function GetBulletsLeft () {
return bulletsLeft;
}

alt text

more ▼

asked Dec 05 '10 at 08:26 PM

Daniele gravatar image

Daniele
116 15 16 21

Please make sure your code is correctly formatted, because its unreadeble like this.

Could you tell us what isnt working?

Dec 05 '10 at 08:27 PM Maarten

How does one put in correct format? Sorry Im new to all this ;/

Dec 05 '10 at 08:29 PM Daniele

In correct format now. Basically nothing happens in game when I left click to fire weapon. Though on the far right it says bullets left and that goes down after each click.

Dec 05 '10 at 08:43 PM Daniele
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first
var range = 100.0;
var fireRate = 0.05;
var force = 10.0;
var damage = 5.0;
var bulletsPerClip = 40;
var clips = 20;
var reloadTime = 0.5;
private var hitParticles : ParticleEmitter;
var muzzleFlash : Renderer;
var bulletsLeft : int = 0;
private var nextFireTime = 0.0;
private var m_LastFrameShot = -1;
function Start ()
{
    hitParticles = GetComponentInChildren ( ParticleEmitter );
// We don't want to emit particles all the time, only when we hit something.

    if ( hitParticles )

        hitParticles.emit = false;

    bulletsLeft = bulletsPerClip;
}

function Update()
{
    if ( Input.GetButtonDown ( "Fire1" ) )
    {
        FireOneShot();
    }
}

function LateUpdate()
{
    if ( muzzleFlash )
    {
// We shot this frame, enable the muzzle flash
        if ( m_LastFrameShot == Time.frameCount )
        {
            muzzleFlash.transform.localRotation =
                Quaternion.AngleAxis ( Random.Range ( 0, 359 ), Vector3.forward );
            muzzleFlash.enabled = true;

            if ( audio )
            {
                if ( !audio.isPlaying )
                    audio.Play();

                audio.loop = true;
            }
        }

// We didn't, disable the muzzle flash

        else
        {
            muzzleFlash.enabled = false;
            enabled = false;
// Play sound

            if ( audio )
            {
                audio.loop = false;
            }
        }
    }
}

function Fire ()
{
    if ( bulletsLeft == 0 )
        return;

// If there is more than one bullet between the last and this frame
// Reset the nextFireTime
    if ( Time.time - fireRate > nextFireTime )
        nextFireTime = Time.time - Time.deltaTime;

// Keep firing until we used up the fire time
    while ( nextFireTime < Time.time && bulletsLeft != 0 )
    {
        FireOneShot();
        nextFireTime += fireRate;
    }
}

function FireOneShot ()
{
    var direction = transform.TransformDirection ( Vector3.forward );
    var hit : RaycastHit;
// Did we hit anything?

    if ( Physics.Raycast ( transform.position, direction, hit, range ) )
    {
// Apply a force to the rigidbody we hit
        if ( hit.rigidbody )
            hit.rigidbody.AddForceAtPosition ( force * direction, hit.point );

// Place the particle system for spawing out of place where we hit the surface!
// And spawn a couple of particles
        if ( hitParticles )
        {
            hitParticles.transform.position = hit.point;
            hitParticles.transform.rotation =
                Quaternion.FromToRotation ( Vector3.up, hit.normal );
            hitParticles.Emit();
        }

// Send a damage message to the hit object
        hit.collider.SendMessageUpwards ( "ApplyDamage", damage,
                                          SendMessageOptions.DontRequireReceiver );
    }

    bulletsLeft--;

// Register that we shot this frame,
// so that the LateUpdate function enabled the muzzleflash renderer for one frame
    m_LastFrameShot = Time.frameCount;
    enabled = true;
// Reload gun in reload Time

    if ( bulletsLeft == 0 )
        Reload();
}

function Reload ()
{
// Wait for reload time first - then add more bullets!
    yield WaitForSeconds ( reloadTime );
// We have a clip left reload

    if ( clips > 0 )
    {
        clips--;
        bulletsLeft = bulletsPerClip;
    }
}

function GetBulletsLeft ()
{
    return bulletsLeft;
}

This isn't changed, just better formatting...

Where are you spawning/creating/instantiating the prefab/bullet? Sorry, not seeing it... When it does FireOneShot(); when you click, it makes the direction, and a raycast... Aplies a force if it hits something, and spawns some particles, then it takes away damage from that item if possible.. Finally it takes out some bullets, checks if their is no bullets, if so, they reload.... Their isn't any bullet.. Am I right?

Also, maybe you don't want a visual bullet, then in that case, you have to make the particle system to use for the ParticleEmmiter up top...

Hope that helps!

more ▼

answered Dec 05 '10 at 09:08 PM

Justin Warner gravatar image

Justin Warner
6.3k 19 27 65

Ye I don't want visible bullets. Just the sparks or whatever to show where bullets have been fired if you know what I mean. Check the image I just uploaded above. The bullets go down and automatically reload when hits 0.

Dec 05 '10 at 09:52 PM Daniele

I try to continuously shoot the explosive sphere that only has a mere 20 hits points... nothing happens basically ;/

Dec 05 '10 at 09:56 PM Daniele

What a mess of a script. First tip: add #pragma strict to the top so you can have the inspector tell you problems so we dont have to. One thing it would tell you is that Fire () isn't even called. Firerate variables only do stuff when you have if statements to consider them. In your if(Input.GetButtonDown) it should consider the firerate like: if(Input.GetButtonDown && NextFireTime == 0)

Personally... I would start over on this script.

Aug 13 '12 at 11:02 PM Dev Solo
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1176
x447
x329
x309

asked: Dec 05 '10 at 08:26 PM

Seen: 733 times

Last Updated: Aug 13 '12 at 11:02 PM