From e57104778118ebed9a79b5be55b39d790bec031a Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Fri, 6 Nov 2020 02:25:10 +0100 Subject: [PATCH] Attempt to set camera after moving for a while --- Assets/CameraController.cs | 49 +++++++++++++++++++-------------- Assets/Scenes/SampleScene.unity | 6 ++-- 2 files changed, 31 insertions(+), 24 deletions(-) diff --git a/Assets/CameraController.cs b/Assets/CameraController.cs index a336118..234fbab 100644 --- a/Assets/CameraController.cs +++ b/Assets/CameraController.cs @@ -14,8 +14,9 @@ public class CameraController : MonoBehaviour { // Center position of the target relative to the camera. public Vector3 offset; - private float _lastSwitch = -1; - private bool _facingRight; + + private float _goingSince; + private bool _goingRight; // Store initial values void Start() @@ -28,27 +29,33 @@ public class CameraController : MonoBehaviour { void FixedUpdate() { if (!target) return; + float horiz = Input.GetAxis("Horizontal"); + if (_goingSince == 0 && Mathf.Abs(horiz) > 0.1f) + { + _goingRight = horiz > 0; + _goingSince = Time.time; + } + + if (Mathf.Abs(horiz) < 0.01f) + _goingSince = 0; + // Get where the camera should be, and what movement is required. - var position = _tr.position; - bool facingLeft = target.localScale.x < 0; - Vector3 anchorPos = position + (facingLeft ? new Vector3(-offset.x, offset.y, offset.z) : offset); - Vector3 movement = target.position - anchorPos; + var ownPos = _tr.position; + Vector3 anchorPosL = ownPos + offset; + Vector3 anchorPosR = ownPos + new Vector3(-offset.x, offset.y, offset.z); + var targetPos = target.position; + Vector3 movementL = targetPos - anchorPosL; + Vector3 movementR = targetPos - anchorPosR; + if (movementL.x > 0 && movementR.x < 0 + && _goingSince == 0) + return; + var movement = (_goingSince != 0 ? _goingRight : movementL.x < movementR.x) + ? movementL //Ha már egy ideje megyünk egy irányba vagy ha arra közelebb van a kamerának, akkor arra megy + : movementR; - float sp = speed; - /*if (_lastSwitch >= 0 && Time.time - _lastSwitch < 0.5f) - sp /= 4;*/ - float diff = Time.time - _lastSwitch; - if (_lastSwitch >= 0 && diff < 2f) - sp *= 0.5f + diff / 4; - - if (facingLeft == _facingRight) //Megváltozott az irány - _lastSwitch = Time.time; - - _facingRight = !facingLeft; - // Update position based on movement and speed. - Vector3 newCamPos = position + movement * sp; - position = newCamPos; - _tr.position = position; + Vector3 newCamPos = ownPos + movement * speed; + ownPos = newCamPos; + _tr.position = ownPos; } } \ No newline at end of file diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 9978294..9fe3908 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -273,7 +273,7 @@ Camera: far clip plane: 1000 field of view: 60 orthographic: 1 - orthographic size: 4 + orthographic size: 3 m_Depth: 0 m_CullingMask: serializedVersion: 2 @@ -297,7 +297,7 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1351818047} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: -4.86, y: 2.58, z: -5.0090327} + m_LocalPosition: {x: -5.59, y: 2.59, z: -5.0090327} m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 0} @@ -622,7 +622,7 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 2053847420} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: -8.08, y: 2.49, z: 0} + m_LocalPosition: {x: -8.28, y: 2.75, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 0}