Player Movement doesn't work

Now, I am new to unity, so a simple answer will help me a lot. I was working on a test platformer project. I was writing the player’s movement script and I suppose it should be correct. But when I run the project, my player doesn’t move at all. Any help will really be appreciated.

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    private float vaxis;
    private float haxis;
    public float moveSpeed = 40f;
    public float jumpSpeed = 20f;
    private Vector2 newForce = new Vector2(0f, 0f);
    public Rigidbody2D player;

    private void FixedUpdate()
    {
        haxis = Input.GetAxisRaw("Horizontal");
        vaxis = Input.GetAxisRaw("Vertical");
        if(vaxis == -1)
            vaxis = 0;

        newForce.x = moveSpeed * haxis * Time.fixedDeltaTime;
        newForce.y = jumpSpeed * vaxis * Time.fixedDeltaTime;
        player.AddForce(newForce);
    }
}

I have assigned the Rigidbody2D attached to my player to ‘player’ variable. I see nothing wrong.
Please help me. This is really frustrating.

Thanks,
Nandan Goyal

For AddForce to work “Is Kinematic” needs to be unchecked on the rigidbody

Hi!
First make sure that the “isKinematic” option is unchecked.
Don’t use Fixed update to check input. Use Update and store input in a variable and use fixed update to perform movement.
Use getcomponent to get the RIGIDBODY attached.
And don’t forget to attach the script to the player gameobject haha
Good luck!

Hey! So really your code is fine and should work - I’m manged to get your exact code working in a little test. Therefore something must have gone wrong in unity rather than the code. Lets go through the things to check:


I see you mentioning that the “isKinematic” box is unchecked - you shouldn’t actually have a “isKinematic” box, as your code references a Rigidbody2D, and “isKinematic” is only an option on 3D Rigidbodies. Is your Rigidbody the right type? If you are using a Rigidbody2D what you need to check is that the “Simulated” box is ticked, otherwise physics of your player object isn’t calculated.

You’ve said that you already have your player object attached to the PlayerMovement script, so really thee only other thing is to check if your inputs are configured in “Project Settings” so you can actually give x/y axis inputs.

The final pitfall I’ve got to admit I’ve fallen for at least once is how is your camera set up? If you have your camera locked to the player and nothing in the back ground to compare things to it will never look like the player moves as the camera tracks the player perfectly.

If none of these helps then I am as stumped as you!

Hi everyone.
First of all, I wanted to inform you guys that my problem is solved. The problem was simply in my speed variables. I was multiplying them with deltaTime, which made the applied force too weak to get the player moved.
I also wanted to thank you guys for all your suggestions and help. Thanks a lot.

It also do not works for me pls if you fixed that give me script pls