You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
1.2 KiB
C#
41 lines
1.2 KiB
C#
3 days ago
|
using UnityEngine;
|
||
|
using Cinemachine;
|
||
|
|
||
|
public class EdgePanningController: MonoBehaviour
|
||
|
{
|
||
|
public CinemachineFreeLook virtualCamera;
|
||
|
private CinemachineCameraOffset cameraOffset;
|
||
|
|
||
|
public float edgeThreshold = 50f; // Edge detection distance
|
||
|
public float panSpeed = 0.1f; // How much to offset per frame
|
||
|
|
||
|
private Vector3 defaultOffset;
|
||
|
|
||
|
private void Start()
|
||
|
{
|
||
|
cameraOffset = virtualCamera.GetComponent<CinemachineCameraOffset>();
|
||
|
defaultOffset = cameraOffset.m_Offset; // Store default offset
|
||
|
}
|
||
|
|
||
|
private void Update()
|
||
|
{
|
||
|
Vector3 newOffset = defaultOffset;
|
||
|
Vector3 mousePos = Input.mousePosition;
|
||
|
|
||
|
float screenWidth = Screen.width;
|
||
|
float screenHeight = Screen.height;
|
||
|
|
||
|
// Left Edge
|
||
|
if (mousePos.x <= edgeThreshold) newOffset.x -= panSpeed;
|
||
|
// Right Edge
|
||
|
if (mousePos.x >= screenWidth - edgeThreshold) newOffset.x += panSpeed;
|
||
|
// Bottom Edge
|
||
|
if (mousePos.y <= edgeThreshold) newOffset.y -= panSpeed;
|
||
|
// Top Edge
|
||
|
if (mousePos.y >= screenHeight - edgeThreshold) newOffset.y += panSpeed;
|
||
|
|
||
|
// Smooth transition
|
||
|
cameraOffset.m_Offset = Vector3.Lerp(cameraOffset.m_Offset, newOffset, Time.deltaTime * 5f);
|
||
|
}
|
||
|
}
|