Howdy I’m working on a third person game. I have a problem with the camera when I rotate it on the Y axis. This changes its position X when it reaches a certain rotation. I want to keep the X position that I have when I rotate the camera but I do not succeed … Here is a video with what happens: bandicam 2020-04-03 04-58-41-522.mp4 - Google Drive
Below is the code:
cameraController.cs
public class CameraController : MonoBehaviour
{
public Transform target; //Player
public Transform pivot; //Pivot for rotating camera
public Vector3 offset;
public bool useOffsetValues;
public float rotateSpeed;
public float maxViewAngle;
public float minViewAngle;
public bool invertY;
void Start()
{
if (!useOffsetValues)
{
offset = target.position - transform.position;
}
pivot.transform.position = target.transform.position;
//pivot.transform.parent = target.transform;
pivot.transform.parent = null;
Cursor.lockState = CursorLockMode.Locked; //Hides Mouse's Cursor
}
void LateUpdate()
{
pivot.transform.position = target.transform.position;
//Rotating Player by Mouse X Axis
float horizontal = Input.GetAxis("Mouse X") * rotateSpeed;
pivot.Rotate(0, horizontal, 0);
//Rotating Player's Pivot by Mouse Y Axis
float vertical = Input.GetAxis("Mouse Y") * rotateSpeed;
//pivot.Rotate(-vertical, 0, 0);
if (invertY)
{
pivot.Rotate(vertical, 0, 0);
} else
{
pivot.Rotate(-vertical, 0, 0);
}
//Limit up/down Camera rotation
if (pivot.rotation.eulerAngles.x > maxViewAngle && pivot.rotation.eulerAngles.x < 180f)
{
pivot.rotation = Quaternion.Euler(maxViewAngle, pivot.rotation.eulerAngles.y, 0);
}
if (pivot.rotation.eulerAngles.x > 180f && pivot.rotation.eulerAngles.x < 360f + minViewAngle)
{
pivot.rotation = Quaternion.Euler(360f + minViewAngle, pivot.rotation.eulerAngles.y, 0);
}
//Rotate Camera by Mouse X Axis
float desiredYAngle = pivot.eulerAngles.y;
float desiredXAngle = pivot.eulerAngles.x;
Quaternion rotation = Quaternion.Euler(desiredXAngle, desiredYAngle, 0);
transform.position = target.position - (rotation * offset);
//Camera follow Player
if (transform.position.y < target.position.y)
{
transform.position = new Vector3(transform.position.x, target.position.y -.5f, transform.position.z);
}
transform.LookAt(target);
}
}
playerController.cs
public class PlayerController : MonoBehaviour
{
public float moveSpeed;
public float jumpForce;
public CharacterController controller;
public float gravityScale;
public Animator anim;
public Transform pivot;
public float rotateSpeed;
public GameObject playerModel;
private Vector3 moveDirection;
void Start()
{
controller = GetComponent<CharacterController>();
}
void Update()
{
float yStore = moveDirection.y;
moveDirection = (transform.forward * Input.GetAxis("Vertical")) + (transform.right * Input.GetAxis("Horizontal"));
moveDirection = moveDirection.normalized * moveSpeed;
moveDirection.y = yStore;
if (controller.isGrounded)
{
moveDirection.y = 0f;
if (Input.GetButtonDown("Jump"))
{
moveDirection.y = jumpForce;
}
}
//Applying gravity
moveDirection.y = moveDirection.y + (Physics.gravity.y * gravityScale * Time.deltaTime);
controller.Move(moveDirection * Time.deltaTime);
if (Input.GetAxisRaw("Horizontal") != 0 || Input.GetAxisRaw("Vertical") != 0)
{
transform.rotation = Quaternion.Euler(0f, pivot.rotation.eulerAngles.y, 0);
Quaternion newRotation = Quaternion.LookRotation(new Vector3(moveDirection.x, 0f, moveDirection.z));
playerModel.transform.rotation = Quaternion.Slerp(playerModel.transform.rotation, newRotation, rotateSpeed * Time.deltaTime);
}
anim.SetBool("isGrounded", controller.isGrounded);
anim.SetFloat("Speed", (Mathf.Abs(Input.GetAxis("Vertical")) + Mathf.Abs(Input.GetAxis("Horizontal"))));
}
}