Created
October 19, 2017 19:55
-
-
Save JackDraak/e550407511ffe4c85d864f32520a7e31 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment