【问题标题】:Top-down space flight physics - AddForce - Object moves off center the faster it goes自上而下的太空飞行物理 - AddForce - 物体越快越远离中心
【发布时间】:2017-03-15 16:22:32
【问题描述】:

我刚刚开始了一个小项目,我希望玩家从自上而下的角度控制一艘船。我希望整个事情都使用基于物理的运动。

我的问题是,当我向刚体施加力时,它所附着的物体似乎越远离它的位置,它移动得越快。但是,transform.positions 似乎没问题。

我整理了this little unity package。如果你按 W,你按 W 的时间越长,船就会向上移动并偏离中心。S 会朝另一个方向移动。

代码比较简单:

using UnityEngine;
using System.Collections;

public class TestController : MonoBehaviour {
  public float mainThrust = 100;
  private Vector3 cameraOffset = new Vector3(0f, 0f, 150f);
  private new Rigidbody rigidbody;

  void Start () {
    rigidbody = GetComponent<Rigidbody>();
  }

  void FixedUpdate() {
    float axisVertical = Input.GetAxis("Vertical");
    Vector2 force = new Vector2(0f, axisVertical) * mainThrust * Time.fixedDeltaTime;
    rigidbody.AddRelativeForce(force, ForceMode.Impulse);
    Camera.main.transform.position = rigidbody.transform.position - cameraOffset;
    Debug.Log("camPos: " + Camera.main.transform.position
      + " - rbPos: " + rigidbody.transform.position);
  }
}

任何帮助表示赞赏。

谢谢, 妮可

【问题讨论】:

  • 如果你能提供截图/视频会有所帮助,因为我不能 100% 确定你的问题是什么。但是,我怀疑由于脚本的执行顺序,您的相机只是落后于您的游戏对象,但我可能是错的。
  • 顺便说一句,你可以让相机成为船的孩子,它会自动跟随它。
  • Video 来了。相机似乎不是问题。将相机附加到对象会导致相同的行为。

标签: c# unity3d game-physics


【解决方案1】:

只需将您的 Camera.main.transform.position = rigidbody.transform.position - cameraOffset; 行放入 LateUpdate MonoBehaviour 方法中,我认为您会很好(如果我正确理解了问题)。如果您需要进一步解释,可以寻找this

编辑:

这是我用来获得所需结果的脚本(基于在您的包中找到的那个):

using UnityEngine;
using System.Collections;

public class TestController : MonoBehaviour
{
    public float mainThrust = 100;
    private Vector3 cameraOffset = new Vector3(0f, 0f, 150f);
    private new Rigidbody rigidbody;

    private Vector3 tmpV1 = new Vector3(0f, 0f, 0f);
    private Vector3 tmpV2 = new Vector3(0f, 0f, 0f);

    // Draw debug cross at camera position
    private void drawX()
    {
        tmpV1.x = Camera.main.transform.position.x - 10f;
        tmpV1.y = Camera.main.transform.position.y;
        tmpV2.x = Camera.main.transform.position.x + 10f;
        tmpV2.y = Camera.main.transform.position.y;
        Debug.DrawLine(tmpV1, tmpV2, Color.red);
        tmpV1.x = Camera.main.transform.position.x;
        tmpV1.y = Camera.main.transform.position.y - 10f;
        tmpV2.x = Camera.main.transform.position.x;
        tmpV2.y = Camera.main.transform.position.y + 10f;
        Debug.DrawLine(tmpV1, tmpV2, Color.red);
    }

    void Start()
    {
        rigidbody = GetComponent<Rigidbody>();
    }

    void FixedUpdate()
    {
        float axisVertical = Input.GetAxis("Vertical");
        // impulse force in Newton
        Vector2 force = new Vector2(0f, axisVertical) * mainThrust * Time.fixedDeltaTime;
        rigidbody.AddRelativeForce(force, ForceMode.Impulse);
    }

    void LateUpdate()
    {
        Camera.main.transform.position = rigidbody.transform.position - cameraOffset;
        drawX();
        Debug.Log("camPos: " + Camera.main.transform.position + " - rbPos: " + rigidbody.transform.position);
    }
}

【讨论】:

  • 抱歉,没用。 :(
  • @NicoBüttner 好吧,我只是用一个空项目和你提供的包试了一下,它运行良好......你确定你没有在LateUpdate() 上打错字吗?您是否删除了复制到 LateUpdate 的行?
  • 您确定它对您有用吗?我现在在两个系统上进行了测试,行为是相同的。为了澄清事情,我将视频 (youtu.be/12z526xrUpM) 上传到 youtube。我希望立方体保持在红十字的屏幕中间。再看看控制台:Camera.main.position 和rigidbody.position 是一样的……
  • 我刚刚再次测试,它运行良好:你可以看到它here(重 .gif 文件)。我编辑了我的答案,为您提供了我用来实现此目的的代码。
  • 非常抱歉!我仍然在 FixedUpdate 方法中画了十字,所以它看起来是相同的行为,但实际上十字落在了相机的正确位置后面。实际上,我什至不必将其放入 LateUpdate 方法中,而只需使用 Update 方法即可。那么当我在 FixedUpdate 方法中对对象施加力时,对象的实际定位在 FixedUpdate 和 Update 之间的某处解决是否正确?
猜你喜欢
  • 2022-01-14
  • 1970-01-01
  • 1970-01-01
  • 2014-12-06
  • 2021-01-23
  • 1970-01-01
  • 1970-01-01
  • 2018-09-17
  • 1970-01-01
相关资源
最近更新 更多