释放按钮后重置浮点值
Resetting the float value after a button is released
我正在尝试在我的游戏中实现车辆加速机制,当用户在移动中按住 shift 键时,车辆将加速。这方面工作正常!
但我的问题是当按钮被释放并再次按下时它会记住提升值然后乘以它。相反,我希望它在释放时重置为默认值,并且仅在按下按钮时增加。
这是我尝试过的方法:
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;
}
}
见https://docs.unity3d.com/ScriptReference/Input.GetButtonUp.html
Input.GetButtonUp()
的描述和规则
if (Input.GetButtonUp("Fire3"))
{
Reset();
}
我正在尝试在我的游戏中实现车辆加速机制,当用户在移动中按住 shift 键时,车辆将加速。这方面工作正常! 但我的问题是当按钮被释放并再次按下时它会记住提升值然后乘以它。相反,我希望它在释放时重置为默认值,并且仅在按下按钮时增加。
这是我尝试过的方法:
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;
}
}
见https://docs.unity3d.com/ScriptReference/Input.GetButtonUp.html
Input.GetButtonUp()
if (Input.GetButtonUp("Fire3"))
{
Reset();
}