- Home /
Vertical camera rotation around player issues
Hi, I have a camera that rotates around a player that I can not seem to get right. The rotation around the horizontal axis works fine but when I add rotation around the vertical axis it can rotate freely (it uses all the axes, not just two). When I use them separately they work fine, other than that the vertical one has choppy/laggy movement that I want to get rid of aswell.
How can I solve this problem?
Video of issues: https://youtu.be/ddrwJVH7CWE
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCamera : MonoBehaviour {
GameObject player;
Vector3 cameraOffset;
Vector3 scrollOffset;
Vector3 zoom;
public Transform target;
public Transform cameraPos;
public float Y;
public float Z;
public float X;
public float angularSpeed = 350f;
private void Start()
{
player = GameObject.Find("Player");
cameraOffset = new Vector3(0, 5, -8);
transform.rotation = Quaternion.Euler(15, 0, 0);
transform.position = player.transform.position + cameraOffset;
if (target == null)
{
Debug.LogError("Assign a target for the camera in Unity's inspector");
}
}
public void Update()
{
X = cameraPos.transform.position.x - target.position.x;
Y = cameraPos.transform.position.y - target.position.y;
Z = cameraPos.transform.position.z - target.position.z;
scrollOffset = new Vector3((X / 10), (Y / 10), (Z / 10));
transform.position = player.transform.position + cameraOffset;
if (Input.GetAxis("Mouse ScrollWheel") > 0) //Zoom in
{
cameraOffset-= scrollOffset;
}
if (Input.GetAxis("Mouse ScrollWheel") < 0) //Zoom out
{
cameraOffset += scrollOffset;
}
}
private void LateUpdate()
{
transform.position = target.position + cameraOffset;
float movementHorizontal = Input.GetAxis("Mouse X") * angularSpeed * Time.deltaTime;
float movementVertical = Input.GetAxis("Mouse Y") * angularSpeed * Time.deltaTime;
if (!Mathf.Approximately(movementHorizontal, 0f))
{
transform.RotateAround(target.position, Vector3.up, movementHorizontal); //Rotation around horizontal axis
transform.RotateAround(target.position, -Vector3.right, movementVertical); //Rotation around vertical axis (choppy movement, laggy)
cameraOffset = transform.position - target.position;
}
}
}
Answer by C_R_E_A_T_R_O_R · Jun 07, 2018 at 11:15 PM
This is the default behavior of the Rotate function, fortunately - unfortunately, a workaround is to docsomething like:
transform.Rotate(-horizontal, vertical,0) And right after that: float z = transform.eulerAngles.z transform.Rotate(0, 0, -z)
That way you cancel the rotation around the z axis.
You could check if this works
I tried using the code but ins$$anonymous$$d of it focusing on the player rotating around it the camera rotates freely. Removing the transform.Rotate(-horizontal, vertical,0) part makes the horizontal movement work. $$anonymous$$oving the mouse vertically ins$$anonymous$$d makes the camera rotate one of the sides of the player. The remaining code did however lock the Z rotation.
Now I am stuck with the camera rotating around one side rather than over the top. I hope the image makes any sense.
Yeah, so this is called gimbal lock and it is caused when using euler angles (which rotateAround needs). What it means is that as two axis are rotating simultaniously they will also make the third one rotate. Check this: https://en.wikipedia.org/wiki/Gimbal_lock
This can be solved by using Quaternions, which can be a little bit weird at first glance. I believe that there is no built in function to rotateAround with Quaternions though... If i find something usefull i will let you know
Your answer
Follow this Question
Related Questions
Rotatearaound a moving object 1 Answer
How do I get the world space up direction for RotateAround? 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers