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.
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;
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;
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;
// 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;
}
}

View file

@ -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}