问题描述
在触发按钮上播放动画的代码似乎不起作用.我在Youtube上观看了一个视频,并使用一个简单的animation.Play();
对该视频进行了处理,但我无法在我的计算机上使用它.我做错了什么或团结改变了它?请帮助我在网上找不到解决方案.所有解决方案也不起作用".
The code to play animation on trigger button does not seem to work. I saw a video on Youtube and with a simple animation.Play();
it worked on that video but yet, I couldn't get it to work on my computer. What did I do wrong or did unity change it? please help I cant find solution on the net. All the "solution not working either".
这是我得到的错误:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class animationtrigger : MonoBehaviour {
void Start()
{
}
int speed = 10;
// Update is called once per frame
void Update () {
if (Input.GetKeyDown("N"))
{
animation.Play("Cube|moving side");
//transform.Translate(1 * Vector3.forward * Time.deltaTime * speed);
//Animator anim = GetComponent<Animator>();
//if (null != anim)
// {
// anim.Play("Cube|moving side");
// }
}
}
}
推荐答案
团结一致.我已经看到相似过去几周的问题.尽管我不认为它们是重复的,但在这里为以后的问题回答所有这些都是很有意义的.
Unity changed. I've seen similar questions for the past few weeks. Although I don't think they are duplicates but it would make sense to answer all these here for future questions.
animation
变量在Component
下定义为MonoBehaviour
继承的public
变量.然后,您的代码将从MonoBehaviour
继承,您可以访问animation
.
The animation
variable is defined under Component
as a public
variable which MonoBehaviour
inherits from. Your code then inherits from MonoBehaviour
and you have access to animation
.
这些是Component
类中不推荐使用的变量的完整列表:
These are the complete list of variables from the Component
class that are deprecated:
public Component animation { get; }
public Component audio { get; }
public Component camera { get; }
public Component collider { get; }
public Component collider2D { get; }
public Component constantForce { get; }
public Component guiElement { get; }
public Component guiText { get; }
public Component guiTexture { get; }
public Component hingeJoint { get; }
public Component light { get; }
public Component networkView { get; }
public Component particleEmitter { get;
public Component particleSystem { get; }
public Component renderer { get; }
public Component rigidbody { get; }
public Component rigidbody2D { get; }
访问附加到相同脚本的组件的新方法:
使用GetComponent<ComponentName>()
.大写该变量的 first 字母使其成为组件类.音频的一种例外是AudioSource
而不是音频.
Use GetComponent<ComponentName>()
. Capitalize the first letter of that variable to make it its component class. One exception is audio which becomesAudioSource
instead of Audio.
1 .animation.Play("Cube|moving side");
变为GetComponent<Animation>().Play("Cube|moving side");
2 .rigidbody2D.velocity
变为GetComponent<Rigidbody2D>().velocity
3 .rigidbody.velocity
变为GetComponent<Rigidbody>().velocity
4 .renderer.material
变为GetComponent<Renderer>().material
5 .particleSystem.Play()
变为GetComponent<ParticleSystem>().Play()
6 .collider2D.bounds
变为GetComponent<Collider2D>().bounds
7 .collider.bounds
变为GetComponent<Collider>().bounds
8 .audio.Play("shoot");
变为GetComponent<AudioSource>().Play("shoot");
9 .camera.depth
变为GetComponent<Camera>().depth
我不必将所有内容都放进去,因为下面的示例应指导任何人这样做.这些是SO上最常问到的问题.
I don't have to put all of them because the example below should guide anyone do this. These are the most asked and asked components on SO.
缓存组件:
您可以缓存组件,这样就不必每次都拥有GetComponent.
You can cache component so that you don't have be GetComponent everytime.
Animation myAnimation;
void Start()
{
//Cache animation component
myAnimation = GetComponent<Animation>();
}
void Update()
{
if(somethingHappens)
{
//No GetComponent call required again
myAnimation.Play("Cube|moving side");
}
}
这篇关于将旧的Unity代码升级到Unity 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!