- Home /
How do I rotate the camera upwards when zoom is close?
Hello. I'm trying to work on my CameraController script. Currently I'm attempting to rotate the camera upwards towards the "center" of my player object when it's close.
I have my rotation and zoom controlled by sliders at the moment, and may change that in time (gearing towards mobile controls).
Reason why I want this is because when the zoom is all the way down the camera is looking at the feet of my player. Later on in development I'd like this close zoom to allow the player to look at their character and how they look from head to toe. I'd also like this to smoothly hit that spot as the zoom value drops.
Here is my code:
using UnityEngine;
using UnityEngine.UI;
public class CameraController : MonoBehaviour {
public Transform player; // Access to the player object's Transform component
public float camHeight;
public float camDistance;
public Slider zoomSlider; // Access to UI Zoom Slider
public Slider rotateSlider; // Access to the UI Rotate Slider
private float fov; // Set variable for field of view
private Vector3 offset; // Set variable for camera offset/ position
private Vector3 lookAngle;
private GameObject[] roofs;
void Awake ()
{
offset = new Vector3(player.position.x, player.position.y + camHeight, player.position.z - camDistance);
roofs = GameObject.FindGameObjectsWithTag("Roof");
}
private void Update()
{
Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.45f));
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100))
{
// Checks for seeing if player is inside
if (hit.collider.CompareTag("Floor"))
{
for (int i = 0; i < roofs.Length; i++)
{
roofs[i].SetActive(false);
}
}
else
{
for (int i = 0; i < roofs.Length; i++)
{
roofs[i].SetActive(true);
}
}
}
}
void LateUpdate ()
{
if (Input.GetMouseButtonDown(0))
{
rotateSlider.minValue = rotateSlider.value - 180;
rotateSlider.maxValue = rotateSlider.value + 180;
}
if (zoomSlider.value <= 10)
{
lookAngle = new Vector3(player.position.x, player.position.y + ANGLE TO LOOK UP, player.position.z);
}
if (zoomSlider.value > 10)
{
lookAngle = player.position;
}
transform.position = player.position + offset; // Sets camera offset
transform.LookAt(lookAngle); // Look at player
transform.RotateAround(player.position, Vector3.up, rotateSlider.value); // Rotate around player based on slider value
fov = zoomSlider.value;
Camera.main.fieldOfView = fov;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612173754im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Camera Zoom from rotation. 0 Answers
Basic Camera rotation and Zoom 0 Answers
Need help implementing a zoom function 0 Answers
Local Third Person Movement based off Camera Rotation 0 Answers