- Home /
Camera Relative Movement With Rigidbody.AddForce
//Movement
move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
rigidbody.AddForce(move.normalized * currentSpeed);
This is my code for my players movement. I've been searching for answers to this but I can't find any that work with rigidbody.AddForce.
I just want the x and z axes of my player movement to be based on the cameras x and z, but I don't know how to do this. Pretty new to Unity and this is confusing for me since addforce is already local...
Answer by Ed unity · Apr 17, 2014 at 09:05 PM
Try adding a character controller to the Player instead of a Rigidbody and then call CharacterController.Move(move) passing in your "move" vector.
https://docs.unity3d.com/Documentation/ScriptReference/CharacterController.Move.html
Answer by Lazdude17 · Apr 17, 2014 at 11:05 PM
Solved it just by using this instead //Up if (Input.GetAxis("Vertical") > 0.2f) { rigidbody.AddForce(Camera.main.transform.forward * currentSpeed); }
But now I'm just getting cardinal directions with no interpolation...Is there a way I could fix this?`//Up if (Input.GetAxis("Vertical") > 0.2f) { move = (Camera.main.transform.forward); rigidbody.AddForce(move.normalized * currentSpeed); }
//Down
if (Input.GetAxis("Vertical") < -0.2f)
{
move = (-Camera.main.transform.forward);
rigidbody.AddForce(move.normalized * currentSpeed);
}
//Left
if (Input.GetAxis("Horizontal") < -0.2f)
{
move = (-Camera.main.transform.right);
rigidbody.AddForce(move.normalized * currentSpeed);
}
//Right
if (Input.GetAxis("Horizontal") > 0.2f)
{
move = (Camera.main.transform.right);
rigidbody.AddForce(move.normalized * currentSpeed);
}
//Face Direction of $$anonymous$$ovement
transform.LookAt(transform.position + move);`