- Home /
Add value once when Trigger is pressed
I'm new to Unity and C# and currently playing around with character movements. I want my character to run when pressing the right trigger of my xbox one controller. I only have following problem: when pressing the right trigger, my running speed is added infinitely; when releasing the right trigger the running speed is subtracted infinitely. How can I apply my running speed to my character only when the right trigger is pressed? (Meaning as soon as I release the Trigger the speed is my 'normal speed').
This is the code:
if (Input.GetAxis("Run") == 1)
{
speed += running;
}
if (Input.GetAxis("Run") == -1)
{
speed -= running;
}
Answer by Deadog103 · Mar 29, 2020 at 10:28 PM
Its because you are constantly adding/subtracting the speed value whenever you use the triggers. Try making a variable that keeps the original speed then make speed equal that.
public float speed;
float origSpeed;
start()
{
origSpeed = speed;
}
//then in your IF statement put this:
if (Input.GetAxis("Run") == 1)
{
speed = running + origSpeed;
}
if (Input.GetAxis("Run") == -1)
{
speed = running - origSpeed;
}
I hope that helps
Your answer
Follow this Question
Related Questions
Pad Controller xbox360 - triggers 0 Answers
Xbox Controller and Unity Buttons UI 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Shoot when Xbox 1 trigger pressed. 0 Answers