【发布时间】:2017-08-03 12:55:51
【问题描述】:
假设您有一个移动的Rigidbody 对象。通过Rigidbody.AddForce 或Rigidbody.velocity 将力添加到此对象。该物体可以滚动撞击另一个物体并改变方向。
我知道Extrapolation,但在这种情况下,几乎不可能使用某些公式在 x 秒内获得对象的位置,因为对象可以撞击另一个对象并改变速度/过程中的方向。
Unity 2017 引入了Physics.autoSimulation 和Physics.Simulate 来解决这个问题。对于二维物理,即Physics2D.autoSimulation 和Physics2D.Simulate。我所做的只是首先将Physics.autoSimulation 设置为false,然后调用Physics.Simulate 函数。
在我的示例中,我想知道Rigidbody 在对其施加力后在4 秒内的位置,它似乎在像1 这样的小几秒内工作正常。问题是,当我将较大的数字(如 5 及以上)传递给 Simulate 函数时,预测的位置不准确。差远了
为什么会发生这种情况,我该如何解决?这个问题在 Android 设备上更为严重。
我当前的 Unity 版本是 Unity 2017.2.0b5。
以下是我正在使用的示例代码。 guide GameObject 仅用于显示/显示预测位置的位置。
public GameObject bulletPrefab;
public float forceSpeed = 50;
public GameObject guide;
// Use this for initialization
IEnumerator Start()
{
//Disable Physics AutoSimulation
Physics.autoSimulation = false;
//Wait for game to start in the editor before moving on(NOT NECESSARY)
yield return new WaitForSeconds(1);
//Instantiate Bullet
GameObject obj = Instantiate(bulletPrefab);
Rigidbody bulletRigidbody = obj.GetComponent<Rigidbody>();
//Calcuate force speed. (Shoot towards the x + axis)
Vector3 tempForce = bulletRigidbody.transform.right;
tempForce.y += 0.4f;
Vector3 force = tempForce * forceSpeed;
//Addforce to the Bullet
bulletRigidbody.AddForce(force, ForceMode.Impulse);
//yield break;
//Predict where the Rigidbody will be in 4 seconds
Vector3 futurePos = predictRigidBodyPosInTime(bulletRigidbody, 4f);//1.3f
//Show us where that would be
guide.transform.position = futurePos;
}
Vector3 predictRigidBodyPosInTime(Rigidbody sourceRigidbody, float timeInSec)
{
//Get current Position
Vector3 defaultPos = sourceRigidbody.position;
Debug.Log("Predicting Future Pos from::: x " + defaultPos.x + " y:"
+ defaultPos.y + " z:" + defaultPos.z);
//Simulate where it will be in x seconds
Physics.Simulate(timeInSec);
//Get future position
Vector3 futurePos = sourceRigidbody.position;
Debug.Log("DONE Predicting Future Pos::: x " + futurePos.x + " y:"
+ futurePos.y + " z:" + futurePos.z);
//Re-enable Physics AutoSimulation and Reset position
Physics.autoSimulation = true;
sourceRigidbody.velocity = Vector3.zero;
sourceRigidbody.useGravity = false;
sourceRigidbody.position = defaultPos;
return futurePos;
}
【问题讨论】:
标签: c# unity3d rigid-bodies