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 MrSlher · Jan 30, 2012 at 05:19 PM · animationgameobjectbuttontoggle

GameObject as a toggle button to play character Animation

Hi everybody.

I wanted to know if there is any way to use a GameObject as a Toggle button to play a character animation.

I have a character on the middle of my scene, and 5 (Cube) Game Object on the side. I want to assign to each of these GO a character animation (Jump/Run/Walk/...), so when I clic on the Run GameObject the character starts to run. And When I clic on the GO again the character stops to run and returns to idle position.

Could anyone help?

Comment
Add comment · Show 5
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 Hanseshadow.du · Jan 30, 2012 at 05:39 PM 0
Share

You probably want a GUI button, but I don't have enough information to deter$$anonymous$$e that. So, you would want to capture mouse down and mouse up on a game object.

http://answers.unity3d.com/questions/156371/detecting-a-mousedown-on-gameobject-onmousedown-or.html

avatar image Hanseshadow.du · Jan 30, 2012 at 05:40 PM 0
Share

On$$anonymous$$ouseDown hasn't worked for me on Android devices, so I use something that works on multiple platforms for my game object buttons (collider.Raycast()).

     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

     RaycastHit hit;

 if(collider.Raycast(ray, out hit, 100f))
     {
         if(Input.Get$$anonymous$$ouseButtonDown(0))

     {
         $$anonymous$$ouseDown();
     }

     if(Input.Get$$anonymous$$ouseButtonUp(0))
     {
             $$anonymous$$ouseUp();
     }
     }
avatar image MrSlher · Jan 30, 2012 at 08:05 PM 0
Share

Thanks for the answer. Yes, I'm trying to do a GUI Button. As I said above, I'm not very good at scripting, so I don't understand of this piece of code.

I guess that this script needs to be on the GUI GameObject, But I have two camera, have I to specify from which the ray is from?

Is it good if I do something like that? :

if(collider.Raycast(ray, out hit, 100f)) {

     if(Input.Get$$anonymous$$ouseButtonDown(0)){
     animation.Play ("run");

} { $$anonymous$$ouseDown(); }

 if(Input.Get$$anonymous$$ouseButtonUp(0))
 { animation.Play ("idle");
 }
  { 
         $$anonymous$$ouseUp();
 }
 }

Since I've 2 characters in my scene, have I to name each animation in function of the characters? for exemple "Tico_run" ins$$anonymous$$d of just "run"? Or have I to add a piece of script to the character?

avatar image Hanseshadow.du · Jan 30, 2012 at 08:22 PM 0
Share

The code I posted is for a game object in the scene and not a GUI element. You should check out the GUI documentation for buttons. It has code for click events. :)

http://unity3d.com/support/documentation/ScriptReference/GUI.Button.html

avatar image MrSlher · Jan 30, 2012 at 08:28 PM 0
Share

Actually i'm using EZ GUI to build my GUI elements (EZ GUI uses Empty GameObjects)

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Berenger · Jan 30, 2012 at 05:38 PM

Attach a collider on each of your cubes, then in their script add the function OnMouseDown, then toggle a boolean like animate = !animate

Comment
Add comment · Show 2 · 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 MrSlher · Jan 30, 2012 at 07:28 PM 0
Share

Thank you for the answer, and let me apologize because I'm not very good at scripting. I've no problems for the first part,and I think that Hanseshadow gave me the explanations of the second. But I don't realy get what you mean for "toggle a boolean like animate = !animate". I understand that the GUI button needs to have 2 states (activate/desactivate), but how can i use that with the piece of script which is below? And how can I use this to control my character?

avatar image Berenger · Jan 30, 2012 at 07:48 PM 0
Share

Actually, both the override of the function On$$anonymous$$ouseDown (look here = http://unity3d.com/support/documentation/ScriptReference/$$anonymous$$onoBehaviour.On$$anonymous$$ouseDown.html) and the cast of a ray at the event clic Hanseshadow solution) are valid, knowing that you don't need the position of the clic.

For the boolean part, it was more theoric. In your case, you need to stop the animation if it's playing, or start a new one if not. So once you successfully received the event clic, try something like :

// The object have been clicked

if( animation.isPlaying ) animation.Stop();

else animation.Play( myClip );

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

6 People are following this question.

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

Related Questions

Can we do Animation for Game objects (child or parent) using control on Buttons like Play, Pause, Reverse or Forward in Unity 5 by C# script ? 1 Answer

Game Object Toggle GUI Button Errors 1 Answer

Help touch button 1 Answer

UI Button Pressed color motion don't reset to Normal color 1 Answer

Button animation state stays Pressed or Highlighted after disabling GameObject 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