Skip to content

Commit

Permalink
X-Axis Cam Following
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsZauberer committed Feb 10, 2021
1 parent 25357d0 commit 60bf259
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Assets/Scenes/SampleScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -1559,6 +1559,7 @@ MonoBehaviour:
m_EditorClassIdentifier:
controller: {fileID: 1551824122}
runSpeed: 80
camObj: {fileID: 519420028}
--- !u!50 &1551824124
Rigidbody2D:
serializedVersion: 4
Expand Down Expand Up @@ -2228,7 +2229,7 @@ PrefabInstance:
- target: {fileID: 7188535337102831262, guid: d4bdf011d4d149f42b5a06977a02fdab,
type: 3}
propertyPath: m_LocalPosition.x
value: 1.26
value: -4.2
objectReference: {fileID: 0}
- target: {fileID: 7188535337102831262, guid: d4bdf011d4d149f42b5a06977a02fdab,
type: 3}
Expand Down
15 changes: 14 additions & 1 deletion Assets/Scripts/Player/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ public class Player : MonoBehaviour
{
public CharacterController2D controller;
public float runSpeed = 40f;
public GameObject camObj;
Vector2 camStartPos;
float horizontalMove = 0f;
bool jump = false;

// Update is called once per frame
void Start() {
camStartPos = camObj.transform.position;
}

void Update()
{
horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
Expand All @@ -22,5 +27,13 @@ void Update()
void FixedUpdate() {
controller.Move(horizontalMove * Time.fixedDeltaTime, false, jump);
jump = false;

camFollow();
}

void camFollow() {
if (camStartPos.x < transform.position.x) {
camObj.transform.position = new Vector3(transform.position.x, camObj.transform.position.y, -10);
}
}
}

0 comments on commit 60bf259

Please sign in to comment.