我想制作一种返回玩家面对的位置的方法
我制作了一个简单的脚本来检查玩家面临的位置,并在动画仪中放置在我的动画师
1 = up
2 =右
3 = down
4 =左右,
private Vector2 velocity;
private Animator animator;
private int direction;
private void Awake() {
animator = GetComponent<Animator>();
}
void Update(){
velocity.x = Input.GetAxisRaw("Horizontal");
velocity.y = Input.GetAxisRaw("Vertical");
switch(velocity){
case Vector2(0,1):
direction = 1;
break;
case Vector2(1,0):
direction = 2;
break;
case Vector2(0,-1):
direction = 3;
break;
case Vector2(-1,0):
direction = 4;
break;
}
animator.SetFloat("Facing",direction);
然后我得到错误
资产/脚本/playermovement.cs(21,25):错误CS8129:对于类型为“ vector2”,找不到合适的“解构”实例或扩展方法,具有2个OUT参数和一个void返回类型。
I made a simple script to check what position the player is facing and put that in my animator
1 = up
2 = right
3 = down
4 = left
private Vector2 velocity;
private Animator animator;
private int direction;
private void Awake() {
animator = GetComponent<Animator>();
}
void Update(){
velocity.x = Input.GetAxisRaw("Horizontal");
velocity.y = Input.GetAxisRaw("Vertical");
switch(velocity){
case Vector2(0,1):
direction = 1;
break;
case Vector2(1,0):
direction = 2;
break;
case Vector2(0,-1):
direction = 3;
break;
case Vector2(-1,0):
direction = 4;
break;
}
animator.SetFloat("Facing",direction);
then I get the error
Assets/Scripts/PlayerMovement.cs(21,25): error CS8129: No suitable 'Deconstruct' instance or extension method was found for type 'Vector2', with 2 out parameters and a void return type.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设
velocity
是类型vector2
您无法设置x
和y
的值,因为它们被读取 - 只有属性。而是使用此:
Assuming
velocity
is of typeVector2
you can't set the values ofx
andy
as they are read-only properties.Use this instead:
向量是结构,如果不使用
新
egcase new vector2(0f,1f):
,就无法创建它们。您可以使用一些已准备好的方向的捷径:Vectors are structs and you can't create them without using
new
e.g.case new Vector2(0f, 1f):
. There are some ready made shortcuts for direction built in that you can use:我相信您的问题是您尝试使用开关语句的方式,因为Vector(1,0)试图调用一个函数,而不是实际引用您所寻找的值的可行替代方案是使用时使用
像下面的示例
一样价值手动
I believe your issue comes with how you are trying to use your switch statement as Vector(1,0) is attempting to call a function not actually reference the value you are looking a viable alternative is to use
when
like in the following exampleI'm also using the shorthand
up
,left
,right
anddown
instead of defining the values manually