Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class DroneController : MonoBehaviour {
- Rigidbody rigidbody;
- AudioSource audio;
- bool thrustAudio = false;
- bool thrustAudioOn = false;
- // Use this for initialization
- void Start () {
- rigidbody = GetComponent<Rigidbody>();
- audio = GetComponent<AudioSource>();
- }
- // Update is called once per frame
- void Update () {
- ProcessInput();
- ProcessAudio();
- }
- private void ProcessInput()
- {
- var key_w = Input.GetKey(KeyCode.W);
- var key_a = Input.GetKey(KeyCode.A);
- var key_d = Input.GetKey(KeyCode.D);
- var key_ua = Input.GetKey(KeyCode.UpArrow);
- var key_la = Input.GetKey(KeyCode.LeftArrow);
- var key_ra = Input.GetKey(KeyCode.RightArrow);
- if (key_w || key_a || key_d || key_ua || key_la || key_ra) thrustAudio = true;
- else thrustAudio = false;
- if (key_w || key_ua) Thrust(0); // Main thrust
- if (key_a || key_la) { Thrust(-1); return; } // Port thrust
- else if (key_d || key_ra) { Thrust(1); return; } // Starboard thrust
- return;
- }
- private void ProcessAudio()
- {
- if (!thrustAudioOn && thrustAudio)
- {
- audio.Play();
- thrustAudioOn = true;
- }
- else if (thrustAudioOn && !thrustAudio)
- {
- audio.Stop();
- thrustAudioOn = false;
- }
- }
- private void Thrust(int v)
- {
- switch(v)
- {
- case -1: // Port
- Debug.Log("Port thrust " + Time.time);
- transform.Rotate(Vector3.back);
- break;
- case 0: // Main
- Debug.Log("Main thrust " + Time.time);
- rigidbody.AddRelativeForce(Vector3.up);
- break;
- case 1: // Starboard
- Debug.Log("Starboard thrust " + Time.time);
- transform.Rotate(Vector3.forward);
- break;
- default:
- throw new NotImplementedException();
- }
- }
- }
Add Comment
Please, Sign In to add comment