Advertisement
Guest User

Untitled

a guest
Aug 26th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class SimpleMovement : MonoBehaviour {
  5.  
  6. public float speed = 5f;
  7. public Buttons[] input;
  8.  
  9. private Rigidbody2D body2d;
  10. private InputState inputState;
  11.  
  12.  
  13. // Use this for initialization
  14. void Start () {
  15. body2d = GetComponent<Rigidbody2D> ();
  16. inputState = GetComponent<InputState> ();
  17. }
  18.  
  19. // Update is called once per frame
  20. void Update () {
  21. Debug.Log(inputState + " - " + input[1]);
  22. var right = inputState.GetButtonValue (input [0]);
  23. var left = inputState.GetButtonValue (input [1]);
  24. var velX = speed;
  25.  
  26. if (right || left) {
  27.  
  28. velX *= left ? -1 : 1;
  29. } else {
  30. velX = 0;
  31. }
  32.  
  33. body2d.velocity = new Vector2 (velX, body2d.velocity.y);
  34. }
  35. }
  36.  
  37.  
  38.  
  39.  
  40. //Second script
  41.  
  42.  
  43. using UnityEngine;
  44. using System.Collections;
  45. using System.Collections.Generic;
  46.  
  47. public class ButtonState{
  48. public bool value;
  49. public float holdTime = 0;
  50. }
  51.  
  52. public class InputState : MonoBehaviour {
  53.  
  54. private Dictionary<Buttons, ButtonState> buttonStates = new Dictionary<Buttons, ButtonState>();
  55.  
  56. public void SetButtonValue(Buttons key, bool value){
  57. if(!buttonStates.ContainsKey(key))
  58. buttonStates.Add(key, new ButtonState());
  59.  
  60. var state = buttonStates [key];
  61.  
  62. if (state.value && !value) {
  63. Debug.Log ("Button " + key + " released "+state.holdTime);
  64. state.holdTime = 0;
  65. } else if (state.value && value) {
  66.  
  67. state.holdTime += Time.deltaTime;
  68.  
  69. Debug.Log("Button "+key+" down "+state.holdTime);
  70. }
  71.  
  72. state.value = value;
  73.  
  74. }
  75.  
  76. public bool GetButtonValue(Buttons key){
  77. if (buttonStates.ContainsKey (key))
  78. return buttonStates [key].value;
  79. else
  80. return false;
  81. }
  82.  
  83. }
  84.  
  85.  
  86.  
  87. //third code
  88.  
  89.  
  90.  
  91. using UnityEngine;
  92. using System.Collections;
  93.  
  94. public enum Buttons
  95. {
  96. Right,
  97. Left
  98. }
  99.  
  100. public enum Condition
  101. {
  102. GreaterThan,
  103. LessThan
  104. }
  105.  
  106. [System.Serializable]
  107. public class InputAxisState
  108. {
  109. public string axisName;
  110. public float offValue;
  111. public Buttons button;
  112. public Condition condition;
  113.  
  114. public bool value
  115. {
  116.  
  117. get{
  118. var val = Input.GetAxis(axisName);
  119.  
  120. switch(condition){
  121. case Condition.GreaterThan:
  122. return val > offValue;
  123. case Condition.LessThan:
  124. return val < offValue;
  125. }
  126.  
  127. return false;
  128. }
  129.  
  130. }
  131. }
  132.  
  133. public class InputManager : MonoBehaviour
  134. {
  135.  
  136. public InputAxisState[] inputs;
  137. public InputState inputState;
  138.  
  139. void Update ()
  140. {
  141. foreach (var input in inputs)
  142. {
  143. inputState.SetButtonValue(input.button, input.value);
  144. }
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement