Attempt to set camera after moving for a while

This commit is contained in:
Norbi Peti 2020-11-06 02:25:10 +01:00
parent b7a5715e34
commit e571047781
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
2 changed files with 31 additions and 24 deletions

View file

@ -14,8 +14,9 @@ public class CameraController : MonoBehaviour {
// Center position of the target relative to the camera. // Center position of the target relative to the camera.
public Vector3 offset; public Vector3 offset;
private float _lastSwitch = -1;
private bool _facingRight; private float _goingSince;
private bool _goingRight;
// Store initial values // Store initial values
void Start() void Start()
@ -28,27 +29,33 @@ public class CameraController : MonoBehaviour {
void FixedUpdate() void FixedUpdate()
{ {
if (!target) return; 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. // Get where the camera should be, and what movement is required.
var position = _tr.position; var ownPos = _tr.position;
bool facingLeft = target.localScale.x < 0; Vector3 anchorPosL = ownPos + offset;
Vector3 anchorPos = position + (facingLeft ? new Vector3(-offset.x, offset.y, offset.z) : offset); Vector3 anchorPosR = ownPos + new Vector3(-offset.x, offset.y, offset.z);
Vector3 movement = target.position - anchorPos; var targetPos = target.position;
Vector3 movementL = targetPos - anchorPosL;
float sp = speed; Vector3 movementR = targetPos - anchorPosR;
/*if (_lastSwitch >= 0 && Time.time - _lastSwitch < 0.5f) if (movementL.x > 0 && movementR.x < 0
sp /= 4;*/ && _goingSince == 0)
float diff = Time.time - _lastSwitch; return;
if (_lastSwitch >= 0 && diff < 2f) var movement = (_goingSince != 0 ? _goingRight : movementL.x < movementR.x)
sp *= 0.5f + diff / 4; ? movementL //Ha már egy ideje megyünk egy irányba vagy ha arra közelebb van a kamerának, akkor arra megy
: movementR;
if (facingLeft == _facingRight) //Megváltozott az irány
_lastSwitch = Time.time;
_facingRight = !facingLeft;
// Update position based on movement and speed. // Update position based on movement and speed.
Vector3 newCamPos = position + movement * sp; Vector3 newCamPos = ownPos + movement * speed;
position = newCamPos; ownPos = newCamPos;
_tr.position = position; _tr.position = ownPos;
} }
} }

View file

@ -273,7 +273,7 @@ Camera:
far clip plane: 1000 far clip plane: 1000
field of view: 60 field of view: 60
orthographic: 1 orthographic: 1
orthographic size: 4 orthographic size: 3
m_Depth: 0 m_Depth: 0
m_CullingMask: m_CullingMask:
serializedVersion: 2 serializedVersion: 2
@ -297,7 +297,7 @@ Transform:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1351818047} m_GameObject: {fileID: 1351818047}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} 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_LocalScale: {x: 1, y: 1, z: 1}
m_Children: [] m_Children: []
m_Father: {fileID: 0} m_Father: {fileID: 0}
@ -622,7 +622,7 @@ Transform:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2053847420} m_GameObject: {fileID: 2053847420}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} 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_LocalScale: {x: 1, y: 1, z: 1}
m_Children: [] m_Children: []
m_Father: {fileID: 0} m_Father: {fileID: 0}