add dithering back, only zoom when right clicking
This commit is contained in:
@@ -34,6 +34,17 @@ public class CameraController : MonoBehaviour
|
||||
[SerializeField] private float collisionOffset = 0.1f;
|
||||
[SerializeField] private float positionSmoothness = 15f;
|
||||
|
||||
[Header("Occlusion Settings")]
|
||||
[SerializeField] private LayerMask obstructionMask;
|
||||
[SerializeField] private float fadedAlpha = 0.25f;
|
||||
[SerializeField] private float fadeSpeed = 5f;
|
||||
|
||||
private Dictionary<Renderer, float> currentAlpha =
|
||||
new Dictionary<Renderer, float>();
|
||||
|
||||
private Dictionary<Renderer, float> targetAlpha =
|
||||
new Dictionary<Renderer, float>();
|
||||
|
||||
private float yaw;
|
||||
private float pitch = 20f;
|
||||
private float targetDistance;
|
||||
@@ -82,6 +93,8 @@ private void LateUpdate()
|
||||
return;
|
||||
}
|
||||
|
||||
UpdateOcclusion();
|
||||
UpdateFade();
|
||||
HandleRotation();
|
||||
HandleZoom();
|
||||
UpdateCameraPosition();
|
||||
@@ -95,11 +108,9 @@ private void HandleRotation()
|
||||
UnityEngine.Cursor.lockState = CursorLockMode.None;
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
UnityEngine.Cursor.visible = false;
|
||||
UnityEngine.Cursor.lockState = CursorLockMode.Locked;
|
||||
}
|
||||
|
||||
UnityEngine.Cursor.visible = false;
|
||||
UnityEngine.Cursor.lockState = CursorLockMode.Locked;
|
||||
|
||||
yaw += Input.GetAxis("Mouse X") * rotationSpeed * Time.deltaTime;
|
||||
pitch -= Input.GetAxis("Mouse Y") * rotationSpeed * Time.deltaTime;
|
||||
@@ -109,6 +120,11 @@ private void HandleRotation()
|
||||
|
||||
private void HandleZoom()
|
||||
{
|
||||
if (!Input.GetMouseButton(1))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
float scrollInput = Input.GetAxis("Mouse ScrollWheel");
|
||||
|
||||
targetDistance -= scrollInput * zoomSpeed;
|
||||
@@ -137,269 +153,11 @@ private void UpdateCameraPosition()
|
||||
|
||||
MainCamera.transform.position = Vector3.SmoothDamp(MainCamera.transform.position, finalPosition, ref currentVelocity, 1f / positionSmoothness);
|
||||
|
||||
MainCamera.transform.rotation = Quaternion.LookRotation(targetPosition - MainCamera.transform.position, Vector3.up); }
|
||||
|
||||
/*public float camTargetRotation { get; private set; } = 0;
|
||||
public CameraControlMode CamControlMode { get; private set; } = CameraControlMode.SnapRotate;
|
||||
public Camera MainCamera;
|
||||
|
||||
private bool isCamRotating = false;
|
||||
|
||||
[Header("Rotation")]
|
||||
public float mouseSensitivity = 200f;
|
||||
public float minPitch = -30f;
|
||||
public float maxPitch = 60f;
|
||||
public float distance = 5f;
|
||||
private float yaw;
|
||||
private float pitch;
|
||||
|
||||
[Header("Zoom")]
|
||||
public float followSpeed = 10f;
|
||||
public float maxDistance = 6f;
|
||||
public float minDistance = 1f;
|
||||
public float zoomSpeed = 5f;
|
||||
public float heightOffset = -0.5f;
|
||||
private float currentDistance;
|
||||
private float distanceVelocity;
|
||||
|
||||
[Header("Collision")]
|
||||
public float collisionRadius = 0.3f;
|
||||
public float collisionOffset = 0.2f;
|
||||
public LayerMask collisionMask;
|
||||
|
||||
[Header("Occlusion Settings")]
|
||||
public LayerMask obstructionMask;
|
||||
public float fadedAlpha = 0.25f;
|
||||
public float fadeSpeed = 5f;
|
||||
|
||||
private Dictionary<Renderer, float> currentAlpha =
|
||||
new Dictionary<Renderer, float>();
|
||||
|
||||
private Dictionary<Renderer, float> targetAlpha =
|
||||
new Dictionary<Renderer, float>();
|
||||
|
||||
public Transform playerCamHome;
|
||||
Transform dougBody;
|
||||
|
||||
private void Update()
|
||||
{
|
||||
UpdateOcclusion();
|
||||
UpdateFade();
|
||||
|
||||
if (Input.GetMouseButton(1))
|
||||
{
|
||||
HandleFreeCamRotation();
|
||||
}
|
||||
else
|
||||
{
|
||||
CamAvoidCollisions();
|
||||
}
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
Vector3 angles = playerCamHome.transform.eulerAngles;
|
||||
|
||||
yaw = angles.y;
|
||||
pitch = angles.x;
|
||||
currentDistance = distance;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Trigger cam rotation
|
||||
/// </summary>
|
||||
public void RotateCam()
|
||||
{
|
||||
if (!isCamRotating)
|
||||
{
|
||||
float currentY = playerCamHome.transform.rotation.eulerAngles.y;
|
||||
camTargetRotation = currentY + 90f;
|
||||
isCamRotating = true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set cam rotation immediately
|
||||
/// </summary>
|
||||
void SnapToRotation(float rotation)
|
||||
{
|
||||
if (CamControlMode == CameraControlMode.SnapRotate)
|
||||
{
|
||||
playerCamHome.RotateAround(
|
||||
transform.position,
|
||||
Vector3.up,
|
||||
rotation
|
||||
);
|
||||
|
||||
camTargetRotation = rotation;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Move cam to desired rotation (every frame)
|
||||
/// </summary>
|
||||
void HandleSnapCamRotation()
|
||||
{
|
||||
float currentY = playerCamHome.transform.rotation.eulerAngles.y;
|
||||
float step = 200 * Time.deltaTime;
|
||||
float newY = Mathf.MoveTowardsAngle(currentY, camTargetRotation, step);
|
||||
|
||||
playerCamHome.RotateAround(
|
||||
transform.position,
|
||||
Vector3.up,
|
||||
newY - currentY
|
||||
);
|
||||
|
||||
if (Mathf.Abs(Mathf.DeltaAngle(newY, camTargetRotation)) < 0.01f)
|
||||
{
|
||||
isCamRotating = false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Move cam to desired free rotation (every frame)
|
||||
/// </summary>
|
||||
void HandleFreeCamRotation()
|
||||
{
|
||||
if (dougBody == null)
|
||||
{
|
||||
if (GameManager.Instance.PlayerController.DougBody != null)
|
||||
dougBody = GameManager.Instance.PlayerController.DougBody.transform;
|
||||
else
|
||||
return;
|
||||
}
|
||||
|
||||
Vector3 target = new Vector3(dougBody.position.x, dougBody.position.y + heightOffset, dougBody.position.z);
|
||||
|
||||
// Mouse Input
|
||||
float mouseX = Input.GetAxis("Mouse X");
|
||||
float mouseY = Input.GetAxis("Mouse Y");
|
||||
|
||||
yaw += mouseX * mouseSensitivity * Time.deltaTime;
|
||||
pitch -= mouseY * mouseSensitivity * Time.deltaTime;
|
||||
|
||||
pitch = Mathf.Clamp(pitch, minPitch, maxPitch);
|
||||
|
||||
// Rotation
|
||||
Quaternion rotation = Quaternion.Euler(pitch, yaw, 0);
|
||||
|
||||
// Zoom
|
||||
float scroll = Input.GetAxis("Mouse ScrollWheel");
|
||||
|
||||
if (Mathf.Abs(scroll) > 0.0001f)
|
||||
{
|
||||
distance -= scroll * zoomSpeed;
|
||||
distance = Mathf.Clamp(distance, minDistance, maxDistance);
|
||||
}
|
||||
|
||||
currentDistance = Mathf.SmoothDamp(
|
||||
currentDistance,
|
||||
distance,
|
||||
ref distanceVelocity,
|
||||
0.05f);
|
||||
|
||||
float adjustedDistance = currentDistance;
|
||||
|
||||
Vector3 desiredDirection =
|
||||
rotation * Vector3.back;
|
||||
|
||||
// Camera obstruction handling
|
||||
if (Physics.SphereCast(
|
||||
target,
|
||||
collisionRadius,
|
||||
desiredDirection,
|
||||
out RaycastHit hit,
|
||||
currentDistance,
|
||||
collisionMask,
|
||||
QueryTriggerInteraction.Ignore))
|
||||
{
|
||||
adjustedDistance =
|
||||
Mathf.Max(
|
||||
hit.distance - collisionOffset,
|
||||
minDistance);
|
||||
}
|
||||
|
||||
// Desired Position
|
||||
Vector3 offset = rotation * Vector3.forward * -adjustedDistance;
|
||||
Vector3 desiredPosition = target + offset;
|
||||
|
||||
if (desiredPosition.y < dougBody.position.y)
|
||||
{
|
||||
desiredPosition.y = dougBody.position.y;
|
||||
}
|
||||
|
||||
// Smooth Follow
|
||||
playerCamHome.transform.position = Vector3.Lerp(
|
||||
playerCamHome.transform.position,
|
||||
desiredPosition,
|
||||
followSpeed * Time.deltaTime
|
||||
);
|
||||
|
||||
// Look at player
|
||||
playerCamHome.transform.LookAt(target + Vector3.up);
|
||||
}
|
||||
|
||||
void CamAvoidCollisions()
|
||||
{
|
||||
if (dougBody == null)
|
||||
{
|
||||
if (GameManager.Instance.PlayerController.DougBody != null)
|
||||
dougBody = GameManager.Instance.PlayerController.DougBody.transform;
|
||||
else
|
||||
return;
|
||||
}
|
||||
|
||||
Vector3 target = new Vector3(dougBody.position.x, dougBody.position.y + heightOffset, dougBody.position.z);
|
||||
Quaternion rotation = Quaternion.Euler(pitch, yaw, 0);
|
||||
Vector3 desiredDirection = rotation * Vector3.back;
|
||||
|
||||
float adjustedDistance = currentDistance;
|
||||
|
||||
if (Physics.SphereCast(
|
||||
target,
|
||||
collisionRadius,
|
||||
desiredDirection,
|
||||
out RaycastHit hit,
|
||||
currentDistance,
|
||||
collisionMask,
|
||||
QueryTriggerInteraction.Ignore))
|
||||
{
|
||||
adjustedDistance =
|
||||
Mathf.Max(
|
||||
hit.distance - collisionOffset,
|
||||
minDistance);
|
||||
}
|
||||
|
||||
// Desired Position
|
||||
Vector3 offset = rotation * Vector3.forward * -adjustedDistance;
|
||||
Vector3 desiredPosition = target + offset;
|
||||
|
||||
if (desiredPosition.y < dougBody.position.y)
|
||||
{
|
||||
desiredPosition.y = dougBody.position.y;
|
||||
}
|
||||
|
||||
// Smooth Follow
|
||||
playerCamHome.transform.position = Vector3.Lerp(
|
||||
playerCamHome.transform.position,
|
||||
desiredPosition,
|
||||
followSpeed * Time.deltaTime
|
||||
);
|
||||
|
||||
// Look at player
|
||||
playerCamHome.transform.LookAt(target + Vector3.up);
|
||||
MainCamera.transform.rotation = Quaternion.LookRotation(targetPosition - MainCamera.transform.position, Vector3.up);
|
||||
}
|
||||
|
||||
void UpdateOcclusion()
|
||||
{
|
||||
if (dougBody == null)
|
||||
{
|
||||
if (GameManager.Instance.PlayerController.DougBody != null)
|
||||
dougBody = GameManager.Instance.PlayerController.DougBody.transform;
|
||||
else
|
||||
return;
|
||||
}
|
||||
|
||||
// Reset all renderers to visible
|
||||
foreach (Renderer r in new List<Renderer>(targetAlpha.Keys))
|
||||
{
|
||||
@@ -407,12 +165,12 @@ void UpdateOcclusion()
|
||||
}
|
||||
|
||||
Vector3 dir =
|
||||
MainCamera.transform.position - dougBody.position;
|
||||
MainCamera.transform.position - target.position;
|
||||
|
||||
float dist = dir.magnitude;
|
||||
|
||||
RaycastHit[] hits = Physics.RaycastAll(
|
||||
dougBody.position,
|
||||
target.position,
|
||||
dir.normalized,
|
||||
dist,
|
||||
obstructionMask,
|
||||
@@ -473,7 +231,7 @@ void UpdateFade()
|
||||
targetAlpha.Remove(r);
|
||||
}
|
||||
}
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
public enum CameraControlMode
|
||||
|
||||
Reference in New Issue
Block a user