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 Chris 4 · Apr 14, 2011 at 07:12 PM · animationcollisioncolliderplayertag

Play annimation on collision not working.

I am very new to scripting here in unity. I have a script that is suppost to play when you collide into a tagged game object. The only problem is my script does not work. The player walks on the object and animation does not play. Player is suppost to walk on it making the platform go down slowly. (Going up causes the platform to go throught the character) Here is my script.

function OnCollisionEnter(collision : Collision)

{

if (collision.gameObject.tag == "Elevator")

{

animation.Play("Down22");

}

}

I am probably doing something very wrong. I cannot figure it out. Like I said I am new to unity scripting.

Comment
Add comment · Show 1
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 MC HALO · Apr 14, 2011 at 08:02 PM 0
Share

is your player a 2d object or a 3d object ?

1 Reply

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

Answer by MC HALO · Apr 14, 2011 at 08:15 PM

Ok this normally works for me. as you can see that at the moment your tag is the elevator, what this means is that once the player hits the elevator the animation will play. Now this does not work because when you collied with the object it will say the player does not have the animation attached to it. so what you need to do is attach this script to the Elevator object and add a tag to the main player called "Player". once you have done that in the script change the following:

var CloseCollision : boolean = false;

function OnCollisionEnter(collision : Collision)

{

if(CloseCollision == flase){ if (collision.gameObject.tag == "Player"){

animation.Play("Down22");

CloseCollision = true; } else{ CloseCollision = false; // read the description below to understand what is going on } } }

well if you notice on the top of the script i created a boolean called CloseCollision. Now what this does is it allows you to turn you collision on and off. So for example: now what will happen is if the player collieds with the elevator object it will play the animation then set the Collision boolean to true. once it is set to true the collision will stop this is because on the top of the following line "if (collision.gameObject.tag == "Player"){" i have add the following line "If(CloseCollision == false){" this line makes everything work. what it is saying to the system when you run the game check and see if the boolean is set to false so the box in the inspector mode should be unchecked if it is and if the player collides with the object it will play the animation. Now after the animation is over i said to the system "hey look" if this guy has hit the wall set the boolean to true. Once it is true the collision will no longer do anything that's because it will only run if the boolean check box is set to false. Now the "else" plays an important part if we have no collided with the object it will make the boolean remain false, that's all that does. This lines of code should now only make the animation play once :). make sure the script is attached to the elevator object not the player. i hope this helps. Oh yes makes sure you have the elevator animation attached to the elevator object in unity.

Comment
Add comment · Show 4 · 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 Chris 4 · Apr 14, 2011 at 09:18 PM 0
Share

It works $$anonymous$$y script was bad as expected. Now after i walk on to it it does the animation, however I walk when on the object and the animation restarts... I am sure I can find a way to stop that from happening. So thanks.

avatar image MC HALO · Apr 15, 2011 at 01:20 AM 0
Share

Hey i updated the script :)

avatar image Chris 4 · Apr 16, 2011 at 02:28 PM 0
Share

Cool, I am sure this will work:) I did it such a wimpy way but it works. I am sure this is more eficient though. I will try it as soon as I can.

avatar image MC HALO · Apr 16, 2011 at 04:11 PM 0
Share

:) lol ok no way is a wimpy way :)

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

Collision detection problem 1 Answer

Collision with animations 0 Answers

How to stop an animation on collision 0 Answers

Tags returning true before collision 2 Answers

Trigger animtaion when hit 1 Answer


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