Okay, I’ve spent legitimately 3 days on this and I’ve become stumped by the ‘transform.RotateAround’ instruction. I’m trying to make a third person camera and it’s all working fine except I can’t figure out how to limit the Y Axis, so that it will stop if it gets too high or low around the character. Instead, what’s happening now is if I move the camera too far it’ll bug out at the ‘poles’ of the Y Axis.
public class ThirdPersonCamera : MonoBehaviour
{
[SerializeField]
private float distanceH;
[SerializeField]
private float distanceV;
[SerializeField]
private float smooth;
[SerializeField]
private Transform follow;
[SerializeField]
private float camSmoothDampTime = 0.1f;
[Range (0f, 4f)][SerializeField]
public float mouseSensitivityX = 1f;
[Range(0f, 4f)][SerializeField]
public float mouseSensitivityY = 1f;
private Vector3 lookDir;
private Vector3 restPosition;
private Vector3 velocityCamSmooth = Vector3.zero;
void Start()
{
follow = GameObject.FindWithTag("CameraTarget").transform;
}
void Update()
{
//Control with joystick
transform.RotateAround(follow.transform.position, follow.up, mouseSensitivityX * Input.GetAxis("Joy X") * Time.deltaTime * 100);
transform.RotateAround(follow.transform.position, follow.right, mouseSensitivityY * Input.GetAxis("Joy Y") * Time.deltaTime * 100);
//Control with mouse
transform.RotateAround(follow.transform.position, follow.up, mouseSensitivityX * Input.GetAxis("Mouse X") * Time.deltaTime * 100);
transform.RotateAround(follow.transform.position, follow.right, mouseSensitivityY * Input.GetAxis("Mouse Y") * Time.deltaTime * 100);
}
void LateUpdate()
{
Vector3 characterOffset = follow.position;
lookDir = characterOffset - this.transform.position;
lookDir.Normalize();
restPosition = characterOffset * distanceV - lookDir * distanceH;
smoothPosition(this.transform.position, restPosition);
transform.LookAt(follow);
}
void smoothPosition(Vector3 fromPos, Vector3 toPos)
{
this.transform.position = Vector3.SmoothDamp(fromPos, toPos, ref velocityCamSmooth, camSmoothDampTime);
}
}
This is what i have so far. Void Update is where the user controls are. I hope the internet can help. Thank you in advance!