- Home /
Moving a Rigidbody Relative to Camera Direction
Basically, I made a cube that kind of bounces around using a RigidBody. The player is able to control the direction the cube bounces (left, right, forward, backward), and the camera rotates around the player. I have both the camera a child of an Empty GameObject that keeps track of the Player object without affecting it. Both that and the player objects inside an Empty GameObject.
The problem I'm having is, when the camera is rotated, the cube doesn't move according to the direction the camera is facing. As in, when the camera has been rotated 180 degrees, a forward input sends the cube bouncing backwards relative to the camera. I am very new to Unity and would love some help with this.
This is the original method where I handled the movement (other than the bouncing). Everything I tried to fix my problem just ended in disaster.
void FixedUpdate () {
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
Thank you in advance to anyone willing to help!