Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Rob 4 · Dec 20, 2010 at 03:58 PM · animationanimationcliploopingoneshot

Stop animation after playing once

I'm using RaycastHit to script a door opening animation when my character comes within a few feet of the door. How do I get the animation (of door sliding out of the way) to play only one time.

Right now, once I'm within the specified distance, the door animation keeps playing over and over (so keeps opening and closing until I move back out of distance).

Animation simply has door keyed to normal position at 0 frames, and then position.y made negative so door slides into ground at 30 frames. And here's the script I'm using:

var rayCastLength = 10;

function Update ()

{ var hit:RaycastHit;

//check if we're colliding if(Physics.Raycast(transform.position, transform.forward, hit, rayCastLength)) { //with a door - need to add the 'collider' in this code since using Raycast instead of just collision if(hit.collider.gameObject.tag == "door") { //open the door hit.collider.gameObject.animation.Play("doorOpen"); }

}

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Paulius-Liekis · Dec 20, 2010 at 04:10 PM

Setting ClampForever wrapMode on your door animation should do the trick.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rob 4 · Dec 20, 2010 at 05:32 PM 0
Share

Thanks. That fixed it. So did play once and even default. I must have had it set on Loop inadvertently.

On a related note, if I want the animation to reverse if I move out of range (so door only stays open when I walk near it), is there a simple way to do that? Or a way to have the door stay open for 5 seconds and then close?

Thanks

avatar image Rob 4 · Dec 20, 2010 at 05:43 PM 0
Share

O$$anonymous$$ - wasn't just my imagination - there seems to be a glitch. If I set the doorOpen animation on Once, it repeats itself many times over a short time period - so the door races up and down about 5 to fifteen times - then after going through all those cycles, the door remains fixed in the open position.

(With clampForever this behavior didn't happen. And with Once, it seems inconsistent whether or not it Loops several times or works properly from the outset).

Is this expected behavior or some bug in the system (or in my noobie code more likely)

avatar image Paulius-Liekis · Dec 20, 2010 at 08:41 PM 0
Share

The Once behavior might be by design - once "Once" is done it resets time to 0, so if you call Play again, it will play again. ClampForever will make animation get stuck in last frame. You can reverse playbakc by setting animation speed to negative, although I would suspect that there might be some bugs related to that in Unity 3.1 (I fixed some of those in 3.2).

avatar image Rob 4 · Dec 21, 2010 at 04:44 AM 0
Share

Ok. Thanks for the clarification. Now that you've described how it works, I think I should be able to add some simple javascript to get the behavior I desire.

avatar image Rob 4 · Dec 21, 2010 at 08:00 AM 0
Share

Ok. Tried adding some code in javascript to stop the door from opening and closing repeatedly but didn't have any luck. Basically assigned a variable that would be changed after the door first opened, and tried to use that variable to stop the animation from being played again - but not working for some reason. I posted the relevant code snippet below in hopes someone could tell me where I messed up. Don't know if this is due to mistake on my part or if Unity scripting has to follow certain rules I'm not aware of.

Thanks - code in next post as running out of room:

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Animation playing over and over 0 Answers

Unity Animator cant play the same animation twice. 2 Answers

Character won't run straight 1 Answer

How do you properly attach Animation clips? 1 Answer

How to create additional animation inside Unity for models that already have some imported animations 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges