【发布时间】: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