C# Player Movement

I am a beginner and I’m trying to follow the UFO tutorial here on Unity. I have gone through the player movement video several times now, but when I try to test it my UFO won’t move no matter what I press.

public float speed;             //Floating point variable to store the player's movement speed.

    private Rigidbody2D rb2d;       //Store a reference to the Rigidbody2D component required to use 2D Physics.

    // Use this for initialization
    void Start()
    {
        //Get and store a reference to the Rigidbody2D component so that we can access it.
        rb2d = GetComponent<Rigidbody2D>();
    }

    //FixedUpdate is called at a fixed interval and is independent of frame rate. Put physics code here.
    void FixedUpdate()
    {
        //Store the current horizontal input in the float moveHorizontal.
        float moveHorizontal = Input.GetAxis("Horizontal");

        //Store the current vertical input in the float moveVertical.
        float moveVertical = Input.GetAxis("Vertical");

        //Use the two store floats to create a new Vector2 variable movement.
        Vector2 movement = new Vector2(moveHorizontal, moveVertical);

        //Call the AddForce function of our Rigidbody2D rb2d supplying movement multiplied by speed to move our player.
        rb2d.AddForce(movement * speed);
    }
}

This is my script. Is there something wrong with it?

Good day.

If the script is correct, the problem is on how you have the object in the scen, its components configurations, etc…

Look more, try to understand what are you doing in order to see what you miss!

Bye!

Hi @twingames07 ,

I tried this code in my already open 3D project (too lazy to start another 2D one), and just converted the RigidBody2D to RigidBody, and the 2D vectors into 3D. I created a cube, added a rigidbody component to it, and then dragged the script onto it. I also added a passive plane. At first, I was also surprised to see this not moving… then I realized there is a speed variable which was set to 0 by default. So I set it to 20 to see some awesome tumbling physics! :slight_smile:

Good luck!!!