I have implemented a main camera that follows and orbits around an object (player). Now I would like to implement a Zoom In-Out mechanic. This functionality would allows the camera to approach the object that is being followed (lookAt()). Zooming in and out would be done by using the mouse scroll wheel. This is my code.
public Transform PlayerTransform;
private UnityEngine.Vector3 _cameraOffset;
[Range(0.01f, 1.0f)]
public float smoothFactor = 0.5f;
void Start()
{
_cameraOffset = transform.position - PlayerTransform.position;
}
void Update()
{
UnityEngine.Vector3 newPos = PlayerTransform.position + _cameraOffset;
transform.position = UnityEngine.Vector3.Slerp(transform.position, newPos, smoothFactor);
if (Input.GetMouseButton(0))
{
UnityEngine.Quaternion camTurnAngle =
Quaternion.AngleAxis(Input.GetAxis("Mouse X") * rotSpeed, Vector3.up);
_cameraOffset = camTurnAngle * _cameraOffset;
transform.LookAt(PlayerTransform);
}
}
The code should also limit how close and far away from the player can the camera move. Thank you for considering my question.