我想制作一种返回玩家面对的位置的方法

发布于 2025-02-04 12:17:00 字数 837 浏览 2 评论 0原文

我制作了一个简单的脚本来检查玩家面临的位置,并在动画仪中放置在我的动画师

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷鸟归林 2025-02-11 12:17:02

假设velocity是类型vector2您无法设置xy的值,因为它们被读取 - 只有属性。

而是使用此:

velocity = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));

Assuming velocity is of type Vector2 you can't set the values of x and y as they are read-only properties.

Use this instead:

velocity = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
北方的巷 2025-02-11 12:17:02

向量是结构,如果不使用 eg case new vector2(0f,1f):,就无法创建它们。您可以使用一些已准备好的方向的捷径:

    if(velocity == Vector2.up)
    {
        print("direction = 1");
    } else if (velocity == Vector2.right)
    {
        print("direction = 2");
    } else if (velocity == Vector2.up)
    {
        print("direction = 3");
    } else if (velocity == Vector2.down)
    {
        print("direction = 4");
    } else if (velocity == Vector2.left)
    {
        print("direction = 1");
    }

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:

    if(velocity == Vector2.up)
    {
        print("direction = 1");
    } else if (velocity == Vector2.right)
    {
        print("direction = 2");
    } else if (velocity == Vector2.up)
    {
        print("direction = 3");
    } else if (velocity == Vector2.down)
    {
        print("direction = 4");
    } else if (velocity == Vector2.left)
    {
        print("direction = 1");
    }
冷血 2025-02-11 12:17:01

我相信您的问题是您尝试使用开关语句的方式,因为Vector(1,0)试图调用一个函数,而不是实际引用您所寻找的值的可行替代方案是使用时使用像下面的示例

                case Vector2 v when v.Equals(Vector2.up):
                    Debug.Log("Up");
                    break;
                case Vector2 v when v.Equals(Vector2.left):
                    Debug.Log("Left");
                    break;
                case Vector2 v when v.Equals(Vector2.back):
                    Debug.Log("Back");
                    break;
                case Vector2 v when v.Equals(Vector2.right):
                    Debug.Log("Right");
                    break;

一样价值手动

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 example

                case Vector2 v when v.Equals(Vector2.up):
                    Debug.Log("Up");
                    break;
                case Vector2 v when v.Equals(Vector2.left):
                    Debug.Log("Left");
                    break;
                case Vector2 v when v.Equals(Vector2.back):
                    Debug.Log("Back");
                    break;
                case Vector2 v when v.Equals(Vector2.right):
                    Debug.Log("Right");
                    break;

I'm also using the shorthand up, left, right and down instead of defining the values manually

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文