Movement Script

First off I do not know if this is the right forum for this thread, what I am trying to do is make a script that test for a key pressed and add force to the object, I am very new to C# and Unity. The a and d key being pressed works, but when I press w and s it moves the object in the same way as the a and d keys. I took the code from a different project and tried to make it work for this, but I was not sure if I changed the right things.

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{

    // This is a refference to the Rigidbody component and it is called "rb"
    public Rigidbody rb;

    public float forwardForce = 500f;
    public float sidewarsForce = 500f;

    // Marked as FixedUpdate because unity likes that when messing around with physics
    void FixedUpdate()
    {
        if (Input.GetKey("w"))
        {
            rb.AddForce(forwardForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
        }                                                    // If statement only executed if condition is met
        if (Input.GetKey("d"))
        {
            rb.AddForce(sidewarsForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
        }

        if (Input.GetKey("a"))
        {
            rb.AddForce(-sidewarsForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
        }
        if (Input.GetKey("s"))
        {
            rb.AddForce(-forwardForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
        }
    }
}

-Mango

You are adding the force to the same part of the vector3. In the first one, you are adding it to the xposition, the first part of the vector.
forwardForce * Time,0,0

for sideways, you want the z portion of the vector
0,0,sidewarsForce*Time…

I’m surprised it works like that without an actual vector3, but if it’s working in one direction, it must be all right.

It’s just an overloaded version of AddForce that takes discrete axes values:

public void AddForce(float x, float y, float z, ForceMode mode = ForceMode.Force);

But your answer is absolutely the correct thing to fix OP’s original problem.

thanks