My cube suddenly stopped moving out of the blue

Hello, I am new to unity and was the following beginner game tutorial videos from Brackeys and everything was going absolutely fine until I hit a brick wall from no where. Just yesterday, my cube was moving perfectly and it was such a great feeling but today I ran my game without changing anything and it just won’t move. I have no idea why and I have been trying to solve this problem for almost 5 hours with no luck. This is my rigidbody and the force and sideways force I am applying to my cube:
173830-screenshot-489.png

173831-screenshot-490.png

This is my player movement code:

using UnityEngine; 
public class PlayerMovement2 : MonoBehaviour
{
    public Rigidbody rb;
    public float forwardForce = 2000f;
    public float sidewaysForce = 100f;
    // Update is called once per frame
    //Fixedupdate is for unity physics, update is for anything else that's not in the start() and does not include physics 
    void Update()
    {
        //add a forward force
        rb.AddForce(0,0, forwardForce * Time.deltaTime);
       if (Input.GetKey("d")) {
        rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
       }
       if (Input.GetKey("a")) {
        rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
       }
    }
}

I would really like to know the problem here and how to solve it.

Is the public Rigidbody rb set to the correct rigidbody in your scene? It should be the rigidbody of your player, but judging by the name of it, it's set to player1. Should there be two player objects in your scene? (as well as two player movement scripts). ---------- Another helpful thing would be to debug if (Input.GetKey("d")) is called properly. So you could add a Debug.Log("d was pressed") in that if statement, just to see if it isn't a problem with your key setup. (unlikely). @sneake

1 Answer

1

Your script name is Player movement 2 and the rb variable is assigned to player1. You might be putting the wrong game object in your rb variable. Or you have the wrong script attached (maybe you duplicated an empty script and used that ?)