Unity 2D 核心中无法将类型“UnityEngine.Rigidbody2D”隐式转换为“UnityEngine.Rigidbody”错误

问题描述 投票:0回答:1

所以我是 Unity 的新手,我正在遵循教程。 https://www.youtube.com/watch?v=34fgsJ2-WzM 但是当我编写代码时出现此错误。如果这是一个愚蠢的问题,我很抱歉。提前谢谢。

代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerCtrl : MonoBehaviour
{

    public float movSpeed;
    float speedX, speedY;
    Rigidbody rb;

// Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();


    }

// Update is called once per frame
    void Update()
    {
        speedX = Input.GetAxisRaw("Horizontal") * movSpeed;
        speedY = Input.GetAxisRaw("Vertical") * movSpeed;
        rb.velocity = new Vector2(speedX, speedY);
    }
}

我尝试调试并重写代码,问题仍然存在。

c# unity-game-engine 2d unityscript
1个回答
0
投票

您的

rb
变量是刚体的 3D 版本

Rigidbody rb;

改为

Rigidbody2D

Rigidbody2D rb;
© www.soinside.com 2019 - 2024. All rights reserved.