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 sean1231 · Jun 16, 2012 at 07:34 AM · animationcolliderbuttonplay

Play animation when in collider and button pressed

I am trying to play an animation when the player is in a colider and he/she presses a button e.g. "e"

I tried to get this script to work:

function OnCollisionStay()

{

if(Input.GetKey("e"))

animation.Play("Test_an");

}

But this does not work, Any ideas?

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

Answer by syclamoth · Jun 16, 2012 at 07:40 AM

By 'in a collider', what do you mean? There are several things which could be going wrong here.

For starters, the physics engine will only detect collisions (and send the messages you need for this kind of behaviour) if there is at least one non-kinematic rigidbody involved in the collision. Of course, a non-kinematic rigidbody will immediately try to extract itself from inside any collider it finds itself within- the 'OnCollisionStay' message is referring to objects remaining in contact, not necessarily intersecting.

In this case, you should probably be using a trigger volume. Set the collider you are trying to test to 'isTrigger', and then use the 'OnTriggerStay' message, instead.

If your character is not controlled by a rigidbody, you will have to attach a rigidbody component to it and set it to 'isKinematic'. This will allow it to receive trigger messages (although it will still not receive collision messages from other kinematic or static colliders).

Comment
Add comment · Show 1 · 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 sean1231 · Jun 17, 2012 at 12:23 AM 0
Share

Thankyou for the help!

If anyone else has run into the same problem, Here is my code:

var anim : String; var AnimationSource = gameObject;

function OnTriggerStay() { if(Input.Get$$anonymous$$ey("e"))

 AnimationSource.animation.Play(anim);

}

Just attatch this code to the trigger and attatch the animation to a gameObject.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Activating One Part of An Animation 2 Answers

What am I doing wrong with this attack script? 2 Answers

what is the jump axis for if (Input.GetAxis(" ? ") 2 Answers

How to activate someting if a key is held down? 2 Answers

Animation Script not working. 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