我正在尝试在我的游戏中实现一种车辆助力机制,即当用户按住移动时,车辆将加速。那方面很好!但是我的问题是,释放并再次按下按钮时,它会记住升压值,然后将其相乘。相反,我希望它在释放时重置为默认值,并且仅在按下按钮时才增加。
这是我尝试过的:
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();
}