I am new to Unity and I am following a tutorial, but when I write the code I get this error:
Cannot implicitly convert type 'UnityEngine.Rigidbody2D' to 'UnityEngine.Rigidbody'
The code:
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);
}
}
How can I fix this? I tried debugging and rewriting the code and the issue persists.
Your rb
variable in the PlayerCtrl
class has the type for a 3D rigidbody:
Rigidbody rb;
Change it to Rigidbody2D
instead:
Rigidbody2D rb;