- Home /
Question by
BuyinBrian · Apr 07, 2017 at 02:56 PM ·
vrfps
Move along X,Z if camera is looking up or down
I'm new to unity and having trouble figuring this out.
I have a VR game where the user uses a joystick to move around a room. The movement is always based on where the user is looking. So pushing up on the joystick will always move forward in the direction of the camera.
The one issue I'm having is that I would like someone to move forwards or backwards when they are looking up. Right now, the code prevents that from happening:
void Start () {
rb = GetComponent<Rigidbody>();
}
void FixedUpdate() {
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical );
//transform the movement based on where the camera is moving
Vector3 finalMovement = Camera.main.transform.TransformDirection(movement);
rb.AddForce(finalMovement * speed);
}
Comment
Your answer