Question by
AbdulhamidB7 · May 15, 2020 at 10:34 PM ·
movementspeeddebugdefault
Resetting the float value after a button is released
Hi Guys, So 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;
Vector3 _movement;
private float movementBoost = 2f;
private float resetBoost = 10f;
void Start()
{
}
void Update()
{
HandleMovementInput();
Reset();
}
void HandleMovementInput()
{
float moveVertical = Input.GetAxis("Vertical");
_movement = new Vector3(0, 0, moveVertical);
transform.Translate(_movement * movementSpeed * Time.deltaTime, Space.World);
if (Input.GetButtonDown("Fire3"))
{
movementSpeed = movementSpeed * movementBoost;
_movement *= movementSpeed;
Debug.Log(movementSpeed);
}
}
private void Reset()
{
movementSpeed = resetBoost;
}
}
Comment