团结-通过抵消重力来创造悬停车

问题描述 投票:0回答:3

我希望实现一种创建副词的方法。我当前要悬停的代码是

readonly float yForce = 80;
Physics.Raycast(hoverbike.transform.position, Vector3.down, out hit);
Debug.Log(hit.distance);
if (hit.distance < 10 && hit.distance > 0)
{
   if (hoverbike.velocity.y < 0.1)
    {
        hoverbike.AddForce(0, yForce, 0, ForceMode.Acceleration);
        Debug.Log("applying force!");
    }
}

此方法可行,但效果不佳,车辆会上下弹起。我还尝试减去与自行车y velocity完全相同的力,但车辆缓慢向下漂移,没有达到我希望的离地面10个单位的高度。我该如何实现?简单地抵消它的当前速度很容易,但是如何使它浮回up到所需的高度?

c# unity3d
3个回答
1
投票

悬停基本上是视觉效果

使对撞机在车辆下方延伸,以便将其放在地面上时,自行车出现将其悬停在所需的高度。物理引擎仅执行物理。它并不关心这些对撞机是什么,它只是希望它们以物理上的方式表现,如果这意味着跌倒直到跌落到地面,那么让他们跌落直到跌落到地面。利用物理引擎的优势,而不是四处走动,然后尝试解决由于绕过物理引擎而产生的错误。


0
投票

简单地关闭引力要比不断与引力作斗争要容易得多;这种频繁的调整很可能是您弹跳的原因。激活自行车后,您可以完全将对象的y velocity计算完全掌握在手中:

public class Bike : MonoBehaviour
{
    private Rigidbody hoverbike;
    private bool isBikeActive = false;

    [SerializeField] private float verticalSpeedMultiplier = 1f;
    [SerializeField] private float hoverHeight = 10f;
    [SerializeField] private float hoverTolerance = 0.5f;

    private void Awake()
    {
        hoverbike = GetComponent<Rigidbody>();
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space)) { ToggleBike(); }

        if (isBikeActive)
        {
            Physics.Raycast(hoverbike.transform.position, Vector3.down, out RaycastHit hit);

            Vector3 modifiedVelocity = hoverbike.velocity;

            if ((hit.distance > hoverHeight - hoverTolerance) && (hit.distance < hoverHeight + hoverTolerance))
            {
                modifiedVelocity.y = 0f;
            }
            else
            {
                modifiedVelocity.y = -(hit.distance - hoverHeight) * verticalSpeedMultiplier;
            }

            Debug.Log($"Distance from ground: {hit.distance}, Bike Velocity.y: {modifiedVelocity}");
            hoverbike.velocity = modifiedVelocity;
        }
    }

    private void ToggleBike()
    {
        isBikeActive = !isBikeActive;
        hoverbike.useGravity = !isBikeActive;
    }
}

您的自行车现在将始终尝试朝其下方对象上方的hoverHeight单位移动,直到其位于该点hoverTolerance以内。它也将朝着这一点更平稳地移动,离目标高度越远,移动速度就越快。

如果您希望自行车仍能上下摆动,可以通过随时间慢慢修改hoverHeight来实现,也许可以使用正弦函数。


0
投票

[首先,以向下的速度缩放您的阻尼力,然后施加一个额外的力,该额外的缩放力取决于它需要向上移动多远。跟踪您在此过程中施加的力/加速度,并将该值限制为恒定。

readonly float yForce = 80f; // requires tuning
readonly float dampenFactor = 0.8f; // requires tuning
readonly float offsetFactor = 0.5f; // requires tuning

readonly float targetHeight = 10f

Physics.Raycast(hoverbike.transform.position, Vector3.down, out hit);
Debug.Log(hit.distance);
if (hit.distance < targetHeight && hit.distance > 0)
{
    float availableForce = yForce;

    // cancel out downward velocity
    if (hoverbike.velocity.y < 0)
    {
        // Cap out upward force based on yForce
        float cappedDampenForce = Mathf.Min(dampenFactor * -hoverbike.velocity.y,
                availableForce);

        // How much force is available for the offset?
        availableForce -= cappedDampenForce;

        hoverbike.AddForce(Vector3.up * cappedDampenForce, ForceMode.Acceleration);
        Debug.Log("applied dampening force");
    }

    // Find upward force scaled by distance left to target height, and cap that amount
    float cappedOffsetForce = Mathf.Min(offsetFactor * (targetHeight - hit.distance), 
            availableForce);

    hoverbike.AddForce(Vector3.up * cappedOffsetForce, ForceMode.Acceleration);
    Debug.Log("applied offset force");
}
© www.soinside.com 2019 - 2024. All rights reserved.