Skip to content

Instantly share code, notes, and snippets.

@ashblue
Created February 25, 2015 14:09
Show Gist options
  • Save ashblue/58a9ddd298376a6a585d to your computer and use it in GitHub Desktop.
Save ashblue/58a9ddd298376a6a585d to your computer and use it in GitHub Desktop.
Unity player input monitoring wrapper
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using BehaviorDesigner.Runtime;
public class PlayerInputMonitor : MonoBehaviour {
// Behavior variables
Behavior behavior;
Dictionary<string, SharedBool> behaviorBools;
Dictionary<string, SharedFloat> behaviorFloats;
// Input monitors
[HideInInspector] public float hSpeed;
[HideInInspector] public float vSpeed;
[HideInInspector] public bool attack;
[HideInInspector] public bool attackAlt;
[HideInInspector] public bool jump;
bool jumpRelease;
bool ready = false;
[Tooltip("Used to spit out artifical no input defaults during menus, cutscenes, ect.")]
public bool disableInput = false;
public bool forwardPress;
[SerializeField] float forwardPressDelay = 0.3f; // How long is the forward press valid for
[SerializeField] float forwardPressThreshold = 0.3f;
float forwardPressCountdown; // Timer countdown
bool forwardPressEnabled = false; // Determines if forward press is availble again
void Awake () {
behaviorBools = new Dictionary<string, SharedBool>();
behaviorFloats = new Dictionary<string, SharedFloat>();
behavior = GetComponent<Behavior>();
}
void BTReady () {
if (ready) return;
behaviorBools.Add("attack", behavior.GetVariable("attack") as SharedBool);
behaviorBools.Add("attackAlt", behavior.GetVariable("attackAlt") as SharedBool);
behaviorBools.Add("jump", behavior.GetVariable("jump") as SharedBool);
behaviorBools.Add("crouch", behavior.GetVariable("crouch") as SharedBool);
behaviorBools.Add("jumpRelease", behavior.GetVariable("jumpRelease") as SharedBool);
behaviorFloats.Add("hSpeed", behavior.GetVariable("hSpeed") as SharedFloat);
behaviorFloats.Add("vSpeed", behavior.GetVariable("vSpeed") as SharedFloat);
ready = true;
}
void Update () {
if (!ready) return;
// Read input in Update so button presses aren't missed.
if (Input.GetButtonDown("Jump")) jump = true;
if (Input.GetButtonUp("Jump")) jumpRelease = true;
if (Input.GetButtonDown("Fire1")) attack = true;
if (Input.GetButtonDown("Fire2")) attackAlt = true;
}
void FixedUpdate () {
if (ready && !disableInput) {
hSpeed = Input.GetAxis("Horizontal");
vSpeed = Input.GetAxis("Vertical");
bool crouch = vSpeed < 0 ? true : false;
// @TODO Move all into public vars and remove
// Update corresponding input receivers
behaviorBools["attack"].Value = attack;
behaviorBools["jump"].Value = jump;
behaviorBools["jumpRelease"].Value = jumpRelease;
behaviorBools["attackAlt"].Value = attackAlt;
behaviorBools["crouch"].Value = crouch;
behaviorFloats["hSpeed"].Value = hSpeed;
behaviorFloats["vSpeed"].Value = vSpeed;
UpdateForwardPress();
} else if (ready && disableInput) {
hSpeed = 0f;
vSpeed = 0f;
behaviorBools["attack"].Value = false;
behaviorBools["jump"].Value = false;
behaviorBools["jumpRelease"].Value = false;
behaviorBools["attackAlt"].Value = false;
behaviorBools["crouch"].Value = false;
behaviorFloats["hSpeed"].Value = hSpeed;
behaviorFloats["vSpeed"].Value = vSpeed;
}
BehaviorManager.instance.Tick(behavior);
// Reset the input once it has been used
jump = false;
jumpRelease = false;
attack = false;
attackAlt = false;
}
void UpdateForwardPress () {
forwardPressCountdown -= Time.deltaTime;
// Check if pushing forward has returned
if (Mathf.Abs(hSpeed) <= forwardPressThreshold) {
forwardPressEnabled = true;
}
// Enable forward press
if (Mathf.Abs(hSpeed) > forwardPressThreshold && !forwardPress && forwardPressEnabled) {
forwardPress = true;
forwardPressEnabled = false;
forwardPressCountdown = forwardPressDelay;
}
// disable forward press after expiration
if (forwardPress && forwardPressCountdown < 0) {
forwardPress = false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment