按游戏中的按钮播放动画
Press a in game Button to play a animation
我最近想在我的游戏中添加一个 InGame 按钮。它不是 GUI 或 UI 按钮,它是一个块,例如添加到墙上。
虚拟代码:
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
}
}
}
那么如何将特定块连接到按钮,如果我按下 e,则只在特定块上播放动画?请记住,我是 Unity 的新手。
感谢您的帮助!
我不知道你是否已经成功地创建了你的动画,所以我将从头开始解释。在编辑器中,select 你的门,然后按 ctrl+6 打开动画 window。从这里您可以为您的块设置动画。当您完成 animation 的创建后,您的块对象将附加一个新脚本:animator。在animator中可以看到animator状态机window
这是两个不同的东西:
Animation: 定义单个动画(平移、旋转、颜色变化……)
Animator:定义相应游戏对象的动画发生时间。动画师可以有变量(例如,布尔值)来定义下一个要播放的动画
任何对象都可以有一个动画器(你的按钮可以有一个在按下时移动。你的门可以有另一个来打开/关闭)
例如,在您的按钮动画器中,您应该具有三种状态:Idle、Press、UnPress。
状态 Press 将包含速度为 1 的动画 "press"。状态 UnPress 将包含动画“按速度 -1
然后,仍然在动画器中 window,您将在 Idle 和其他两个状态之间创建链接,并添加一个名为 "OnPress" 的触发条件(例如)
您可以用同样的方法来为您的门设置动画
然后在您的 Button 代码中编写
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
}
}
}
然后你可以添加另一个触发器来取消按下按钮
还有一件事:当你说"Connect the button to the block"时,我觉得你误解了什么:你的按钮脚本应该已经添加到编辑器的块中
查看这两个链接以获取有关动画的更多信息:
http://docs.unity3d.com/Manual/animeditor-UsingAnimationEditor.html
http://docs.unity3d.com/Manual/AnimatorWindow.html
我最近想在我的游戏中添加一个 InGame 按钮。它不是 GUI 或 UI 按钮,它是一个块,例如添加到墙上。
虚拟代码:
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
}
}
}
那么如何将特定块连接到按钮,如果我按下 e,则只在特定块上播放动画?请记住,我是 Unity 的新手。 感谢您的帮助!
我不知道你是否已经成功地创建了你的动画,所以我将从头开始解释。在编辑器中,select 你的门,然后按 ctrl+6 打开动画 window。从这里您可以为您的块设置动画。当您完成 animation 的创建后,您的块对象将附加一个新脚本:animator。在animator中可以看到animator状态机window
这是两个不同的东西:
Animation: 定义单个动画(平移、旋转、颜色变化……)
Animator:定义相应游戏对象的动画发生时间。动画师可以有变量(例如,布尔值)来定义下一个要播放的动画
任何对象都可以有一个动画器(你的按钮可以有一个在按下时移动。你的门可以有另一个来打开/关闭)
例如,在您的按钮动画器中,您应该具有三种状态:Idle、Press、UnPress。 状态 Press 将包含速度为 1 的动画 "press"。状态 UnPress 将包含动画“按速度 -1
然后,仍然在动画器中 window,您将在 Idle 和其他两个状态之间创建链接,并添加一个名为 "OnPress" 的触发条件(例如)
您可以用同样的方法来为您的门设置动画
然后在您的 Button 代码中编写
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
}
}
}
然后你可以添加另一个触发器来取消按下按钮
还有一件事:当你说"Connect the button to the block"时,我觉得你误解了什么:你的按钮脚本应该已经添加到编辑器的块中
查看这两个链接以获取有关动画的更多信息:
http://docs.unity3d.com/Manual/animeditor-UsingAnimationEditor.html http://docs.unity3d.com/Manual/AnimatorWindow.html