- Home /
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; } }
Answer by Kitty · Feb 22, 2010 at 04:29 PM
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
may i know where to put the batterycollect script? is it attached to the main camera?
Answer by Jaap Kreijkamp · Feb 17, 2010 at 12:05 AM
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);
}
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Gui Texture Swap? 1 Answer
Assign GUITexture in scene to variable via scripting? 1 Answer
Hide/Unhide GUITexture Help! 1 Answer
Draw GUI texture from it's center point. 2 Answers