Question by
basukumar2222 · May 24, 2020 at 12:32 PM ·
cameraunity5camera-movementplayer movementuntiy3d
Making camera follow and rotate player
I'm making my camera follow the player. However, I can't figure out the best way to rotate the player. Here's my camera controller:
using UnityEngine;
namespace UltimateCameraController.Cameras.Controllers
{
[AddComponentMenu("Ultimate Camera Controller/Camera Controller")]
public class CameraController : MonoBehaviour
{
[Header("Follow Settings")]
[Space(10)]
[Tooltip("Should the camera follow the target?")]
public bool followTargetPosition = true; //Do we want our camera to follow the target object
[Tooltip("The target object our camera should follow or orbit around")]
public Transform targetObject; //The object that our camera should follow
[Tooltip("The smooth factor when the camera follows a target object")]
[Range(0.2f, 1f)]
public float cameraFollowSmoothness; //The smooth factor when the camera follows a target object
[Header("Orbit Settings")]
[Space(10)]
[Tooltip("Should the player be able to orbit around the target object?")]
public bool orbitAroundTarget = true; //Do we want to add orbit functionality to the camera
[Tooltip("The speed by which the camera rotates when orbiting")]
[Range(2f, 15f)]
public float rotationSpeed; //The speed by which the camera rotates when orbiting
[Tooltip("The mouse button that the player must hold in order to orbit the camera")]
public MouseButtons mouseButton; //The mouse button that the player must hold in order to orbit the camera
private Vector3 _cameraOffset; //How far away is the camera from the target
private void Start()
{
//We calculate the distance between the camera and the target
_cameraOffset = transform.position - targetObject.position;
}
//We use late update so that player movement is completed before we move the camera
//This way we can avoid glitches
private void FixedUpdate()
{
//We do an error check
if (targetObject == null)
{
Debug.LogError("Target Object is not assigned. Please assign a target object in the inspector.");
return;
}
//If we want the camera to follow the target
if (followTargetPosition)
{
//We set the position the camera should move to, to the sum of the offset and the target's position
var newPosition = targetObject.position + _cameraOffset;
//We are moving slowly to the new position. The smooth factors determines how fast
//the camera will move to its new postion
transform.position = Vector3.Slerp(transform.position, newPosition, cameraFollowSmoothness);
}
//If we want to make the player able to orbit around the target
if (orbitAroundTarget)
{
//We call the function to orbit the camera
OrbitCamera();
}
}
//Method to handle Orbit of the Camera
private void OrbitCamera()
{
//If the player holds the selected mouse button
if (Input.GetMouseButton((int)mouseButton))
{
//We cache the mouse rotation values multiplied by the rotation speed
float y_rotate = Input.GetAxis("Mouse X") * rotationSpeed;
float x_rotate = Input.GetAxis("Mouse Y") * rotationSpeed;
//We calculate the rotation angles based on the cached values and a specific axes
Quaternion xAngle = Quaternion.AngleAxis(y_rotate, Vector3.up);
Quaternion yAngle = Quaternion.AngleAxis(x_rotate, Vector3.left);
//We multiply the rotation angle by the camera offset
_cameraOffset = xAngle * _cameraOffset;
_cameraOffset = yAngle * _cameraOffset;
//We make our transform to "look" at the target
transform.LookAt(targetObject);
}
}
}
//Custom enumerator that represents the mouse buttons
public enum MouseButtons
{
LeftButton = 0,
RightButton = 1,
ScrollButton = 2
};
}
This is my camera zoom:
using UnityEngine;
namespace UltimateCameraController.Cameras.Controllers
{
//We use Require Component attribute to ensure that there is always a camera attached to the game object
//so that we can effectively avoid null reference exceptions
[RequireComponent(typeof(Camera))]
[AddComponentMenu("Ultimate Camera Controller/Camera Zoom")]
public class CameraZoom : MonoBehaviour
{
//We cache the target camera so that we won't need to use
//GetComponent every frame and thus we can boost performance
private Camera targetCamera;
[Header("Zoom Configuration")]
[Space(10)]
[SerializeField]
[Tooltip("Maximum Zoom Value")]
private float zoomMax; //Maximum Zoom Value
[SerializeField]
[Tooltip("Minimum Zoom Value")]
private float zoomMin; //Minimum Zoom Value
[Space(10)]
[SerializeField] [Tooltip("Scroll Bar Sensitivity")] [Range(1, 100)]
private float sensitivity; // Sensitivity Variable
private void Start()
{
//Here we cache the target Camera
targetCamera = GetComponent<Camera>();
}
private void Update()
{
//We cache camera's field of view in a temporary variable
float fieldOfView = targetCamera.fieldOfView;
//Simple calculation to perform the zoom functionality
fieldOfView += Input.GetAxis("Mouse ScrollWheel") * sensitivity * -1;
//We ensure that the camera's field of view is always within
//the specified limits
fieldOfView = Mathf.Clamp(fieldOfView, zoomMin, zoomMax);
//We assign the temp field of view to the actual FOV in the camera
targetCamera.fieldOfView = fieldOfView;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
How to have your player controls change while your camera rotates? 0 Answers
I build a own game but i have a cam problem 0 Answers
How do you move the camera with the player. Whats wrong with my code? 0 Answers
How to make camera follow player from point to point 2 Answers
Camera flickers while chasing the player 0 Answers