- Home /
Question by
Aseus48 · Mar 14 at 08:42 AM ·
camera rotatecamera follow
How to make the camera move up and down
I have a script to move the player with the camera rotates left right but can't up down does anyone know how to do this?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace SG
{
public class CameraHandler : MonoBehaviour
{
public Transform targetTransform;
public Transform cameraTransform;
public Transform cameraPivotTransform;
private Transform myTransform;
private Vector3 cameraTransformPosition;
private LayerMask ignoreLayers;
public static CameraHandler singleton;
public float lookSpeed = 0.1f;
public float followSpeed = 0.1f;
public float pivotSpeed = 0.03f;
private float defaultPosition;
private float lookAngle;
private float pivotAngle;
private float minimumPivot = -35;
private float maximumPivot = 35;
private void Awake()
{
singleton = this;
myTransform = transform;
defaultPosition = cameraTransform.localPosition.z;
ignoreLayers = ~(1 << 8 | 1 << 9 | 1 << 10);
}
public void FollowTarget(float delta)
{
Vector3 targetPosition = Vector3.Lerp(myTransform.position, targetTransform.position, delta / followSpeed);
myTransform.position = targetPosition;
}
public void HandleCameraRotation(float delta, float mouseXInput, float mouseYInput)
{
lookAngle += (mouseXInput * lookSpeed) / delta;
pivotAngle -= (mouseYInput * pivotSpeed) / delta;
pivotAngle = Mathf.Clamp(pivotAngle, minimumPivot, maximumPivot);
Vector3 rotation = Vector3.zero;
rotation.y = lookAngle;
Quaternion targetRotation = Quaternion.Euler(rotation);
myTransform.rotation = targetRotation;
rotation = Vector3.zero;
rotation.x = pivotAngle;
targetRotation = Quaternion.Euler(rotation);
cameraPivotTransform.localRotation = targetRotation;
}
}
}
Comment
Do you want it to orbit around the player top and bottom, or move up and down while looking at the player?
Have you looked into cinemachine's freelook camera to fit your need? Its got all this out of the box.
Your answer

Follow this Question
Related Questions
Rotate camera around object with camera follow 1 Answer
change objects Axis to match cameras rotation 0 Answers
How to look player and enemy at the same time using cinemachine FreeLook camera. 0 Answers
camera follow rotation issue 3rd person controller 1 Answer
How to achieve a 3rd person camera for walk on planet games? 0 Answers