I am completely new to scripting. I am not familiar with a lot of the syntax, however I am familiar with some basic coding. This is what I need to accomplish, but have no idea how to approach it:
The player unlocks a door after investigating 5 different clues. The clues have sphere colliders on then that pops up informative text whenever the player enters the collider, then disappears when they back away from the clue. like this:
var noteText : String = "";
function OnGUI() {
GUILayout.BeginArea (Rect((Screen.width/2)-50, (Screen.height/2) , 300, 300));
GUILayout.Label(noteText);
GUILayout.EndArea ();
}
function OnTriggerEnter( other : Collider ){
noteText = "Clue Text";
}
function OnTriggerExit( other : Collider ){
noteText = "";
}
I need to make it so after the player has entered all 6 of the "clue spheres" at least once that a "Door Open" animation is played on an object in my scene (I have made the door animation already). The player can then pass through the open door.
So how do I code it so that the script keeps track of if all the clues that have been "entered", and then plays the animation when all of them have been investigated?
I would make the clues script itself handle this, and use a simple static counter variable to count the number of clues remaining to be found.
Here is the modified script - only a few lines added:
private static var numRemainingClues = 0;
private var shown = false;
var noteText : String = "";
function Start() {
// each clue counts itself when it starts:
numRemainingClues++;
}
function OnGUI() {
GUILayout.BeginArea (Rect((Screen.width/2)-50, (Screen.height/2) , 300, 300));
GUILayout.Label(noteText);
GUILayout.EndArea ();
}
function OnTriggerEnter( other : Collider ){
noteText = "Clue Text";
// each clue counts itself out when shown for the first time:
if (!shown) {
shown = true;
numRemainingClues--;
if (numRemainingClues == 0) {
PlayTheDoorAnimation(); // however you want to trigger that
}
}
}
function OnTriggerExit( other : Collider ){
noteText = "";
}
One method is to keep an array of clues found and then opening the door when the length of the array is greater than 4 or something. You can search the array to make sure that the clue isn't already in it and then use the Array.Push method to add a clue id onto the array. e.g.
var cluesArray;
function Start(){
cluesArray = new Array();
}
function AddClue(clueID:int){
var alreadyIn = false;
for(i=0;i<cluesArray.length;i++){
if(cluesArray*==clueID){*
*alreadyIn = true;*
*break;*
*}*
*}*
*if(!alreadyIn)*
*cluesArray.Push(clueID);*
*if(cluesArray.length>=4)*
*doorOpen = true;*
*}*
*```*
*<p>Alternatively if there are only a few clues in a place you can use boolean values associated with each clue and only open the door when all clues are set to true. e.g.</p>*
*```*
*var clue1found = false;*
*var clue2found = false;*
*function Update(){*
*if(clue1found&&clue2found){*
*//open door*
*}*
*}*
*```*
private static var numRemainingClues = 0;
private var shown = false;
var noteText : String = "";
var other : GameObject;
function Start() {
// each clue counts itself when it starts:
numRemainingClues++;
}
function OnGUI() {
GUILayout.BeginArea (Rect((Screen.width/2)-50, (Screen.height/2) , 300, 300));
GUILayout.Label(noteText);
GUILayout.EndArea ();
}
function OnTriggerEnter( other : Collider ){
noteText = "Clue Text";
// each clue counts itself out when shown for the first time:
if (!shown) {
shown = true;
numRemainingClues--;
if (numRemainingClues == 0) {
other.animation.Play("DoorOpen"); // however you want to trigger that
}
}
}
function OnTriggerExit( other : Collider ){
noteText = "";
}
I have the other GameObject var set to the Door in the Inspector for all of the clues, however when I walk into a clue I get the error "Missing Component Exception: There is no animation attached to the "First Person Controller" game object, but a script is trying to access it." I am completely baffled as to why the script is looking for animation on the FP controller.
The last sample tells the collider that triggers the event IS the First Person Controller, so it searches an animation in it, but I figure you already know that by this time, don't you ?
Instead of using the "other" object, use the door object.