【问题标题】:How to ease-in y velocity of a game object in Unity?如何在 Unity 中缓和游戏对象的 y 速度?
【发布时间】:2020-05-30 16:44:41
【问题描述】:

我目前有一个游戏对象,在按下按钮时,我在y 轴上添加速度,所以角色会跳起来......这可以按预期工作。

public float jumpForce

private void Jump()
{
    playerRigidbody.velocity = new Vector2(playerRigidbody.velocity.x, jumpForce);
    AudioManagerController.instance.PlaySfx(Util.AudioEffect.PlayerJump);
}

但是,这是非常线性的,玩家以恒定的速度跳跃。我想改变它,让玩家开始缓慢地跳起来,逐渐(指数?快速?不确定这里的正确词是什么)按下按钮的时间越长越快。

有点像缓入:

我尝试在update() 中将速度从0 提高到jumpForce,类似于:

jumpModifier = time.deltaTime; // In start()
...
jumpModifier = jumpModifier + time.deltaTime; // in update() 

我已经尝试了其中的一些变体,但它并没有完全达到我想要的效果。

请问如何让我的角色以一种轻松的方式跳起来?

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    EaseIn 曲线是 Cos+Lerp 曲线,或“Coserp”。

    //Ease in
    public static float Coserp(float start, float end, float value)
    {
        return Mathf.Lerp(start, end, 1.0f - Mathf.Cos(value * Mathf.PI * 0.5f));
    }
    
    public static Vector2 Coserp(Vector2 start, Vector2 end, float value)
    {
        return new Vector2(Coserp(start.x,end.x,value),Coserp(start.y,end.y,value));
    }
    
    public static Vector3 Coserp(Vector3 start, Vector3 end, float value)
    {
        return new Vector3(Coserp(start.x, end.x, value), Coserp(start.y, end.y, value), Coserp(start.z, end.z, value));
    }
    

    【讨论】:

    • 如果我知道 end 向量,这将有效 - 在我的情况下,我不知道!一开始我需要能够(仅)轻轻地增加velocity.y 的值,并且随着时间的推移很快......但我会尝试根据我的需要采用它,看看它是否有效
    • 好的,我正要试一试并阅读代码 - 我可以告诉这个代码即使不尝试它也不会工作,原因有两个:1)内部递归函数正在传递浮点值到主函数,而主函数采用向量 (2)(我不太确定这个,但是......)这是一个没有退出条件的递归函数!不过,我会感谢你的总体思路,我会查看 cos + lerp 曲线,看看我能做什么。谢谢。
    • @LocustHorde 我已经更新了答案,已经很晚了,我忘记了答案 *facepalm 的主要代码。立即查看。
    猜你喜欢
    • 2016-11-03
    • 2013-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多