- Home /
Rotation on X and RotateAround on Y also rotating Z axis
Hello, I am creating a third person controller with an Xbox One Controller. I have the main camera as a child of the player. I am trying to rotate the Player on the Y axis with "RightJoystickX" (looking left and right), then rotating the camera AROUND the player (slightly above) on the X axis. This results in it also rotating on the Z axis. I've read several posts as to why it is doing this, and have found some solutions, however they're for using the mouse position on the screen, which does not help. I also found a solution if I were NOT rotating AROUND an object. (Which is using two different Rotate()'s, one for X and one for Y axis.) However, RotateAround messes that up and does not work.
I plan on using this controller or a shooting game.
Here is my code:
void Update(){
//Player movement
movementVector.x = Input.GetAxis("LeftJoystickX") * movementSpeed;
movementVector.z = Input.GetAxis("LeftJoystickY") * -movementSpeed;
movementVector = gameObject.transform.TransformDirection(movementVector);
//Rotate character and camera on Y axis
characterLookVector = new Vector3(0, Input.GetAxis("RightJoystickX"), 0);
transform.Rotate(characterLookVector * lookSpeed * Time.deltaTime);
//Rotate camera around player on X axis
cameraLookVector = new Vector3(Input.GetAxis("RightJoystickY"), 0, 0 );
rotatePosition = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y + 2f, gameObject.transform.position.z);
Camera.main.transform.RotateAround(rotatePosition, cameraLookVector, lookSpeed * Time.deltaTime);
//Jumping
if(characterController.isGrounded){
movementVector.y = 0;
if(Input.GetButtonDown("A")) movementVector.y = jumpPower;
}
movementVector.y -= gravity * Time.deltaTime;
characterController.Move(movementVector * Time.deltaTime);
}
I've tried so many solutions, but none seem to be working using a joystick controller. I fear it may be some complicating work to solve this issue but I'm really not sure. Please help! :)
Your answer
Follow this Question
Related Questions
joystick script for 3rd person controller 1 Answer
2D shooting with mobile joystick 1 Answer
2D top down projectile with joystick problem 1 Answer
move object back and forth with joystick input 0 Answers
2D Vertical Shooter Joystick Setup 0 Answers