代码之家  ›  专栏  ›  技术社区  ›  Leo Jay

无法隐式转换类型“UnityEngine”。Rigidbody2D到UnityEngine。Unity 2D内核中的刚体错误

  •  -1
  • Leo Jay  · 技术社区  · 1 年前

    我是Unity的新手 following a tutorial ,但当我编写代码时,我会收到以下错误:

    无法隐式转换类型“UnityEngine”。Rigidbody2D到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);
        }
    }
    

    我该怎么解决这个问题? 我尝试调试和重写代码,但问题仍然存在。

    1 回复  |  直到 1 年前
        1
  •  1
  •   ipodtouch0218    1 年前

    你的 rb 变量在 PlayerCtrl 类的类型为 3D rigidbody :

    Rigidbody rb;
    

    更改为 Rigidbody2D 相反:

    Rigidbody2D rb;