CompleteCameraController.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using UnityEngine;
  2. using System.Collections;
  3. public class CompleteCameraController : MonoBehaviour
  4. {
  5. public bool Loop;
  6. Vector3 playerPrevPos, playerMoveDir;
  7. public Vector3 offset;
  8. public Transform Target;
  9. public float distance = 2.8f;
  10. public float targetHeight = 2.0f;
  11. private float x = 0.0f;
  12. private float y = 0.0f;
  13. void Start()
  14. {
  15. var angles = transform.eulerAngles;
  16. x = angles.x;
  17. y = angles.y;
  18. }
  19. public void ResetCameraOnStartLeve()
  20. {
  21. distance = 8;
  22. targetHeight = 2;
  23. }
  24. void LateUpdate()
  25. {
  26. if (!Target)
  27. return;
  28. y = Target.eulerAngles.y;
  29. // ROTATE CAMERA:
  30. Quaternion rotation = Quaternion.Euler(x, y, 0);
  31. transform.rotation = rotation;
  32. // POSITION CAMERA:
  33. var position = Target.position - (rotation * Vector3.forward * distance + new Vector3(0, -targetHeight, 0));
  34. transform.position = position;
  35. }
  36. //// Use this for initialization
  37. //void Start()
  38. //{
  39. // offset = transform.position - player.transform.position;
  40. // distance = offset.magnitude;
  41. // playerPrevPos = player.transform.position;
  42. //}
  43. //void LateUpdate()
  44. //{
  45. // playerMoveDir = player.transform.position - playerPrevPos;
  46. // if (playerMoveDir != Vector3.zero)
  47. // {
  48. // playerMoveDir.Normalize();
  49. // transform.position = player.transform.position - playerMoveDir * distance;
  50. // transform.position = new Vector3(transform.position.x, transform.position.y + 5f, transform.position.z); // required height
  51. // transform.LookAt(Vector3.Lerp(transform.position,player.transform.position, Time.deltaTime * 5));
  52. // playerPrevPos = player.transform.position;
  53. // }
  54. //}
  55. }