C# W、 团结中的A、S、D运动,don';我不知道如何使用新功能
您好,我有一个问题与动画控制器脚本用于播放动画取决于哪个键被按下C# W、 团结中的A、S、D运动,don';我不知道如何使用新功能,c#,unity3d,animation,C#,Unity3d,Animation,您好,我有一个问题与动画控制器脚本用于播放动画取决于哪个键被按下 using System.Collections; using System.Collections.Generic; using UnityEngine; public class AnimationController : MonoBehaviour { function UpdateAnimations() { if (Input.GetKeyDown(KeyCode
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimationController : MonoBehaviour
{
function UpdateAnimations()
{
if (Input.GetKeyDown(KeyCode.W))
{
animation.CrossFade("goup");
}
else if (Input.GetKeyDown(KeyCode.A))
{
animation.CrossFade("goleft");
}
else if (Input.GetKeyDown(KeyCode.D))
{
animation.CrossFade("goright");
}
else if (Input.GetKeyDown(KeyCode.S))
{
animation.CrossFade("godown");
}
}
void Start()
{
}
// Update is called once per frame
void Update()
{
UpdateAnimations();
}
上面说“Component.animation”太旧了,我应该使用GetComponent,但我不知道如何使用它。首先,你的意思很可能是
void
,而不是function
,因为你的代码在c#
中,而不是not(到目前为止,它也被长期弃用)
然后是的,你那里的代码有多旧?像这样的直接访问在几年前就被弃用了^^
正如错误已经告诉您的那样,请使用例如like例如
public class AnimationController : MonoBehaviour
{
// Reference this via the Inspector in Unity
// via drag and drop. Then you don't need GetComponent at all
[SerializeField] private Animation _animation;
private void Awake()
{
// or as fallback get it on runtime
if(!_animation) _animation = GetCompoenent<Animation>();
}
// Update is called once per frame
private void Update()
{
UpdateAnimations();
}
private void UpdateAnimations()
{
if (Input.GetKeyDown(KeyCode.W))
{
_animation.CrossFade("goup");
}
else if (Input.GetKeyDown(KeyCode.A))
{
_animation.CrossFade("goleft");
}
else if (Input.GetKeyDown(KeyCode.D))
{
_animation.CrossFade("goright");
}
else if (Input.GetKeyDown(KeyCode.S))
{
_animation.CrossFade("godown");
}
}
}
公共类AnimationController:MonoBehavior
{
//通过Unity中的Inspector引用此内容
//通过拖放。那么您根本不需要GetComponent
[SerializeField]私有动画\u动画;
私人空间
{
//或者作为后备,在运行时获取它
如果(!\u animation)\u animation=getcomponent();
}
//每帧调用一次更新
私有void更新()
{
UpdateAnimations();
}
私有void UpdateAnimations()
{
if(Input.GetKeyDown(KeyCode.W))
{
_动画。交叉淡入(“goup”);
}
else if(Input.GetKeyDown(KeyCode.A))
{
_动画。交叉淡入(“高尔夫”);
}
else if(Input.GetKeyDown(KeyCode.D))
{
_动画。交叉淡入(“goright”);
}
else if(Input.GetKeyDown(KeyCode.S))
{
_动画。交叉淡入(“货仓”);
}
}
}
您是如何在代码中定义“动画”的
您可以尝试在代码顶部添加动画引用:
private Animation animation;
在Start()或Awake()方法中,添加:
// will add reference to the animation to be the Animation component
// in this GameObject
animation = GetComponent<Animation>();
//将添加对要作为动画组件的动画的引用
//在这个游戏对象中
动画=GetComponent();
非常感谢你,伙计