Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 skyyguy · Jun 24, 2016 at 04:23 AM · c#animationcollider

Cant play animation once on collision enter then another animation on collision exit? Called multiple times?

I am having a difficult problem here, reference my Stack overflow question: http://stackoverflow.com/questions/38001740/unity-c-sharp-cannot-make-object-move-down-from-current-y-position-once-on-co

I have platforms that spawn. On collision enter and exit, I want to play an animation ONCE to achieve this effect: When player lands on platform, platform moves down and STAYS DOWN. When they jump up, it moves up and STAYS UP. I've been at this hours - can't get it to work. Im desperate.

I first tried without animations:

 void OnCollisionEnter (Collision col) {
         GameObject platform = col.gameObject;
 
         //platform.GetComponent<Animation> ().Play ("down1");
         if (!playedAnim1) {
            platform.transform.position += new Vector3(platform.transform.position.x, -2, platform.transform.position.z);
 
             playedAnim1 = true;
         }
         touchingPlatform = true;
         Debug.Log ("entering platform");
     }

Trying to just move the platform 2 down from its original position. This did not work so I looked at http://forum.unity3d.com/threads/play-animation-on-collide.111775/ and created 2 animations. I had to stick my platform prefab in an empty game object so the platform would move just within the empty, so I don't know if this may be causing the issue but with this script:

 void OnCollisionEnter (Collision col) {
         GameObject platform = col.gameObject;
 
         //platform.GetComponent<Animation> ().Play ("down1");
         if (!playedAnim1) {
             platform.GetComponent<Animation> ().Play ("down1");
 
             playedAnim1 = true;
         }
         touchingPlatform = true;
         Debug.Log ("entering platform");
     }
 
     void OnCollisionExit (Collision col) {
 
          if (!playedAnim2) {
             platform.GetComponent<Animation> ().Play ("up1");
 
             playedAnim2 = true;
         }
 
         touchingPlatform = false;
         Debug.Log ("exiting platform");
     }

Despite using a boolean flag like the question, the platform moves up and down repeatedly and doesn't work. I don't know what to do and can't complete my project without this aspect.

I think I had to attach a renderer and collider to the empty in order for the prefab to trigger the oncollisionenter and I think the problem MAY be due to oncollisionenter/exit being called multiple times.. is there a way around this?

How can I play an animation ONCE and have it remain in the final position from that animation (i.e 2 down) when my player collides with the platform, then up when it leaves? Is it possible?

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

0 Replies

· Add your reply
  • Sort: 

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

187 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Trigger transition to another animation. 2 Answers

Multiple Cars not working 1 Answer

C#: Climb ledge while inside collider 1 Answer

Distribute terrain in zones 3 Answers

How to set parent collider equal to child collider visually? 2 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