x


Battery Collect Collision Problem

I have two sets of code. The PlayerCollisions script is suppose to allow for the door on a hut to be opened when four batteries are collected and shown in the gui texture shaped as a battery meter. The script is attached to the FPC with the BatteryCollect on the GUI texture.

The problem is that the gui does not show up and the batteries do not disappear and move the meter scale (clearly since it doesn't show up). If someone can look at the PlayerCollisions script and see if you can spot a problem with it, that would be awesome. I'm at a lost and it looks like it's correct and should work, but it does not.

PlayerCollision.js


private var doorIsOpen : boolean = false;
private var doorTimer : float = 0.0;
private var currentDoor : GameObject;

var doorOpenTime : float = 3.0;
var doorOpenSound : AudioClip;
var doorShutSound : AudioClip;
var batteryCollect : AudioClip;

function Update () {
    if(doorIsOpen){
    	doorTimer += Time.deltaTime;

    	if(doorTimer > 3){
    		shutDoor();
    		doorTimer = 0.0;
    	}

    var hit : RaycastHit;
    	if(Physics.Raycast (transform.position,
    		transform.forward, hit, 5)) {
    	if(hit.collider.gameObject.tag=="outpostDoor"
    		&& doorIsOpen == false && BatteryCollect.charge >= 4){
    		currentDoor = hit.collider.gameObject;
    		Door(doorOpenSound, true, "dooropen", currentDoor);
    		GameObject.Find("Battery GUI").GetComponent(GUITexture).enabled=false;
    		}
    	}
    }
}
/*
function OnControllerColliderHit(hit: ControllerColliderHit){
    if(hit.gameObject.tag == "outpostDoor" && doorIsOpen == false){
    	currentDoor = hit.gameObject;
    	Door(doorOpenSound, true, "dooropen", currentDoor);
    }
}
*/
function OpenDoor(){
    audio.PlayOneShot(doorOpenSound);
    doorOpen = true;
    var myOutpost : GameObject = GameObject.Find("outpost");
    myOutpost.animation.Play("dooropen");
}

function shutDoor(){
    audio.PlayOneShot(doorShutSound);
    doorIsOpen = false;

    var myOutpost : GameObject = GameObject.Find("outpost");
    myOutpost.animation.Play("doorshut");
}

function Door(aClip : AudioClip, openCheck : boolean, animName :
String, thisDoor : GameObject){
    audio.PlayOneShot(aClip);
    doorIsOpen = openCheck;

    thisDoor.transform.parent.animation.Play(animName);
}

function OnTriggerEnter(collisionInfo : Collider){
    if(collisionInfo.gameObject.tag == "battery"){
    	BatteryCollect.charge++;
    	audio.PlayOneShot(batteryCollect);
    	Destroy(collisionInfo.gameObject);
    }
}

@script RequireComponent(AudioSource)

BatteryCollect.js


static var charge : int = 0;

var charge1tex : Texture2D;
var charge2tex : Texture2D;
var charge3tex : Texture2D;
var charge4tex : Texture2D;
var charge0tex : Texture2D;

function Start(){
    guiTexture.enabled = false;
    charge = 0;
}

function Update () {
    if(charge == 1){
    	guiTexture.texture = charge1tex;
    	guiTexture.enabled = true;
    }
    else if(charge == 2){
    	guiTexture.texture = charge2tex;
    	}
    	else if(charge == 3){
    		guiTexture.texture = charge3tex;
    	}
    	else if(charge >= 4){
    		guiTexture.texture = charge4tex;
    	}
    	else{
    		guiTexture.texture = charge0tex;
    	}
}
more ▼

asked Feb 16 '10 at 10:14 PM

Kitty gravatar image

Kitty
79 7 7 17

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Haha wow! Thanks, Jaap.

The script I had works (except the door opening properly), but just made a simple, in my face error. I forgot to change the tags on the game objects to what they needed to be.

Yay! for blonde noob moments XD

more ▼

answered Feb 22 '10 at 04:29 PM

Kitty gravatar image

Kitty
79 7 7 17

may i know where to put the batterycollect script? is it attached to the main camera?

Aug 17 '10 at 07:11 AM nicole
(comments are locked)
10|3000 characters needed characters left

The player doesn't get the OnTriggerEnter event but the battery does. Add a script to the battery with the OnTriggerEnter and let it send a message to the playercontroller, so you'd get something like this:

Script to add in battery:

function OnTriggerEnter(collisionInfo : Collider) {
    collisionInfo.gameObject.SendMessage("CollectBattery",
             gameObject, SendMessageOptions.DontRequireReceiver);
}

and replace OnTriggerEnter code in PlayerCollision.js with this:

function CollectBattery(battery : GameObject) {
    BatteryCollect.charge++;
    audio.PlayOneShot(batteryCollect);
    Destroy(battery);
}
more ▼

answered Feb 17 '10 at 12:05 AM

Jaap Kreijkamp gravatar image

Jaap Kreijkamp
6.5k 20 27 71

(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:

x5276
x496

asked: Feb 16 '10 at 10:14 PM

Seen: 1344 times

Last Updated: Feb 17 '10 at 12:02 PM