x


FPS Tutorial Part 3 Help needed

OK so I've been following that FPS tutorial on Unity's web site. I'm at part 3 and trying to keep my tweaked grenade launcher from part one. I've been mucking about with this blasted script all day trying to figure out what I'm doing wrong. Here is my script for the FPS comtroller:

var maximumHitPoints = 100.0;
var hitPoints = 100.0;

var bulletGUI : GUIText;
var rocketGUI : GUITexture;
var grenadeGUI : GUIText;
var healthGUI : GUITexture;

var walkSounds : AudioClip[];
var painLittle : AudioClip;
var painBig : AudioClip;
var die : AudioClip;
var audioStepLength = 0.3;

private var machineGun : MachineGun;
private var Launcher : Launcher;
private var rocketLauncher : RocketLauncher;
private var healthGUIWidth = 0.0;
private var gotHitTimer = -1.0;

var rocketTextures : Texture[];

function Awake () {
    machineGun = GetComponentInChildren(MachineGun);
    rocketLauncher = GetComponentInChildren(RocketLauncher);
    Launcher = GetComponentInChildren(Launcher);

    PlayStepSounds();

    healthGUIWidth = healthGUI.pixelInset.width;
}

function ApplyDamage (damage : float) {
    if (hitPoints < 0.0)
        return;

    // Apply damage
    hitPoints -= damage;

    // Play pain sound when getting hit - but don't play so often
    if (Time.time > gotHitTimer && painBig && painLittle) {
        // Play a big pain sound
        if (hitPoints < maximumHitPoints * 0.2 || damage > 20) {
            audio.PlayOneShot(painBig, 1.0 / audio.volume);
            gotHitTimer = Time.time + Random.Range(painBig.length * 2, painBig.length * 3);
        } else {
            // Play a small pain sound
            audio.PlayOneShot(painLittle, 1.0 / audio.volume);
            gotHitTimer = Time.time + Random.Range(painLittle.length * 2, painLittle.length * 3);
        }
    }

    // Are we dead?
    if (hitPoints < 0.0)
        Die();
}

function Die () {
    if (die)
        AudioSource.PlayClipAtPoint(die, transform.position);

    // Disable all script behaviours (Essentially deactivating player control)
    var coms : Component[] = GetComponentsInChildren(MonoBehaviour);
    for (var b in coms) {
        var p : MonoBehaviour = b as MonoBehaviour;
        if (p)
            p.enabled = false;
    }

    LevelLoadFade.FadeAndLoadLevel(Application.loadedLevel, Color.white, 2.0);
}

function LateUpdate () {
    // Update gui every frame
    // We do this in late update to make sure machine guns etc. were already executed
    UpdateGUI();
}

function PlayStepSounds () {
    var controller : CharacterController = GetComponent(CharacterController);

    while (true) {
        if (controller.isGrounded && controller.velocity.magnitude > 0.3) {
            audio.clip = walkSounds[Random.Range(0, walkSounds.length)];
            audio.Play();
            yield WaitForSeconds(audioStepLength);
        } else {
            yield;
        }
    }
}


function UpdateGUI () {
    // Update health gui
    // The health gui is rendered using a overlay texture which is scaled down based on health
    // - Calculate fraction of how much health we have left (0...1)
    var healthFraction = Mathf.Clamp01(hitPoints / maximumHitPoints);

    // - Adjust maximum pixel inset based on it
    healthGUI.pixelInset.xMax = healthGUI.pixelInset.xMin + healthGUIWidth * healthFraction;

    // Update machine gun gui
    // Machine gun gui is simply drawn with a bullet counter text
    if (machineGun) {
        bulletGUI.text = machineGun.GetBulletsLeft().ToString();
    }

    // Update machine gun gui
    // Machine gun gui is simply drawn with a bullet counter text
    if (Launcher) {
        bulletGUI.text = Launcher.GetBulletsLeft().ToString();
    }

    // Update rocket gui
    // This is changed from the tutorial PDF. You need to assign the 20 Rocket textures found in the GUI/Rockets folder
    // to the RocketTextures property.
    if (rocketLauncher) {
        if (rocketTextures.Length == 0) {
            Debug.LogError ("The tutorial was changed with Unity 2.0 - You need to assign the 20 Rocket textures found in the GUI/Rockets folder to the RocketTextures property.");
        } else {
            rocketGUI.texture = rocketTextures[rocketLauncher.ammoCount];
        }
    }
}

I thought it would be just a simple matter of duplicating some of the variables but I'm clearly doing something wrong!! I don't know why they did not take into account that some of us might want to keep the grenade launcher lol especially since I got it working so sweet and all ;)

HeeeeelP :)

more ▼

asked Feb 15 '11 at 01:12 AM

Michael 12 gravatar image

Michael 12
132 87 99 106

What kind of errors are you getting? It compiles fine, so you are probably just missing some assets the script is trying to reference. Make sure the assets it needs are part of the gameobject.

Feb 15 '11 at 04:21 AM reissgrant

I have it set up now where I can select my grenade launcher by clicking on #3 on the keyboard. The problem I'm having is trying to figure out how to make ammo pickups of grenades add to my Grenade GUI Text, I sort of understand it but I keep mucking it up somewhere in my script, I'm just not that good at scripting yet.

Mar 06 '11 at 02:33 PM Michael 12
(comments are locked)
10|3000 characters needed characters left

0 answers: sort oldest
Be the first one to answer this question
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:

x3419
x86
x84
x47

asked: Feb 15 '11 at 01:12 AM

Seen: 776 times

Last Updated: Feb 15 '11 at 01:15 AM