Hi all,
this is to help out anyone who finds this helpful and to see if anyone can improve upon my code.
During the RPG Core Combat course I was struggling with the Cinemachine follow camera and I wasn’t satisfied with not being able to rotate the player, but not like in 3rd person, still using the Framing Transposer mode.
So what I came up with (mainly thanks to this link Rotating and zooming camera using Unity's Cinemachine) is that when you start holding the right mouse button and drag left or right, the camera will rotate around the player, which on the camera object is X axis. Hopefully this will help somebody. And if anyone can help improving this or make some changes, feel free.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;
namespace RPG.Control
{
public class CameraController : MonoBehaviour
{
[SerializeField] CinemachineVirtualCamera virtualCamera;
[SerializeField] float cameraRotationSpeed = 2f;
private Vector3 oldMousePosition;
void Start()
{
virtualCamera = GameObject.FindGameObjectWithTag("FollowCamera").GetComponent<CinemachineVirtualCamera>();
}
void Update()
{
if( Input.GetMouseButtonDown(1))
{
oldMousePosition = Input.mousePosition;
return;
}
if (Input.GetMouseButton(1))
{
Vector3 currentMousePosition = Input.mousePosition;
if ( currentMousePosition.x < oldMousePosition.x)
{
float x = virtualCamera.transform.eulerAngles.x;
float y = virtualCamera.transform.eulerAngles.y;
virtualCamera.transform.eulerAngles = new Vector3(x, y + cameraRotationSpeed);
}
if (currentMousePosition.x > oldMousePosition.x)
{
float x = virtualCamera.transform.eulerAngles.x;
float y = virtualCamera.transform.eulerAngles.y;
virtualCamera.transform.eulerAngles = new Vector3(x, y - cameraRotationSpeed);
}
}
}
}
}
Need to tag the follow cam:
Start out something like this:
Rotate one way:
Rotate another way:
Having this kind of improves my anxiety of being able to rotate my player so that at any point I can see enemies or where I’m going, if a tree is an a way. Or in general I prefer it this way on RPG games.