简体   繁体   中英

Press a in game Button to play a animation

I recently thought about adding a InGame Button to my Game. It's not a GUI or UI Button, it's a Block, added to a Wall for example.

Dummy Code:

OnTriggerEnter(c:Collider)  { 
    if(c.gameObject.tag =="Player") 
    {
       //Text = "E to interact!" 

      if(key.pressed("e")
      {
         //Connect the Button to a specific Block,      play a Animation
      } 
    }  
 }

So how do I connect a specific Block to the Button, and if I press e, Play the Animation just on the specific block? Please keep in mind that I'm new in Unity. Thanks for helping out!

I don't know if you already managed to create your animation, so I-ll explain from scratch. When in the editor, select your Door and press ctrl+6 to open the animation window. From here you can animate your block. When you will be done creating the animation , your block object will have a new script attached to it: an animator . You can see the animator state machine in the animator window

These are two different things:

  • Animation : Defines a single animation (translation, rotation, change of color, ...)

  • Animator : defines when an animation occurs for the corresponding gameobject. An animator can have variables (for example, a bool) that define which is the next animation to be played

Any object can have an animator (your button can have one to move when it is pressed. You door can have another one to open / close)

For instance, in your button animator, you should have three states: Idle, Press, UnPress. The state Press will contain the animation "press" with speed 1. The state UnPress will contain the animation "press with speed -1

Then, still in the animator window, You will create links between Idle and the two other states and add a trigger condition called "OnPress" (for example)

You can do the same to animate your door

In your Button code, you will then write

public Animator Door; // In the editor, give a reference to your door. It must have an Animator script for this to work

OnTriggerEnter(c:Collider)  { 
    if(c.gameObject.tag =="Player") 
    {
        //Text = "E to interact!" 

        if(key.pressed("e")
        {
           GetComponent<Animator>().SetTrigger("OnPress"); // The button's animator goes to "pressed" state
           Door.SetTrigger("Open");  // The door's animator goes to "open" state
        } 
    }  
}

Then you could add another trigger to unpress the button

One more thing: When you say "Connect the button to the block", I feel like you misunderstood something: Your button script should be already added to the block in the editor

Look at these two links for more information on animations:

http://docs.unity3d.com/Manual/animeditor-UsingAnimationEditor.html http://docs.unity3d.com/Manual/AnimatorWindow.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM