Rigidbody Not Rotating
So I have a rigidbody linked with a camera to allow for first person movement. While the camera moves well, the rigidbody isn't working. While the moving and physics work well, moving forward, backward, etc. use the original direction of the camera. For example, if I turned in the opposite direction of where I started the game facing, pressing "W" moves me backwards, not forwards. I tried making the rigidbody rotate with the mouse, but that doesn't seem to work. I can't tell if it's just that I didn't code it rotating right or what.
public class walk : MonoBehaviour
{
public float thrust = 5.0f;
public float speed = 50;
public Rigidbody rb;
private int mousex;
public int mouseThreshold = 2;
public float rospeed = 15.0f;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate()
{
if (Input.GetKey("a"))
{
rb.AddForce(force: Vector3.left * thrust * speed);
}
if (Input.GetKey("d"))
{
rb.AddForce(force: Vector3.right * thrust * speed);
}
if (Input.GetKey("w"))
{
rb.AddForce(force: Vector3.forward * thrust * speed);
}
if (Input.GetKey("s"))
{
rb.AddForce(force: Vector3.back * thrust * speed);
}
rb.transform.Rotate(0f, Input.GetAxis("Mouse X"), 0f);
}
}
Comment