x


Glich with treasure chest

I made a treasure chest that you find and you open it with a raycast, but the problem I have is after its open, if your raycast hits it the animation plays again. Its pretty stupid looking and i don't no how to fix it. This iss my code so far(only the important parts):

function Update ()
{
    var hit : RaycastHit;
    if (Physics.Raycast(transform.position, transform.forward, hit, 5))
    {
        if(hit.collider.gameObject.tag == "chest")
        {
            hit.collider.gameObject.animation.Play("chestOpen");

        }
    }
}
more ▼

asked Nov 16 '10 at 04:44 AM

Tyler Alvis gravatar image

Tyler Alvis
215 43 50 57

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

1 answer: sort voted first

The solution is to remember the event of opening the chest, and only do the raycast test if the chest was not previously opened. For instance, you could have a

var chestOpened = false;

as an instance variable of that script, then do

chestOpened = true;

when you open the chest (just before the Play("chestOpen") call), and finally, surround the entire raycast check with the following check:

if (!chestOpened) {

// Raycast

}

Of course, since your game will likely have more things to interact with than a single chest, you will probably have to think up a more sophisticated way of tracking events than simple boolean variables, but this should solve your immediate problem.

more ▼

answered Nov 16 '10 at 10:10 AM

Thelo gravatar image

Thelo
47 1 6

You first sentence explained the entire problem. +1

Nov 16 '10 at 01:50 PM Proclyon
(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:

x19
x5

asked: Nov 16 '10 at 04:44 AM

Seen: 534 times

Last Updated: Nov 16 '10 at 05:04 AM