- Home /
Alternating buttons to keep moving.
Hello there,
I am currently developping a little prototype in C# on Unity 5.3. The game is a 2D Side scroll game where the player mainly has to walk (same movement system as Darkest Dungeon / link : https://youtu.be/J-X5-KRJ2JQ?t=997 )
The kind of input I want for the player to move is a direction and a way to accelerate. I provide my diretion using a joystick, and I want the player to accelerate by pressing alternately RT and LT on a xbox 360 controller.
I want also my max speed to fit the rhythm the player is pressing those 2 inputs.
Do you know a way to do it ? Even with only one button, no alternation aspect, it will be a beginning.
Thank you for your time !
hi..there is a manual in unity wiki. here you go link
Answer by Dragday · Feb 23, 2016 at 01:12 PM
try to use
Input.GetButton()
and change the movespeed of the player.
I think that would do it.
You can set a time inside, so the "shorter"is the times when pressed the other button the faster it is.
Answer by jayjf · Feb 23, 2016 at 01:03 PM
Replace the GetKeyDown methods with the methods for the buttons. Otherwise this should do the trick. This might need some fine tuning. For example, if you set optimal time
to a high value you might want to set the speed to a minimum value.
public class AlternatingButtons : MonoBehaviour {
private float _lastInputTime;
private float _maxSpeed = 10;
private float _optimalTime = 0.5f; //your optimal time between inputs in sec
private float _speed;
private bool _lastInputRight = false;
void Update()
{
if(Input.GetKeyDown(KeyCode.A))
{
if(_lastInputRight)
{
CalcSpeed();
}
_lastInputRight = false;
}
if (Input.GetKeyDown(KeyCode.D))
{
if(!_lastInputRight)
{
CalcSpeed();
}
_lastInputRight = true;
}
}
private void CalcSpeed()
{
var timeSinceLastInput = Time.time - _lastInputTime;
var speedParam = 1 - Mathf.Clamp01(Mathf.Abs(_optimalTime - timeSinceLastInput));
_speed = Mathf.Lerp(0, _maxSpeed, speedParam);
_lastInputTime = Time.time;
Debug.Log(_speed);
}
}