CameraController.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class CameraController : MonoBehaviour
  5. {
  6. public float offsetX = 6, offsetY = 3, offsetZ = -16;
  7. public float lerpValue = 3.5f;
  8. [HideInInspector]
  9. public Vector3 targetPos;
  10. public Camera mainCamera;
  11. public GameObject cameras;
  12. public Camera leftCamera;
  13. public Camera rightCamera;
  14. private void FixedUpdate()
  15. {
  16. if (PlayersInput.instance[0] == null)
  17. {
  18. targetPos = Vector3.zero + new Vector3(0, offsetY, offsetZ);
  19. }
  20. else if(PlayersInput.instance[1] == null)
  21. {
  22. targetPos = CameraTargetMove(PlayersInput.instance[0]);
  23. }
  24. else
  25. {
  26. targetPos = Vector3.Lerp(PlayersInput.instance[0].transform.position,
  27. PlayersInput.instance[1].transform.position, 0.5f) + new Vector3(0,offsetY,offsetZ);
  28. }
  29. transform.position = Vector3.Lerp(transform.position, targetPos, lerpValue * Time.deltaTime);
  30. }
  31. Vector3 CameraTargetMove(PlayerController player)
  32. {
  33. if (player.bodyTrans.localScale.x > 0)
  34. {
  35. targetPos = player.transform.position + new Vector3(-offsetX, offsetY, offsetZ);
  36. }
  37. else
  38. {
  39. targetPos = player.transform.position + new Vector3(offsetX, offsetY, offsetZ);
  40. }
  41. return targetPos;
  42. }
  43. }