简体   繁体   English

在游戏中按a按钮播放动画

[英]Press a in game Button to play a animation

I recently thought about adding a InGame Button to my Game. 我最近考虑过要在游戏中添加InGame按钮。 It's not a GUI or UI Button, it's a Block, added to a Wall for example. 它不是GUI或UI按钮,而是一个块,例如添加到Wall。

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? 那么,如何将特定的块连接到按钮,如果按e,就在特定的块上播放动画? Please keep in mind that I'm new in Unity. 请记住,我是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. 在编辑器中时,选择您的Door,然后按ctrl + 6打开动画窗口。 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 . 创建完动画后 ,您的块对象将附加一个新脚本: 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 动画师可以具有定义下一个要播放的动画的变量(例如bool)

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 状态Press将包含速度为1的动画“ press”。状态UnPress将包含速度为-1的动画“ press”

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) 然后,仍在动画器窗口中,您将在Idle和其他两个状态之间创建链接,并添加一个称为“ OnPress”的触发条件(例如)

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 http://docs.unity3d.com/Manual/animeditor-UsingAnimationEditor.html http://docs.unity3d.com/Manual/AnimatorWindow.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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