【问题标题】:Rolling a ball forward with AddForce depending on the track direction根据轨道方向使用 AddForce 向前滚动球
【发布时间】:2019-01-25 14:27:13
【问题描述】:

我有一个单独向前移动的球,玩家只需向左或向右转即可获得宝石,而不会从路上掉下来。

问题是我使用了一个名为 AddForce 的函数来推动球向前,但我不知道如何根据轨道的方向调整方向。例如,如果有左转,我希望我的球自行转身,以适应轨道,让相机跟在后面。

void FixedUpdate()
{
    float moveHorizontal = Input.GetAxis("Horizontal") * sideSpeed * rb.velocity.magnitude / acceleration;

    if (rb.velocity.magnitude <= speedLimit)
    {
        rb.AddForce(0.0f, 0.0f, acceleration); // add vertical force
    }
    rb.AddForce(moveHorizontal, 0.0f, 0.0f); // add horizontal force
}

Screenshot

【问题讨论】:

    标签: unity3d direction normals


    【解决方案1】:

    您可以尝试使用Rigidbody.AddRelativeForce(),在球的相对向前方向上增加力,所以无论球旋转什么,它总是向前。试试这样的:

    void FixedUpdate()
    {
        float moveHorizontal = Input.GetAxis("Horizontal") * sideSpeed * rb.velocity.magnitude / acceleration;
    
        if (rb.velocity.magnitude <= speedLimit)
        {
            rb.AddRelativeForce(0.0f, 0.0f, acceleration); // Add vertical force
        }
    
        rb.AddRelativeForce(moveHorizontal, 0.0f, 0.0f); // Add relative horizontal force
    }
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-10
      • 1970-01-01
      • 2018-11-23
      • 1970-01-01
      相关资源
      最近更新 更多