Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class SimpleMovement : MonoBehaviour {
- public float speed = 5f;
- public Buttons[] input;
- private Rigidbody2D body2d;
- private InputState inputState;
- // Use this for initialization
- void Start () {
- body2d = GetComponent<Rigidbody2D> ();
- inputState = GetComponent<InputState> ();
- }
- // Update is called once per frame
- void Update () {
- Debug.Log(inputState + " - " + input[1]);
- var right = inputState.GetButtonValue (input [0]);
- var left = inputState.GetButtonValue (input [1]);
- var velX = speed;
- if (right || left) {
- velX *= left ? -1 : 1;
- } else {
- velX = 0;
- }
- body2d.velocity = new Vector2 (velX, body2d.velocity.y);
- }
- }
- //Second script
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- public class ButtonState{
- public bool value;
- public float holdTime = 0;
- }
- public class InputState : MonoBehaviour {
- private Dictionary<Buttons, ButtonState> buttonStates = new Dictionary<Buttons, ButtonState>();
- public void SetButtonValue(Buttons key, bool value){
- if(!buttonStates.ContainsKey(key))
- buttonStates.Add(key, new ButtonState());
- var state = buttonStates [key];
- if (state.value && !value) {
- Debug.Log ("Button " + key + " released "+state.holdTime);
- state.holdTime = 0;
- } else if (state.value && value) {
- state.holdTime += Time.deltaTime;
- Debug.Log("Button "+key+" down "+state.holdTime);
- }
- state.value = value;
- }
- public bool GetButtonValue(Buttons key){
- if (buttonStates.ContainsKey (key))
- return buttonStates [key].value;
- else
- return false;
- }
- }
- //third code
- using UnityEngine;
- using System.Collections;
- public enum Buttons
- {
- Right,
- Left
- }
- public enum Condition
- {
- GreaterThan,
- LessThan
- }
- [System.Serializable]
- public class InputAxisState
- {
- public string axisName;
- public float offValue;
- public Buttons button;
- public Condition condition;
- public bool value
- {
- get{
- var val = Input.GetAxis(axisName);
- switch(condition){
- case Condition.GreaterThan:
- return val > offValue;
- case Condition.LessThan:
- return val < offValue;
- }
- return false;
- }
- }
- }
- public class InputManager : MonoBehaviour
- {
- public InputAxisState[] inputs;
- public InputState inputState;
- void Update ()
- {
- foreach (var input in inputs)
- {
- inputState.SetButtonValue(input.button, input.value);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement