I am trying to implement a vehicle boost mechanic in my game whereby when the user holds down shift in movement, the vehicle will speed up. That aspect works fine! But my issue is when the button is released and pressed again it remembers the boost value and then multiplies it. Instead, I would like for it to reset to the default value when released, and only increase when the button is pressed.
Here's what I have tried:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
[SerializeField]
private float movementSpeed;
private float movementBoost = 2f;
private float resetBoost = 10f;
void Start()
{
}
void Update()
{
HandleMovementInput();
Reset();
}
//Handle the player's movement using the keyboard.
void HandleMovementInput()
{
float moveVertical = Input.GetAxis("Vertical");
float moveHorizontal = Input.GetAxis("Horizontal");
Vector3 _movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
transform.Translate(_movement * movementSpeed * Time.deltaTime, Space.World);
//If the player holds down the shift button while moving, increase speed.
if (Input.GetButtonDown("Fire3"))
{
movementSpeed = movementSpeed * movementBoost;
_movement *= movementSpeed;
Debug.Log(movementSpeed);
}
}
private void Reset()
{
movementSpeed = resetBoost;
}
}
see https://docs.unity3d.com/ScriptReference/Input.GetButtonUp.html
for description and rules of Input.GetButtonUp()
if (Input.GetButtonUp("Fire3"))
{
Reset();
}