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