Commit 2e0ba501 authored by Tommé Nöll's avatar Tommé Nöll
Browse files

camera zones, and some refactoring

parent b8f35ac4
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraZone : MonoBehaviour
{
public enum FitMode {
grow,
shrink,
average
}
[Tooltip("How to scale the zone if the cameras aspect ration doesn't match")]
public FitMode fitMode;
private Bounds bounds;
private static CameraController camera;
[SerializeField] [Range(0, 1f)]
private float triggerArea = 0.9f;
private bool playerWasInZone = false;
// Start is called before the first frame update
void Start()
{
CalculateBounds();
if (!camera)
{
FindCamera();
}
}
public void FindCamera()
{
camera = GameObject.FindWithTag("MainCamera").GetComponent<CameraController>();
}
public void CalculateBounds()
{
bounds = new Bounds(transform.position, transform.localScale);
}
// Update is called once per frame
void Update()
{
Vector3 scaledPos = camera.player.transform.position;
scaledPos = bounds.center + (scaledPos - bounds.center) / triggerArea;
ApplyTarget(bounds.Contains(scaledPos));
}
public void ApplyTarget(bool apply)
{
if (apply)
{
if (!playerWasInZone)
{
camera.targetPos = transform.position;
camera.targetSize = CalculateCameraSize();
}
}
else
{
if (playerWasInZone)
{
camera.ResetTarget();
}
}
playerWasInZone = apply;
}
private float CalculateCameraSize()
{
float aspect = camera.GetAspectRatio(); // width / height
//float cameraWidthScaled = bounds.size.y * aspect;
float cameraHeightScaled = bounds.size.x / aspect;
switch (fitMode)
{
case FitMode.grow :
return Mathf.Max(bounds.size.y, cameraHeightScaled);
case FitMode.shrink :
return Mathf.Min(bounds.size.y, cameraHeightScaled);
case FitMode.average :
default :
return (bounds.size.y + cameraHeightScaled) / 2;
}
}
void OnDrawGizmosSelected()
{
CalculateBounds();
Vector3 corner1 = new Vector3(bounds.min.x, bounds.max.y, 0);
Vector3 corner2 = new Vector3(bounds.max.x, bounds.min.y, 0);
Gizmos.color = Color.blue;
Gizmos.DrawLine(bounds.max, corner1);
Gizmos.DrawLine(bounds.max, corner2);
Gizmos.DrawLine(bounds.min, corner1);
Gizmos.DrawLine(bounds.min, corner2);
}
}
fileFormatVersion: 2
guid: 0578a5b86b44fa893bad3ac6139ba5f8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &4291257553622459129
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 4291257553622459127}
- component: {fileID: 4291257553622459126}
m_Layer: 0
m_Name: CameraZone
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &4291257553622459127
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4291257553622459129}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 15, y: 9, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &4291257553622459126
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4291257553622459129}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 0578a5b86b44fa893bad3ac6139ba5f8, type: 3}
m_Name:
m_EditorClassIdentifier:
fitMode: 0
triggerArea: 0.9
fileFormatVersion: 2
guid: 991b856519736f02eb11713b4d01251d
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
......@@ -44,6 +44,10 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 4ee99ab4da7aa543c8897a952d463767, type: 3}
m_Name:
m_EditorClassIdentifier:
globalCorruption: 0
mapTexture: {fileID: 0}
plantSurfaceMaterial: {fileID: 2100000, guid: ac1fcd6dcb153b8b3bb2c6ca837296ff,
type: 2}
--- !u!1001 &5692394672654869799
PrefabInstance:
m_ObjectHideFlags: 0
......
......@@ -234,9 +234,10 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
player: {fileID: 0}
corruptionCamera: {fileID: 8811539148413200178}
offset: {x: 0, y: 0, z: -10}
panSpeed: 4
panSpeed: 2
lookaheadDistance: 4
lookaheadDelay: 1
lookAheadSpeedMultiplier: 0.5
lookAheadSpeedMultiplier: 1
minSpeed: 0.05
......@@ -2310,40 +2310,6 @@ Transform:
type: 3}
m_PrefabInstance: {fileID: 890068451}
m_PrefabAsset: {fileID: 0}
--- !u!84 &891996992
RenderTexture:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name:
m_ImageContentsHash:
serializedVersion: 2
Hash: 00000000000000000000000000000000
m_ForcedFallbackFormat: 4
m_DownscaleFallback: 0
serializedVersion: 3
m_Width: 1181
m_Height: 559
m_AntiAliasing: 1
m_DepthFormat: 0
m_ColorFormat: 21
m_MipMap: 0
m_GenerateMips: 1
m_SRGB: 0
m_UseDynamicScale: 0
m_BindMS: 0
m_EnableCompatibleFormat: 1
m_TextureSettings:
serializedVersion: 2
m_FilterMode: 1
m_Aniso: 1
m_MipBias: 0
m_WrapU: 1
m_WrapV: 1
m_WrapW: 1
m_Dimension: 2
m_VolumeDepth: 1
--- !u!1001 &911953543
PrefabInstance:
m_ObjectHideFlags: 0
......@@ -2970,12 +2936,12 @@ PrefabInstance:
- target: {fileID: 3724751736021467387, guid: a1e0107a2375e1140a669c1da0647f9a,
type: 3}
propertyPath: m_LocalPosition.y
value: -0.5
value: 0.21
objectReference: {fileID: 0}
- target: {fileID: 3724751736021467387, guid: a1e0107a2375e1140a669c1da0647f9a,
type: 3}
propertyPath: m_LocalPosition.x
value: -2.56
value: -18.92
objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: a1e0107a2375e1140a669c1da0647f9a, type: 3}
......@@ -4499,6 +4465,40 @@ Transform:
type: 3}
m_PrefabInstance: {fileID: 1788602749}
m_PrefabAsset: {fileID: 0}
--- !u!84 &1809172834
RenderTexture:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name:
m_ImageContentsHash:
serializedVersion: 2
Hash: 00000000000000000000000000000000
m_ForcedFallbackFormat: 4
m_DownscaleFallback: 0
serializedVersion: 3
m_Width: 1209
m_Height: 545
m_AntiAliasing: 1
m_DepthFormat: 0
m_ColorFormat: 21
m_MipMap: 0
m_GenerateMips: 1
m_SRGB: 0
m_UseDynamicScale: 0
m_BindMS: 0
m_EnableCompatibleFormat: 1
m_TextureSettings:
serializedVersion: 2
m_FilterMode: 1
m_Aniso: 1
m_MipBias: 0
m_WrapU: 1
m_WrapV: 1
m_WrapW: 1
m_Dimension: 2
m_VolumeDepth: 1
--- !u!1001 &1818914861
PrefabInstance:
m_ObjectHideFlags: 0
......@@ -5264,12 +5264,12 @@ PrefabInstance:
- target: {fileID: 1611726988538008503, guid: 5916f00562e9cb5eb97b95a471c1ef03,
type: 3}
propertyPath: m_LocalPosition.x
value: -15.03
value: -39.84
objectReference: {fileID: 0}
- target: {fileID: 1611726988538008503, guid: 5916f00562e9cb5eb97b95a471c1ef03,
type: 3}
propertyPath: m_LocalPosition.y
value: 2.34
value: 3.73
objectReference: {fileID: 0}
- target: {fileID: 1611726988538008503, guid: 5916f00562e9cb5eb97b95a471c1ef03,
type: 3}
......@@ -5387,6 +5387,75 @@ PrefabInstance:
objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 7ef06ffc179d97098a6c30d5f8570a4e, type: 3}
--- !u!1001 &4291257553639303086
PrefabInstance:
m_ObjectHideFlags: 0
serializedVersion: 2
m_Modification:
m_TransformParent: {fileID: 0}
m_Modifications:
- target: {fileID: 4291257553622459129, guid: 991b856519736f02eb11713b4d01251d,
type: 3}
propertyPath: m_Name
value: CameraZone
objectReference: {fileID: 0}
- target: {fileID: 4291257553622459127, guid: 991b856519736f02eb11713b4d01251d,
type: 3}
propertyPath: m_LocalPosition.x
value: 0.19
objectReference: {fileID: 0}
- target: {fileID: 4291257553622459127, guid: 991b856519736f02eb11713b4d01251d,
type: 3}
propertyPath: m_LocalPosition.y
value: 2.62
objectReference: {fileID: 0}
- target: {fileID: 4291257553622459127, guid: 991b856519736f02eb11713b4d01251d,
type: 3}
propertyPath: m_LocalPosition.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4291257553622459127, guid: 991b856519736f02eb11713b4d01251d,
type: 3}
propertyPath: m_LocalRotation.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4291257553622459127, guid: 991b856519736f02eb11713b4d01251d,
type: 3}
propertyPath: m_LocalRotation.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4291257553622459127, guid: 991b856519736f02eb11713b4d01251d,
type: 3}
propertyPath: m_LocalRotation.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4291257553622459127, guid: 991b856519736f02eb11713b4d01251d,
type: 3}
propertyPath: m_LocalRotation.w
value: 1
objectReference: {fileID: 0}
- target: {fileID: 4291257553622459127, guid: 991b856519736f02eb11713b4d01251d,
type: 3}
propertyPath: m_RootOrder
value: 8
objectReference: {fileID: 0}
- target: {fileID: 4291257553622459127, guid: 991b856519736f02eb11713b4d01251d,
type: 3}
propertyPath: m_LocalEulerAnglesHint.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4291257553622459127, guid: 991b856519736f02eb11713b4d01251d,
type: 3}
propertyPath: m_LocalEulerAnglesHint.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4291257553622459127, guid: 991b856519736f02eb11713b4d01251d,
type: 3}
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 991b856519736f02eb11713b4d01251d, type: 3}
--- !u!1001 &4809852221727672545
PrefabInstance:
m_ObjectHideFlags: 0
......@@ -5409,11 +5478,6 @@ PrefabInstance:
propertyPath: zones.Array.size
value: 2
objectReference: {fileID: 0}
- target: {fileID: 8565205532968054277, guid: 5f19e99694f67a245b4244a6b9f38023,
type: 3}
propertyPath: globalCorruption
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8565205532968054277, guid: 5f19e99694f67a245b4244a6b9f38023,
type: 3}
propertyPath: stampMaterial
......@@ -5429,11 +5493,6 @@ PrefabInstance:
propertyPath: zones.Array.data[0]
value:
objectReference: {fileID: 1782372816}
- target: {fileID: 8565205532968054277, guid: 5f19e99694f67a245b4244a6b9f38023,
type: 3}
propertyPath: plantSurfaceMaterial
value:
objectReference: {fileID: 2100000, guid: ac1fcd6dcb153b8b3bb2c6ca837296ff, type: 2}
- target: {fileID: 8565205532968054277, guid: 5f19e99694f67a245b4244a6b9f38023,
type: 3}
propertyPath: zones.Array.data[1]
......@@ -5533,6 +5592,11 @@ PrefabInstance:
propertyPath: m_Name
value: Main Camera
objectReference: {fileID: 0}
- target: {fileID: 8811539148413200178, guid: 388a7af523fdebaafad4a5d1ae1a896b,
type: 3}
propertyPath: m_TargetTexture
value:
objectReference: {fileID: 1809172834}
- target: {fileID: 8811539149977030266, guid: 388a7af523fdebaafad4a5d1ae1a896b,
type: 3}
propertyPath: m_LocalPosition.x
......@@ -5593,25 +5657,10 @@ PrefabInstance:
propertyPath: player
value:
objectReference: {fileID: 0}
- target: {fileID: 8811539149977030271, guid: 388a7af523fdebaafad4a5d1ae1a896b,
type: 3}
propertyPath: corruptionMap
value:
objectReference: {fileID: 120497678}
- target: {fileID: 8811539148413200178, guid: 388a7af523fdebaafad4a5d1ae1a896b,
type: 3}
propertyPath: m_TargetTexture
value:
objectReference: {fileID: 891996992}
- target: {fileID: 8811539148413200140, guid: 388a7af523fdebaafad4a5d1ae1a896b,
type: 3}
propertyPath: corruptionMap
value:
objectReference: {fileID: 120497678}
- target: {fileID: 4797791628540356424, guid: 388a7af523fdebaafad4a5d1ae1a896b,
type: 3}
propertyPath: resetLookaheadDelay
value: 2
objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 388a7af523fdebaafad4a5d1ae1a896b, type: 3}
......@@ -6,8 +6,11 @@ using UnityEngine;
public class CameraController : MonoBehaviour
{
[HideInInspector] public Player player;
private Camera camera;
[SerializeField] private Camera corruptionCamera;
[SerializeField] private Vector3 offset;
[SerializeField] private float panSpeed = 4f;
[SerializeField] private float panSpeed = 4f;
[Header("Lookahead")]
[SerializeField] private float lookaheadDistance = 10.0f;
......@@ -16,12 +19,28 @@ public class CameraController : MonoBehaviour
[SerializeField] private float lookAheadSpeedMultiplier = 0.25f;
[SerializeField] private float minSpeed = 0.01f;
private float playerMovingTime;
private Vector3 lastPlayerPosition;
public Vector3? targetPos;
private float defaultSize;
private float startSize;
private float _targetSize;
public float targetSize
{
get { return _targetSize; }
set
{
startSize = camera.orthographicSize;
_targetSize = value / 2;
}
}
// Start is called before the first frame update
void Start()
{
camera = GetComponent<Camera>();
defaultSize = camera.orthographicSize * 2;
ResetTarget();
try
{
FindPlayer();
......@@ -45,10 +64,15 @@ public class CameraController : MonoBehaviour
lastPlayerPosition = player.transform.position;
}
private void ApplyPlayerMovement()
public void ResetTarget()
{
transform.position += player.transform.position - lastPlayerPosition;
lastPlayerPosition = player.transform.position;
targetPos = null;
targetSize = defaultSize;
}
public float GetAspectRatio()
{
return camera.aspect;
}
void LateUpdate()
......@@ -57,42 +81,93 @@ public class CameraController : MonoBehaviour
{
return;
}
bool playerMoving = (player.transform.position - lastPlayerPosition).magnitude > 0.1f;
playerMovingTime += playerMoving ? Time.deltaTime : -Time.deltaTime;
playerMovingTime = Mathf.Clamp(playerMovingTime, 0f, lookaheadDelay * 2f);
ApplyPlayerMovement();
Vector3 currentOffset = transform.position - player.transform.position;
Vector3 targetOffset = offset;
float moveSpeedMultiplier = (lookaheadDelay - playerMovingTime) / lookaheadDelay;
if (playerMovingTime > lookaheadDelay) //apply lookahead
{
Vector2 playerVelocity2 = player.rigidbody.velocity.normalized;
Vector3 playerVelocity3 = new Vector3(playerVelocity2.x, playerVelocity2.y, 0f);
targetOffset += playerVelocity3 * lookaheadDistance;
if (playerMoving)
{
moveSpeedMultiplier *= -1 * lookAheadSpeedMultiplier;
}
}
moveSpeedMultiplier = Mathf.Max(moveSpeedMultiplier, 0f);
moveSpeedMultiplier *= panSpeed;
bool playerMoving = ApplyPlayerMovement();
MoveCamera(playerMoving);
ResizeCamera();
Vector3 targetMove = targetOffset - currentOffset;
Vector3 actualMove = targetMove * moveSpeedMultiplier;
Debug.Log(targetMove + ", " + actualMove);
if (actualMove.magnitude < minSpeed)
{
actualMove = actualMove.normalized * minSpeed;
}
actualMove *= Time.deltaTime;
if (actualMove.magnitude > targetMove.magnitude) //overshooting target in this frame
{
actualMove = targetMove;
}
//ResetTarget();
}
private bool ApplyPlayerMovement()
{
bool playerMoving = (player.transform.position - lastPlayerPosition).magnitude > 0.1f;
playerMovingTime += playerMoving ? Time.deltaTime : -Time.deltaTime;
playerMovingTime = Mathf.Clamp(playerMovingTime, 0f, lookaheadDelay * 2f);
if (targetPos == null)
{
transform.position += player.transform.position - lastPlayerPosition;
}
lastPlayerPosition = player.transform.position;
return playerMoving;
}
private void MoveCamera(bool playerMoving)
{
Vector3 targetMove;
float moveSpeedMultiplier;
if (targetPos != null)
{
targetMove = ((Vector3)targetPos) + offset - transform.position;
moveSpeedMultiplier = panSpeed;
}
else
{
Vector3 currentOffset = transform.position - player.transform.position;
Vector3 targetOffset = offset;
moveSpeedMultiplier = (lookaheadDelay - playerMovingTime) / lookaheadDelay;
if (playerMovingTime > lookaheadDelay) //apply lookahead
{
Vector2 playerVelocity2 = player.rigidbody.velocity.normalized;
Vector3 playerVelocity3 = new Vector3(playerVelocity2.x, playerVelocity2.y, 0f);
targetOffset += playerVelocity3 * lookaheadDistance;
if (playerMoving)
{
moveSpeedMultiplier *= -1 * lookAheadSpeedMultiplier;
}
}
moveSpeedMultiplier = Mathf.Max(moveSpeedMultiplier, 0f);
moveSpeedMultiplier *= panSpeed;
targetMove = targetOffset - currentOffset;
}
transform.position += ScaleMove(targetMove, moveSpeedMultiplier);
}
private Vector3 ScaleMove(Vector3 targetMove, float moveSpeedMultiplier)
{
Vector3 actualMove = targetMove * moveSpeedMultiplier;
if (actualMove.magnitude < minSpeed)
{
actualMove = actualMove.normalized * minSpeed;
}
actualMove *= Time.deltaTime;
if (actualMove.magnitude > targetMove.magnitude) //overshooting target in this frame
{
actualMove = targetMove;
}
return actualMove;
}