Can't get rigidbody to move?

When I press the wasd or direction keys the cube doesn’t move.

the y value is set to .5

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

public class PlayerRBBehavior : MonoBehaviour
{
    // Start is called before the first frame update
    [SerializeField]
    private Rigidbody rb;

    private Vector3 inputVector;
   
    void Start()
    {
        rb = GetComponent<Rigidbody>();
       
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        inputVector = new Vector3(Input.GetAxis("Horizontal") * 10f, rb.velocity.y, Input.GetAxis("Vertical") *10f);
    }
}

All you are doing in this script is reading the input values into a vector. If you want to move the Rigidbody you will need to call AddForce or MovePosition or set its velocity.

Ok I put the following code, and it worked.

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

public class PlayerRBBehavior : MonoBehaviour
{
    // Start is called before the first frame update
    [SerializeField]
    private Rigidbody rb;

    private float speed = 2f;
   
    void Start()
    {
        rb = GetComponent<Rigidbody>();
       
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");

        Vector3 inputVector = new Vector3(h, 0, v);
        inputVector = inputVector.normalized * speed * Time.deltaTime;


        rb.MovePosition(transform.position + inputVector);
    }
1 Like