- Home /
Barrel Roll on Double Key Press, right & left
I'm trying to make it so when the player double presses right or left, the vehicle barrel rolls in that direction, the roll works but it won't activate on either double-press.
void Dodge()
{
if (Input.GetKey("right"))
{
if (ButtonCooler > 0 && ButtonCount == 1)
{
playerBody.AddRelativeForce(25000, 1000, 0);
rotator.Speed = 750;
rotator.enabled = true;
aclAxis = 0;
}
}
else if (Input.GetKey("left"))
{
if (ButtonCooler > 0 && ButtonCount == 1)
{
playerBody.AddRelativeForce(-25000, 1000, 0);
rotator.Speed = -750;
rotator.enabled = true;
aclAxis = 0;
}
}
else
{
ButtonCooler = 0.5f;
ButtonCount += 1;
}
if (ButtonCooler > 0) ButtonCooler -= 1 * Time.deltaTime; else ButtonCount = 0; //Reset
}
Answer by Jwizard93 · Jul 08, 2017 at 12:09 AM
A while loop is not a timer. You need to initialize more variables that will persist between frames.
private bool pressedOnce;
private float time;
private float timerLength;
void Start()
{
pressedOnce = false;
time = 0;
timerLength = 1; //one second
}
void Update()
{
if(Input.GetKeyDown(KeyCode.Right))
{
if(!pressedOnce)
{
pressedOnce = true;
time = Time.time;
}
else
{
//roll
}
}
if(pressedOnce)
{
if(Time.time - time > timerLength)
{
pressedOnce = false;
}
time += Time.deltaTime;
}
}
Thanks it triggers now but the movement is weird and the script for the roll gets activated but doesn't effectively roll.
I have this code in an Enumerator which activates when pressing a key + right or left and it works like intended: the ship makes a nice roll
if (Input.Get$$anonymous$$ey("right")) // Barrel Roll
{
playerBody.AddRelativeForce(25000, 1000, 0);
rotator.Speed = 750;
}
else if (Input.Get$$anonymous$$ey("left"))
{
playerBody.AddRelativeForce(-25000, 1000, 0);
rotator.Speed = -750;
}
rotator.enabled = true;
aclAxis = 0;
but ins$$anonymous$$d of having the roll linked to an existing skill, i'd like to make a basic feature out of it which is why i went for double-press in Update and that code is now:
if (Input.Get$$anonymous$$eyDown("right"))
{
if (!pressedOnce)
{
pressedOnce = true;
time = Time.time;
}
else
{
playerBody.AddRelativeForce(25000, 1000, 0);
rotator.Speed = 750;
rotator.enabled = true;
aclAxis = 0;
}
}
if (Input.Get$$anonymous$$eyDown("left"))
{
if (!pressedOnce)
{
pressedOnce = true;
time = Time.time;
}
else
{
playerBody.AddRelativeForce(-25000, 1000, 0);
rotator.Speed = -750;
rotator.enabled = true;
aclAxis = 0;
}
}
if (pressedOnce)
{
if (Time.time - time > timerLength)
{
pressedOnce = false;
}
time += Time.deltaTime;
}
Why are the results so different, shouldn't it be the same?
Answer by MikePOD · Jul 07, 2017 at 12:29 PM
The way I would do it is just create a timer after the first button press, and if the button is pressed again in that time, the roll should execute.
if(Input.GetKey(KeyCode.Right))
{
int t = 0;
while(t < 5)
{
if(Input.GetKey(KeyCode.Right))
{
playerBody.AddRelativeForce(25000, 1000, 0);
rotator.Speed = 750;
rotator.enabled = true;
aclAxis = 0;
}
t++;
}
}
Tried it out but doesn't work, after one press the pressure is continuously applied and i tried to prevent it from running multiple times but doesn't fix the issue, because the code is run in Update
$$anonymous$$aybe try making the first check a Get$$anonymous$$eyUp, and the second a Get$$anonymous$$eyDown. I think that should only make the while loop trigger after the first press has been released, so the pressure won't be continuously applied.
now it doesn't trigger, only added Up to first and Down to the 2nd as you said
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Distribute terrain in zones 3 Answers
I want to play an Audio Sound for a character while running. 0 Answers
problem with mapping keys through c# 1 Answer
Multiple Cars not working 1 Answer