i’m in the process of making a camera controller component for the player, the idea is that it stays fixed on the player and can be turned left and right with the Q and E keys, and zoomed in with the mouse scroll wheel, however, the script i have set up works in every way but one, the left and right part, see, the camera rotates perfectly fine, but for whatever reason, the camera flies out a massive distance from the ball unless the ball is at 0 on both the X and Z axis:
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour
{
public Transform target;
public float distance;
public float lift;
public float XPosition;
public bool cameraTurnIsFixed = false;
void Update ()
{
transform.position = target.position + new Vector3(XPosition, lift, distance);
transform.LookAt(target);
if (Input.GetAxis("Mouse ScrollWheel") < 0 && distance >= -5)//Zoom out
{
distance -= 0.2f;
}
if (Input.GetAxis("Mouse ScrollWheel") > 0 && distance <= -2.5)//Zoom in
{
distance += 0.2f;
}
if (cameraTurnIsFixed == false)
{
if (Input.GetKey (KeyCode.Q))
{
transform.RotateAround(target.position, Vector3.up, 40 * Time.deltaTime);
distance = transform.position.z;
XPosition = transform.position.x;
}
if (Input.GetKey (KeyCode.E))
{
transform.RotateAround(target.position, -Vector3.up, 40 * Time.deltaTime);
distance = transform.position.z;
XPosition = transform.position.x;
}
}
}
}
to fix this, i need to make the xPosition and distance variables relative to the target (the player ball), however, i don’t know how to do this to be honest, therefore i am asking here what i should do.
and please, don’t mistake this as me asking to “make my script for me”, i just need a nudge in the right direction, and in fact it would be more helpful for me because i would actually know how it works, just being handed a script with no explanation or informative comments doesn’t exactly teach me anything.
cameraTurnIsFixed is a thing i have in place for when i don’t want the player to run the camera in certain places.
the player is a rolling sphere, the camera is meant to be situated about 1.5 units above the sphere, to get a view from above to make the game feel more 3D, the distance is a short distance the camera is from the player, all the while, the player should be able to rotate the camera on the horizontal plane, almost like if it were a planet, and there was a sun around 1.5 units above the player.
monster hunter has a camera that functions like this too, though you can set the height of the camera, and the player isn’t a sphere.
so can someone assist me?, perhaps point out some documentation and where to apply it to or something.