Pages: [1]   Go Down
  Print  
Author Topic: Scripting help  (Read 269 times) Bookmark and Share
0 Members and 1 Guest are viewing this topic.
Skylebones
Global Moderator
Newbie
****

Karma: +4/-0
Offline Offline

Posts: 68



WWW
« on: February 03, 2010, 03:31:49 PM »

Alright, first off I am a complete scripting newbie. I ordered a bunch of books and read a bunch of tutorials, now I just gotta put it all together and make it stick. So be gentle, I don't have a numeric brain at all. I'm a visual guy.

Alright, I have some objects that I want to play some animation when you click on it. I've figured that part out. Here is my javascript.

Quote
var playing : boolean = false;

function OnMouseDown()
{
   if (!playing) {
      playing = true;
      animation.Play("Test_Animation");
      yield WaitForSeconds(animation["Test_Animation"].length);
      playing = false;
   }
}

I know, basic stuff. Now my question is this. Each time you click on the object it will play the animation. I want it to play the animation once, then stay in it's final state. So for example. Lets say you have a door. You click on it and it opens. I want it to stay open. No matter how many times you click on it. Currently with this script each time you click on it, it will play the open animation. So what do I add to make it only play that animation once?

Thanks guys! I really appreciate it. I've halted progress on my game until I get it completely playable in it's rough form. Which is quiet the task when I know nothing about scripting.
Logged

thylaxene
Global Moderator
Newbie
****

Karma: +9/-0
Offline Offline

Posts: 73



WWW
« Reply #1 on: February 03, 2010, 05:10:37 PM »

haven't tried it, but it should work.

Code:
var playing : boolean = false;
var playedOnce : boolean = false;

function OnMouseDown()
{
   if (!playing && !playedOnce) {
      playing = true;
      animation.Play("Test_Animation");
      yield WaitForSeconds(animation["Test_Animation"].length);
      playing = false;
      playedOnce = true;
   }
}

Basically the "playedOnce" controls whether it can be played again once it has finished the animation or not.

So if you want it to play again you just set "playedOnce" to false and all should work again.

Cheers.
Logged

- a Unity user since 2005
www.thylaxene.com
thylaxene
Global Moderator
Newbie
****

Karma: +9/-0
Offline Offline

Posts: 73



WWW
« Reply #2 on: February 03, 2010, 05:19:11 PM »

Also can't help myelf but this quick tutorial I did might have more advance animation controls in it. Can't remember, but I think I had toggle ability in it as well [EDIT] nope I didn't, I left it as homework! Wink

http://www.theunityartist.com/forum/index.php?topic=92.msg1792#msg1792

Cheers.
« Last Edit: February 03, 2010, 05:20:58 PM by thylaxene » Logged

- a Unity user since 2005
www.thylaxene.com
Skylebones
Global Moderator
Newbie
****

Karma: +4/-0
Offline Offline

Posts: 68



WWW
« Reply #3 on: February 03, 2010, 06:43:34 PM »

AHA!! That did the trick. And it makes perfect sense.

I actually checked out the tutorials you posted. The door one was especially helpful. But I haven't quiet figured out how it's all setup. I've made it a goal not to put any scripts in my game unless I know exactly how they work. That way I'll force myself to learn instead of just copy and paste. I'll dig into it a bit further because it covers some other questions I have like using trigger boxes to trigger animation. Which I think I know how to do, just gotta test it out.

Thanks a ton! I really appreciate it.
Logged

Pages: [1]   Go Up
  Print  
 
Jump to: