How to rotate camera diagonally over players shoulder while still facing towards players direction
Hello guys I made my own camera script but I can't figure out one thing.. I developed an over the shoulder style cam. Whenever the player moves his mouse horizontally, the player rotates around it's y axis (with the camera facing towards it's direction because it's a child in the player object). If he moves his mouse vertically, the camera will rotate around the player object (which works perfectly). However, what I wish to achieve is, when the player moves his mouse upwards, I want the camera to rotate up the x axis and y axis, so it rotates diagonally up to the players head and the camera kinda faces down from over the players shoulder.
From:
To here:
hope you still understand what I mean. At first I changed the y-axis parameter to the rotationXAxis (which I use to rotate the cam on it's x-axis) in the "toRotation"-Quaternion. Worked perfectly when I just moved my mouse upwards. However, when I move the mouse horizontally, the player will still rotate around it's y axis but the camera won't face towards the players direction no more. I tried my best thinking mathematically but I couldn't find a solution. Sorry for the weird description of this problem, I'm not a native speaker and it's hard for me to describe this hahah.
Thanks alot in advancé!
Here's my script: Code (CSharp):
void Update() {
mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
mouseY = -(Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime);
rotationXAxis += mouseY;
rotationXAxis = ClampAngle(rotationXAxis, -30f, 30f);
player.Rotate(Vector3.up * mouseX);
Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
Quaternion toRotation = Quaternion.Euler(rotationXAxis, transform.rotation.eulerAngles.y, rotationXAxis/8);
Quaternion rotation = toRotation;
Vector3 negDistance = new Vector3(xPosOffset, yPosOffset, -distance);
Vector3 position = rotation * negDistance + player.position;
transform.rotation = rotation;
transform.position = position;
mouseY = Mathf.Lerp(mouseY, 0, Time.deltaTime);
}
Your answer
Follow this Question
Related Questions
3d camera not working as I want it to 0 Answers
Camera always behind player 0 Answers
Code Optimization 1 Answer
Help rotating camera script 0 Answers
How to focus camera on back of object when key is pressed. 0 Answers