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.
HighGroundRoyaleNetcode/Assets/Scripts/Gameplay/GameplayObjects/Character/EdgePanningController.cs

124 lines
3.7 KiB
C#

using UnityEngine;
using Cinemachine;
using System.Collections;
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 Coroutine shakeCoroutine;
private void Start()
{
cameraOffset = virtualCamera.GetComponent<CinemachineCameraOffset>();
if (cameraOffset == null)
{
Debug.LogError("[EdgePanningController] No CinemachineCameraOffset component found! Add it to the FreeLook Camera.");
return;
}
defaultOffset = cameraOffset.m_Offset; // Store default offset
}
private void Update()
{
HandleEdgePanning();
}
private void HandleEdgePanning()
{
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 * 3f);
}
public void ShakeCamera(float duration = 0.3f, float magnitude = 0.2f)
{
if (shakeCoroutine != null)
{
StopCoroutine(shakeCoroutine);
}
shakeCoroutine = StartCoroutine(ShakeRoutine(duration, magnitude));
}
private IEnumerator ShakeRoutine(float duration, float magnitude)
{
float elapsed = 0f;
Vector3 originalOffset = defaultOffset;
while (elapsed < duration)
{
float x = Random.Range(-1f, 1f) * magnitude;
float y = Random.Range(-1f, 1f) * magnitude;
float z = Random.Range(-1f, 1f) * magnitude;
cameraOffset.m_Offset = originalOffset + new Vector3(x, y, z);
elapsed += Time.deltaTime;
yield return null;
}
cameraOffset.m_Offset = originalOffset; // Reset to default offset
}
}
//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 * 3f);
// }
//}