Global Game Jam 2022
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
1.1 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class CameraFollower : MonoBehaviour
  5. {
  6. public GameObject Player;
  7. [SerializeField] private float m_FollowStrength = 3f;
  8. [SerializeField] private bool m_FollowX = true;
  9. [SerializeField] private bool m_FollowY = false;
  10. [SerializeField] private float m_OffsetX = 0f;
  11. [SerializeField] private float m_OffsetY = 2.0f;
  12. private Camera m_Camera;
  13. private void Start()
  14. {
  15. m_Camera = GetComponent<Camera>();
  16. }
  17. private void Update()
  18. {
  19. if(Mathf.Abs(Player.transform.position.x - m_Camera.transform.position.x) > m_FollowStrength)
  20. {
  21. m_Camera.transform.Translate(
  22. (m_FollowX ? (m_FollowStrength + Player.transform.position.x - m_Camera.transform.position.x - m_OffsetX) * Time.deltaTime : 0),
  23. (m_FollowY ? (m_FollowStrength + Player.transform.position.y - m_Camera.transform.position.y - m_OffsetY) * Time.deltaTime : 0),
  24. 0);
  25. }
  26. }
  27. }