Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.94 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. public class Player : MonoBehaviour {
  5. public int collectibles = 0;
  6. public UnityEngine.UI.Image collectibleImage;
  7. public GameObject collectibleBG;
  8. UnityEngine.UI.Text collectibleText;
  9. public AudioSource robotMovement;
  10. CameraFilterPack_FX_EarthQuake screenShake;
  11. CameraFilterPack_Atmosphere_Fog fog;
  12. CameraFilterPack_Blizzard blizzard;
  13. AudioSource source;
  14. public bool canMove = true;
  15. Camera cam;
  16. public bool jumped;
  17. public bool doubleJumped;
  18. public bool forestPlaying = false;
  19. public bool desertPlaying = false;
  20. public float gravityScale;
  21. public float movementSpeed;
  22. public float runSpeed;
  23. public float gravity;
  24. Vector3 down = new Vector3(0,0,0);
  25. public Vector3 move;
  26. CharacterController player;
  27. public float lookThreshold;
  28. public float moveThreshold;
  29. public float inputThreshold;
  30. public float deadThreshold;
  31. float yBackup = 0;
  32. public Vector3 input;
  33. Transform camCopy;
  34. Vector3 oldInput;
  35. public GameObject camPlaceholder;
  36. public GameObject bButton;
  37. public GameObject interactText;
  38. public GameObject interactable;
  39. public AudioClip interactClip;
  40. public GameObject SoundtrackObj;
  41. bool keyboard = false;
  42.  
  43. public float jumpSpeed;
  44. public bool grounded;
  45. bool running;
  46. Animator anim;
  47. public AudioSource Soundtrack;
  48. public AudioClip ForestTrack;
  49. public AudioClip DesertTrack;
  50. public AudioSource[] ambienceObjects;
  51. public AudioClip[] ambiences;
  52. public string terrain;
  53. public AudioClip[] fSteps;
  54. public AudioClip[] dSteps;
  55. public AudioClip[] frSteps;
  56. public AudioClip[] drSteps;
  57. public AudioClip[] moveSounds;
  58. public AudioClip[] runSounds;
  59. public AudioClip jump;
  60. public AudioClip land;
  61. public bool paused;
  62. public GameObject pauseUI;
  63. public MenuController menuController;
  64.  
  65. float lastMove;
  66. Vector3 oldPos;
  67.  
  68. // Use this for initialization
  69. void Start() {
  70. fog = Camera.main.GetComponent<CameraFilterPack_Atmosphere_Fog>();
  71.  
  72. blizzard = Camera.main.GetComponent<CameraFilterPack_Blizzard>();
  73. collectibleImage = GameObject.Find("CollectibleImage").GetComponent<UnityEngine.UI.Image>();
  74. collectibleBG = GameObject.Find("CollectibleBG");
  75. collectibleBG.SetActive(false);
  76. collectibleText = GameObject.Find("CollectibleCounter").GetComponent<UnityEngine.UI.Text>();
  77. collectibleText.gameObject.SetActive(false);
  78. screenShake = Camera.main.GetComponent<CameraFilterPack_FX_EarthQuake>();
  79. cam = Camera.main;
  80. player = GetComponent<CharacterController>();
  81. source = GetComponent<AudioSource>();
  82. source.clip = land;
  83. player.enabled = true;
  84. anim = GetComponent<Animator>();
  85. Soundtrack = SoundtrackObj.GetComponent<AudioSource>();
  86.  
  87. }
  88.  
  89. // Update is called once per frame
  90. void Update() {
  91. if (move.magnitude > 1) {
  92.  
  93. lastMove = Time.time;
  94. }
  95. if (Input.GetButtonDown("Pause"))
  96. {
  97. Pause();
  98. }
  99. if (Input.GetKeyDown(KeyCode.K))
  100. {
  101. keyboard = !keyboard;
  102. cam.GetComponent<CamScript>().keyboard = !cam.GetComponent<CamScript>().keyboard;
  103.  
  104. Cursor.lockState = keyboard == true ? CursorLockMode.Locked : CursorLockMode.None;
  105.  
  106. }
  107. if (!paused) {
  108.  
  109.  
  110. running = Running();
  111.  
  112. //Interact
  113. if (interactable) {
  114. if (Input.GetButtonDown("Interact"))
  115. {
  116. interactable.GetComponent<Interactable>().Interact();
  117. source.clip = interactClip;
  118. source.Play();
  119. }
  120. }
  121.  
  122. yBackup = move.y;
  123. input = keyboard == true ? new Vector3(Input.GetAxis("HorKey"), 0, Input.GetAxis("VertKey")) : new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
  124.  
  125.  
  126.  
  127. ThresholdMove(input, inputThreshold);
  128. move = cam.transform.TransformDirection(input);
  129.  
  130. ThresholdMove(move, moveThreshold);
  131. move.x += Mathf.Abs(move.y) * (move.x / (Mathf.Abs(move.x) + Mathf.Abs(move.z)));
  132. move.z += Mathf.Abs(move.y) * (move.z / (Mathf.Abs(move.x) + Mathf.Abs(move.z)));
  133. move.y = yBackup;
  134. if (float.IsNaN(move.x))
  135. {
  136. move.x = 0;
  137. }
  138. if (float.IsNaN(move.z))
  139. {
  140. move.z = 0;
  141. }
  142.  
  143. //Jump
  144. if (Input.GetButtonDown("Jump"))
  145. {
  146. if (player.isGrounded)
  147. {
  148. source.clip = jump;
  149. source.pitch = Random.Range(0.9f, 1.05f);
  150. source.Play();
  151. jumped = true;
  152. move.y = jumpSpeed;
  153. }
  154. else if(!doubleJumped) {
  155. doubleJumped = true;
  156. move.y = jumpSpeed;
  157. }
  158. }
  159. if (move.magnitude > lookThreshold)
  160. {
  161. transform.LookAt(transform.position + new Vector3(move.x, 0, move.z));
  162. }
  163. if (canMove)
  164. {
  165. yBackup = move.y;
  166. down.y = move.y;
  167. move.y = 0;
  168. if (!running)
  169. {
  170. player.Move(move * movementSpeed * Time.deltaTime);
  171. }
  172. else {
  173. player.Move(move * runSpeed * Time.deltaTime);
  174. }
  175. player.Move(down * gravityScale * Time.deltaTime);
  176. move.y = yBackup;
  177. }
  178. }
  179. if (!player.isGrounded)
  180. {
  181. move.y -= gravity * Time.deltaTime;
  182. }
  183. if (player.isGrounded) {
  184. doubleJumped = false;
  185. jumped = false;
  186. }
  187.  
  188. //Audio on terrain
  189. if (input.magnitude > 0.05f && !source.isPlaying && player.isGrounded) {
  190. source.pitch = 1;
  191. if (!running)
  192. {
  193. if (terrain == "Grass")
  194. {
  195. source.clip = fSteps[(int)Random.Range(0, fSteps.Length)];
  196. source.Play();
  197. }
  198. else if (terrain == "Sand")
  199. {
  200. source.clip = dSteps[(int)Random.Range(0, dSteps.Length)];
  201. source.Play();
  202. }
  203. robotMovement.clip = moveSounds[(int)Random.Range(0, moveSounds.Length)];
  204. robotMovement.Play();
  205. }
  206. else
  207. {
  208. if (terrain == "Grass")
  209. {
  210. source.clip = frSteps[(int)Random.Range(0, frSteps.Length)];
  211. source.Play();
  212. }
  213. else if (terrain == "Sand")
  214. {
  215. source.clip = drSteps[(int)Random.Range(0, drSteps.Length)];
  216. source.Play();
  217. }
  218. robotMovement.clip = runSounds[(int)Random.Range(0, runSounds.Length)];
  219. robotMovement.Play();
  220. }
  221.  
  222. }
  223.  
  224. oldInput = input;
  225. grounded = player.isGrounded;
  226. anim.SetBool("DoubleJumped", doubleJumped);
  227. anim.SetBool("Idle", Time.time > lastMove + 10);
  228. anim.SetBool("Running", running);
  229. anim.SetBool("Jumping", jumped);
  230. anim.SetFloat("Vel", input.magnitude);
  231. oldPos = transform.position;
  232. }
  233.  
  234. Vector3 ThresholdMove(Vector3 input, float threshold) {
  235. if (Mathf.Abs(input.x) < threshold)
  236. {
  237. input.x = 0;
  238. }
  239. if (Mathf.Abs(input.z) < threshold)
  240. {
  241. input.z = 0;
  242. }
  243. return input;
  244.  
  245. }
  246.  
  247. //void OnControllerColliderHit(ControllerColliderHit hit) { //When the character lands on different terrain
  248.  
  249. // if (hit.gameObject.tag == "Grass") {
  250. // CheckLand();
  251. // if (!(terrain == "Grass")) {
  252. // StartCoroutine(FadeFogOut());
  253. // terrain = "Grass";
  254. // for (int i = 0; i < ambienceObjects.Length; i++) {
  255.  
  256. // ambienceObjects[i].clip = ambiences[0];
  257. // ambienceObjects[i].Play();
  258. // }
  259. // }
  260. // }
  261. // else if (hit.gameObject.tag == "Sand") {
  262. // CheckLand();
  263. // if (!(terrain == "Sand"))
  264. // {
  265.  
  266. // StartCoroutine(FadeFogIn());
  267. // terrain = "Sand";
  268. // for (int i = 0; i < ambienceObjects.Length; i++)
  269. // {
  270. // ambienceObjects[i].clip = ambiences[1];
  271. // ambienceObjects[i].Play();
  272. // }
  273. // }
  274. // }
  275.  
  276.  
  277.  
  278.  
  279. // }
  280. IEnumerator FadeFogIn()
  281. {
  282. blizzard.enabled = true;
  283. fog.enabled = true;
  284. float fogVal = 0.75f;
  285. float blizzVal = 0.2f;
  286.  
  287. fog.Fade = 0;
  288. blizzard._Fade = 0;
  289. for (int i = 0; i < 20; i++)
  290. {
  291. fog.Fade += (fogVal/20) ;
  292. blizzard._Fade += (blizzVal/20);
  293. yield return new WaitForSeconds(0.1f);
  294. }
  295. yield break;
  296. }
  297. IEnumerator FadeFogOut() {
  298. float fogVal = 0.75f;
  299. float blizzVal = 0.2f;
  300. fog.Fade = fogVal;
  301. blizzard._Fade = blizzVal;
  302. for (int i = 0; i < 20; i++)
  303. {
  304. fog.Fade -= (fogVal / 20);
  305. blizzard._Fade -= (blizzVal / 20);
  306. yield return new WaitForSeconds(0.1f);
  307. }
  308. blizzard.enabled = false;
  309. fog.enabled = false;
  310. yield break;
  311. }
  312.  
  313. void CheckLand() {
  314.  
  315. if (!grounded && source.clip.name == "Jump")
  316. {
  317. source.clip = land;
  318. source.pitch = Random.Range(0.9f, 1.05f);
  319. source.Play();
  320. StartCoroutine(ScreenShake());
  321. }
  322. }
  323.  
  324. IEnumerator ScreenShake() {
  325. screenShake.enabled = true;
  326. screenShake.Speed = Random.Range(40, 60);
  327. screenShake.X = Random.Range(.004f, .01f);
  328. screenShake.Y = Random.Range(.004f, .01f);
  329. yield return new WaitForSeconds(Random.Range(0.25f, 0.6f));
  330. screenShake.enabled = false;
  331. yield break;
  332. }
  333. void OnTriggerEnter(Collider other)
  334. {
  335.  
  336. print("Entered");
  337. if (other.gameObject.tag == "EventTrigger")
  338. {
  339. EventSpawnTrigger trigger = other.gameObject.GetComponent<EventSpawnTrigger>();
  340. if (!trigger.triggered)
  341. { //If we haven't triggered this collider before
  342. trigger.triggered = true;
  343. print("Spawned an Event");
  344. trigger.Spawn();
  345. }
  346. }
  347. else if (other.tag == "Interactable" || other.tag == "Collectible") {
  348.  
  349. bButton.SetActive(true);
  350. interactText.SetActive(true);
  351. interactable = other.gameObject;
  352. }
  353.  
  354. if (other.gameObject.tag == "Grass")
  355. {
  356. if (!forestPlaying)
  357. {
  358. forestPlaying = true;
  359. desertPlaying = false;
  360. Soundtrack.clip = ForestTrack;
  361. Soundtrack.Play();
  362. }
  363. CheckLand();
  364. if (!(terrain == "Grass"))
  365. {
  366. StartCoroutine(FadeFogOut());
  367. terrain = "Grass";
  368. for (int i = 0; i < ambienceObjects.Length; i++)
  369. {
  370.  
  371. ambienceObjects[i].clip = ambiences[0];
  372. ambienceObjects[i].Play();
  373. }
  374. }
  375. }
  376. else if (other.gameObject.tag == "Sand")
  377. {
  378. if (!desertPlaying)
  379. {
  380. desertPlaying = true;
  381. forestPlaying = false;
  382. Soundtrack.clip = DesertTrack;
  383. Soundtrack.Play();
  384. }
  385. CheckLand();
  386. if (!(terrain == "Sand"))
  387. {
  388.  
  389. StartCoroutine(FadeFogIn());
  390. terrain = "Sand";
  391. for (int i = 0; i < ambienceObjects.Length; i++)
  392. {
  393. ambienceObjects[i].clip = ambiences[1];
  394. ambienceObjects[i].Play();
  395. }
  396. }
  397. }
  398.  
  399. }
  400. void OnTriggerExit(Collider other)
  401. {
  402. if (other.tag == "Interactable")
  403. {
  404. ExitInteract();
  405. }
  406. }
  407. public void ExitInteract() {
  408. interactText.SetActive(false);
  409. bButton.SetActive(false);
  410. interactable = null;
  411. }
  412.  
  413. bool Running() {
  414. return Input.GetButton("Run");
  415. }
  416. public void Pause() {
  417. paused = !paused;
  418. //Time.timeScale = Time.timeScale == 1 ? 0 : 1;
  419. menuController.StartDetecting(paused);
  420. pauseUI.SetActive(!pauseUI.activeInHierarchy);
  421. }
  422. public void UpdateCollectibles() { //Update score in corner of screen.
  423. collectibles++;
  424. StartCoroutine(CollectibleTick());
  425. }
  426.  
  427. IEnumerator CollectibleTick() { //Actually updates collectibles score.
  428. collectibleText.gameObject.SetActive(true);
  429. collectibleText.text = collectibles + "/??";
  430. yield return new WaitForSeconds(7.5f);
  431. collectibleText.gameObject.SetActive(false);
  432. yield break;
  433. }
  434.  
  435.  
  436.  
  437. /* void OnTriggerStay(Collider other) {
  438. if (other.tag == "Interactable") {
  439. if (Input.GetButtonDown("Interact")) {
  440. other.GetComponent<Interactable>().Interact();
  441. }
  442. }
  443. }*/
  444. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement