代码之家  ›  专栏  ›  技术社区  ›  David Meléndez

统一同高度弹跳球及落球速度控制

  •  2
  • David Meléndez  · 技术社区  · 6 年前

    如何在统一体中创建一个反弹的球,它反弹到相同的高度,并且我可以使它落得更快或更慢?有人告诉我要用:

    heightVector * |sin(time * speed)|

    但我不知道把它插在哪里。我真的不懂这些东西。救命啊!

    1 回复  |  直到 6 年前
        1
  •  4
  •   PNarimani    6 年前

    你的公式是正确的。
    heightVector 是球的最大高度。例如,如果 (0,10) ,那么就意味着你的球会飞到10米高。
    这个 time speed 是你的球的速度。

    但是,我建议速度乘以 Time.deltaTime 使反弹帧速率独立。


    高度向量 没有复杂性。只需创建两个公共字段,就可以了!

    class Bouncer : MonoBehaviour
    {
        public float Speed = 10;
        public Vector2 HeightVector = new Vector2(0,10);
    }
    

    float 变量。那你需要加上 在每一天 Update

    class Bouncer : MonoBehaviour
    {
        public float Speed = 10;
        public Vector2 HeightVector = new Vector2(0,10);
    
        float timer;
        void Update()
        {
             timer += Time.deltaTime;
        }
    }
    

    恭喜!你现在有计时器了!

    现在你真的快结束了。您只需计算球的当前位置并将其应用于其变换。

    class Bouncer : MonoBehaviour
    {
        public float Speed = 10;
        public Vector3 HeightVector = new Vector3(0,10);
    
        float timer;
        void Update()
        {
             timer += Time.deltaTime;
    
             Vector3 currentPosition = HeightVector * Mathf.Abs(timer * Speed * Time.deltaTime);
    
             transform.position = currentPosition;
        }
    }
    

    编辑:

    class Bouncer : MonoBehaviour
    {
        public float Speed = 10;
        public Vector3 HeightVector = new Vector3(0,10);
        Vector3 originalPosition;
        float timer;
    
        void Start()
        {
            originalPosition = transform.position;
        }
    
        void Update()
        {
             timer += Time.deltaTime;
    
             Vector3 currentPosition = HeightVector * Mathf.Abs(timer * Speed * Time.deltaTime);
    
             transform.position = originalPosition + currentPosition;
        }
    }
    
    推荐文章