Hello everyone.
Weird issue, but this is driving me nuts. I can’t seem to find where or how this script is being called, but it is. I’ve done a million little things, and cobbled together Mirror tutorial scripts so many different times that I’ve forgotten where I even began or where to go next.
I’ve searched around - and found and tried the ‘right click on a script and click “Find References In Scene”’ on all of the scenes I have (which is only 3, one loading, one play, and one old unused one), but this cameracontroller call is not found.
The sample below was included in one of the Mirror tutorials, and is what is being used to follow the player. Yet it is not assigned to any of the objects, in any scene. But it is still working. How?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
namespace UltimateCameraController.Cameras.Controllers
{
public class CameraController : MonoBehaviour
{
private Camera targetCamera;
[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
public GameObject targetObject;
[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()
{
// if (isLocalPlayer)
//{
targetCamera = GetComponent<Camera>();
_cameraOffset = targetObject.transform.position + new Vector3(-10.0f, 20.0f, -24.0f);
//}
}
//We use late update so that player movement is completed before we move the camera/This way we can avoid glitches
private void LateUpdate()
{
//if (isLocalPlayer)
//{
//We do an error check
if (targetObject == null)
{
Debug.LogError("Target Object is not assigned. Please assign a target object in the inspector.");
return;
}
var players = GameObject.FindGameObjectsWithTag("Player");
if (targetObject != null)
{
foreach (var player in players)
{ //Debug.Log("IsNotNull5555");//works
targetObject = player;
transform.LookAt(targetObject.transform.position);
}
}
//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.transform.position + _cameraOffset;
//var newPosition = targetObject.position + _cameraOffset;
transform.position = Vector3.Slerp(transform.position, newPosition, cameraFollowSmoothness);
}
//If we want to make the player able to orbit around the target
if (orbitAroundTarget)
{
//if (isLocalPlayer)
//{
//// 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;
// targetCamera.LookAt(targetObject.transform.position);
transform.LookAt(targetObject.transform.position);
//We make our transform to "look" at the target
//////////transform.LookAt(targetObject.transform.position);
// transform.LookAt(targetObject);
}
}
}
//Custom enumerator that represents the mouse buttons
public enum MouseButtons
{
LeftButton = 0,
RightButton = 1,
ScrollButton = 2
};
}
It is following the player, and rotating around the player - but the “drag to pan” feature isn’t working, which is what started me down this rabbit hole.
I’d like to know when/where/how this is triggered so I can work to resolve the mouse drag pan scenario.
(or if anyone knows how to do that, that would cut out some steps)
Any thoughts?
Thanks all.