【问题标题】:Resetting the float value after a button is released释放按钮后重置浮点值
【发布时间】:2020-05-16 10:31:47
【问题描述】:

我正在尝试在我的游戏中实现车辆加速机制,当用户在移动中按住 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;
}


}

【问题讨论】:

    标签: c# unity3d debugging game-engine


    【解决方案1】:

    https://docs.unity3d.com/ScriptReference/Input.GetButtonUp.html Input.GetButtonUp()的描述和规则

    if (Input.GetButtonUp("Fire3"))
    {
        Reset();
    }
    

    【讨论】:

    • 非常感谢!没想到这么简单的解决方法
    猜你喜欢
    • 2020-10-26
    • 1970-01-01
    • 1970-01-01
    • 2015-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-15
    • 1970-01-01
    相关资源
    最近更新 更多