using UnityEngine;
using System.Collections;
public class Axis : MonoBehaviour {
public Axe customAxe;
public float value;
// Use this for initialization
void Start () {
StartCoroutine(this.customAxe.Update());
}
// Update is called once per frame
void Update () {
this.value = this.customAxe.Value;
}
}
[System.Serializable]
public class Axe{
public KeyCode NegativeButton;
public KeyCode PositiveButton;
public KeyCode AltNegativeButton;
public KeyCode AltPositiveButton;
public float Range = 1f;
public float smoothTimeUp = 1f;
public float smoothTimeDown = 1f;
public float Dead = 0.001f;
public float Value{ get; private set; }
private float velocity = 0f;
public IEnumerator Update(){
while(Application.isPlaying){
if(Input.GetKey(this.NegativeButton) || Input.GetKey(this.AltNegativeButton)){
this.Value = Mathf.SmoothDamp(this.Value,-this.Range,ref this.velocity, this.smoothTimeUp);
if(this.Value+this.Range < this.Dead) this.Value = -this.Range;
} else if(Input.GetKey(this.PositiveButton) || Input.GetKey(this.AltPositiveButton)){
this.Value = Mathf.SmoothDamp(this.Value,this.Range,ref this.velocity,this.smoothTimeUp);
if(this.Range-this.Value < this.Dead) this.Value = this.Range;
} else {
this.Value = Mathf.SmoothDamp(this.Value,0f,ref this.velocity,this.smoothTimeDown);
if(Mathf.Abs(this.Value) < this.Dead) this.Value = 0f;
}
yield return 0;
}
}
}
He creado una clase similar a la que utilizar Unity en el InputManager.En lugar de Sensitivity y Gravity, utilizo smoothTimeUp y smoothTimeDown para el incremento y disminucion de valor.También he incluido una variable Range, para establecer el intervalo y que no sea siempre uno.La variable Dead es el margen o distancia minima a los valores -Range, 0, Range.La clase se actualiza desde una corrutine, y para acceder a su valor consultamos la propiedad Value.
[quote author=kaito date=1453581884" data-ipsquote="" data-cite="kaito" class="ipsQuote">
using UnityEngine;using System.Collections;
public class Axis : MonoBehaviour {
public Axe customAxe;
public float value;
// Use this for initialization
void Start () {
StartCoroutine(this.customAxe.Update());
}
// Update is called once per frame
void Update () {
this.value = this.customAxe.Value;
}
}
[System.Serializable]
public class Axe{
public KeyCode NegativeButton;
public KeyCode PositiveButton;
public KeyCode AltNegativeButton;
public KeyCode AltPositiveButton;
public float Range = 1f;
public float smoothTimeUp = 1f;
public float smoothTimeDown = 1f;
public float Dead = 0.001f;
public float Value{ get; private set; }
private float velocity = 0f;
public IEnumerator Update(){
while(Application.isPlaying){
if(Input.GetKey(this.NegativeButton) || Input.GetKey(this.AltNegativeButton)){
this.Value = Mathf.SmoothDamp(this.Value,-this.Range,ref this.velocity,