x


How to animate a door opening when score reaches a certain #

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?

Any help is greatly appreciated! :)

more ▼

asked May 10 '10 at 07:41 AM

sybrix gravatar image

sybrix
31 2 2 3

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

4 answers: sort voted first

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 = "";
}
more ▼

answered May 10 '10 at 03:08 PM

duck gravatar image

duck ♦♦
41k 92 148 415

Hi Duck, I added the script to all the clues, but I'm getting the error message "Missing Component Exception: There is no animation attached to the game object, but a script is trying to access it." The animation is attached to the door, I don't know how to tell the script to play the animation on the door game object. :(

May 10 '10 at 11:43 PM sybrix
(comments are locked)
10|3000 characters needed characters left

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[i]==clueID){
          alreadyIn = true;
          break;
      }
   }
   if(!alreadyIn)
      cluesArray.Push(clueID);
   if(cluesArray.length>=4)
      doorOpen = true;
}

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.

var clue1found = false;
var clue2found = false;

function Update(){
   if(clue1found&&clue2found){
      //open door
   }
}
more ▼

answered May 10 '10 at 11:08 AM

spinaljack gravatar image

spinaljack
9.1k 18 31 91

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

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.

more ▼

answered May 25 '10 at 11:36 AM

boeington gravatar image

boeington
1 2

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

Okay here's the code I got so far:

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.

more ▼

answered May 11 '10 at 12:29 AM

sybrix gravatar image

sybrix
31 2 2 3

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

x3776
x335

asked: May 10 '10 at 07:41 AM

Seen: 1277 times

Last Updated: May 10 '10 at 07:41 AM