首页 > 解决方案 > Unity3d 新的输入系统逐步增加/减少值,而不是立即从 0 到 1 或 -1

问题描述

我使用 1D 轴进行运动,但值立即从 0 变为 1 或 -1 我需要逐渐增加和减少值,就像 Input.GetAxis() 而不是 Input.GetAxisRaw();在此处输入图像描述

现在我正在阅读这样的输入

float value = controlSystem.DroneInput.Throttle.ReadValue<float>();
drone.AddRelativeForce(0, value * throttle * 100, 0, ForceMode.Force);

标签: c#unity3d

解决方案


我会用Mathf.SmoothDamp这样的简单缓动:

(从上面链接的文档页面采用的代码)

using UnityEngine;

public class Example : MonoBehaviour
{
    float smoothTime = 0.3f;
    float smoothingV = 0.0f;
    float currentValue = 0.0f;

    void Update()
    {
        float target = controlSystem.DroneInput.Throttle.ReadValue<float>();
        currentValue = Mathf.SmoothDamp(currentValue, target, ref smoothingV, smoothTime);

        drone.AddRelativeForce(0, newValue * throttle * 100, 0, ForceMode.Force);
    }
}

推荐阅读