- Home /
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?
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
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();
}
}
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?
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
Actually i'm using EZ GUI to build my GUI elements (EZ GUI uses Empty GameObjects)
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
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?
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 );