CameraControllerCS.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * FUNCTION:
  3. * This script controls the camera movements based on the Player's movements.
  4. *
  5. * USED BY: This script is part of the "Main Camera" prefab.
  6. *
  7. */
  8. using UnityEngine;
  9. using System.Collections;
  10. public class CameraControllerCS : MonoBehaviour {
  11. private Transform tPlayer; //player transform
  12. private Transform tCamera; //Main Camera transform
  13. private Transform tPlayerMesh; //player's mesh
  14. //script references
  15. private InGameScriptCS hInGameScriptCS;
  16. private ControllerScriptCS hControllerScriptCS;
  17. //defines the distance between the player and the camera
  18. //set in the CameraMain() function
  19. private float fCameraLerpValue;
  20. //camera position variables
  21. //all these variables are changed continuously during runtime
  22. //to set the camera position
  23. private float fCameraDistance = 30;//distance between player and camera
  24. private Vector3 v3CamDirection; //camera direction
  25. private float fCurrentCamDir = 90.0f; //camera rotation based on player's rotation
  26. private float fCameraRotationX = 0.0f; //camera x rotation
  27. private float fCameraRotationZ = 0.0f; //camera z rotation
  28. private float fCameraPositionY = 40; //camera Y position
  29. private float fCameraPositionX = -15; //camera X position
  30. public int iCameraState = 0; //camera state
  31. private float fCamShakeImpulse = 0.0f; //Camera Shake Impulse
  32. void Start()
  33. {
  34. tCamera = this.GetComponent<Camera>().transform;
  35. tPlayerMesh = GameObject.Find("PlayerRotation/PlayerMesh").transform;
  36. tPlayer = GameObject.Find("Player").transform;
  37. hInGameScriptCS = (InGameScriptCS)GameObject.Find("Player").GetComponent(typeof(InGameScriptCS));
  38. hControllerScriptCS = (ControllerScriptCS)GameObject.Find("Player").GetComponent(typeof(ControllerScriptCS));
  39. fCameraRotationX = tCamera.localEulerAngles.x;
  40. fCameraRotationZ = tCamera.localEulerAngles.z;
  41. iCameraState = 0;
  42. fCamShakeImpulse = 0.0f;
  43. }
  44. /*
  45. * FUNCTION: Start following the player
  46. * CALLED BY: InGameScript.launchGame()
  47. */
  48. public void launchGame()
  49. {
  50. iCameraState = 1;
  51. }
  52. void Update ()
  53. {
  54. if(hInGameScriptCS.isGamePaused()==true)
  55. return;
  56. if (hInGameScriptCS.isEnergyZero()) //switch to death camera state on depletion of energy
  57. iCameraState = 2;
  58. else
  59. {
  60. iCameraState = 1;
  61. }
  62. }
  63. void FixedUpdate()
  64. {
  65. CameraMain();//camera transitions
  66. }
  67. /*
  68. * FUNCTION: Controls camera movements
  69. * CALLED BY: FixedUpdate()
  70. */
  71. private void CameraMain()
  72. {
  73. fCameraDistance = Mathf.Lerp(fCameraDistance,fCameraLerpValue,Time.deltaTime*1.5f);
  74. fCurrentCamDir = Mathf.Lerp(fCurrentCamDir,-hControllerScriptCS.getCurrentPlayerRotation()+90.0f,Time.deltaTime*4.0f);
  75. tCamera.localEulerAngles = new Vector3(fCameraRotationX, fCurrentCamDir, fCameraRotationZ);
  76. v3CamDirection = rotateAlongY(new Vector3(-1,0,0),-hControllerScriptCS.getCurrentPlayerRotation());
  77. if (iCameraState == 1) //regular gameplay
  78. {
  79. fCameraLerpValue = 35;//maintain a static distance between camera and the player
  80. tCamera.position = new Vector3( tPlayerMesh.position.x + v3CamDirection.x*fCameraDistance + fCameraPositionX,
  81. Mathf.Lerp(tCamera.position.y, tPlayerMesh.position.y + fCameraPositionY, Time.deltaTime*70),
  82. Mathf.Lerp(tCamera.position.z, (tPlayerMesh.position.z + v3CamDirection.z*fCameraDistance), Time.deltaTime*50) );
  83. }
  84. else if(iCameraState == 2) //Camera on death
  85. {
  86. fCameraLerpValue = 60;//increase the distance between the camera and the player
  87. tCamera.position = tPlayerMesh.position + v3CamDirection*fCameraDistance;
  88. tCamera.position = new Vector3(tCamera.position.x, tCamera.position.y+30, tCamera.position.z);//increase the height of the camera
  89. //change the camera angle (look at the death scene)
  90. tCamera.localEulerAngles = new Vector3(Mathf.Lerp(tCamera.localEulerAngles.x, 40, Time.deltaTime*25), tCamera.localEulerAngles.y,
  91. tCamera.localEulerAngles.z);
  92. }
  93. //make the camera shake if the fCamShakeImpulse is not zero
  94. if(fCamShakeImpulse>0.0f)
  95. shakeCamera();
  96. }
  97. /*
  98. * FUNCTION: Calculate camera rotation vector based on player movement
  99. * CALLED BY: CameraMain()
  100. * PARAMETER 1: Camera rotation vector
  101. * PARAMETER 2: Player rotation value
  102. */
  103. private Vector3 rotateAlongY(Vector3 inputVector, float angletoRotate)
  104. {
  105. Vector3 FinalVector = Vector3.zero;
  106. angletoRotate = angletoRotate/57.3f;
  107. FinalVector.x = Mathf.Cos(-angletoRotate) * inputVector.x - Mathf.Sin(-angletoRotate) * inputVector.z;
  108. FinalVector.z = Mathf.Sin(-angletoRotate) * inputVector.x + Mathf.Cos(-angletoRotate) * inputVector.z;
  109. return FinalVector;
  110. }
  111. /*
  112. * FUNCTION: Set the intensity of camera vibration
  113. * PARAMETER 1: Intensity value of the vibration
  114. */
  115. public void setCameraShakeImpulseValue(int iShakeValue)
  116. {
  117. if(iShakeValue==1)
  118. fCamShakeImpulse = 1.0f;
  119. else if(iShakeValue==2)
  120. fCamShakeImpulse = 2.0f;
  121. else if(iShakeValue==3)
  122. fCamShakeImpulse = 1.3f;
  123. else if(iShakeValue==4)
  124. fCamShakeImpulse = 1.5f;
  125. else if(iShakeValue==5)
  126. fCamShakeImpulse = 1.3f;
  127. }
  128. /*
  129. * FUNCTION: Make the camera vibrate. Used for visual effects
  130. */
  131. private void shakeCamera()
  132. {
  133. tCamera.position += new Vector3(0, Random.Range(-fCamShakeImpulse,fCamShakeImpulse),
  134. Random.Range(-fCamShakeImpulse,fCamShakeImpulse));
  135. fCamShakeImpulse-=Time.deltaTime * fCamShakeImpulse*4.0f;
  136. if(fCamShakeImpulse<0.01f)
  137. fCamShakeImpulse = 0.0f;
  138. }
  139. }