Guest User

Untitled

a guest
Oct 20th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class DroneController : MonoBehaviour {
  7. Rigidbody rigidbody;
  8. AudioSource audio;
  9. bool thrustAudio = false;
  10. bool thrustAudioOn = false;
  11.  
  12. // Use this for initialization
  13. void Start () {
  14. rigidbody = GetComponent<Rigidbody>();
  15. audio = GetComponent<AudioSource>();
  16. }
  17.  
  18. // Update is called once per frame
  19. void Update () {
  20. ProcessInput();
  21. ProcessAudio();
  22. }
  23.  
  24. private void ProcessInput()
  25. {
  26. var key_w = Input.GetKey(KeyCode.W);
  27. var key_a = Input.GetKey(KeyCode.A);
  28. var key_d = Input.GetKey(KeyCode.D);
  29. var key_ua = Input.GetKey(KeyCode.UpArrow);
  30. var key_la = Input.GetKey(KeyCode.LeftArrow);
  31. var key_ra = Input.GetKey(KeyCode.RightArrow);
  32.  
  33. if (key_w || key_a || key_d || key_ua || key_la || key_ra) thrustAudio = true;
  34. else thrustAudio = false;
  35.  
  36. if (key_w || key_ua) Thrust(0); // Main thrust
  37. if (key_a || key_la) { Thrust(-1); return; } // Port thrust
  38. else if (key_d || key_ra) { Thrust(1); return; } // Starboard thrust
  39.  
  40. return;
  41. }
  42.  
  43. private void ProcessAudio()
  44. {
  45. if (!thrustAudioOn && thrustAudio)
  46. {
  47. audio.Play();
  48. thrustAudioOn = true;
  49. }
  50. else if (thrustAudioOn && !thrustAudio)
  51. {
  52. audio.Stop();
  53. thrustAudioOn = false;
  54. }
  55. }
  56.  
  57. private void Thrust(int v)
  58. {
  59. switch(v)
  60. {
  61. case -1: // Port
  62. Debug.Log("Port thrust " + Time.time);
  63. transform.Rotate(Vector3.back);
  64. break;
  65. case 0: // Main
  66. Debug.Log("Main thrust " + Time.time);
  67. rigidbody.AddRelativeForce(Vector3.up);
  68. break;
  69. case 1: // Starboard
  70. Debug.Log("Starboard thrust " + Time.time);
  71. transform.Rotate(Vector3.forward);
  72. break;
  73. default:
  74. throw new NotImplementedException();
  75. }
  76. }
  77. }
Add Comment
Please, Sign In to add comment