移動するオブジェクトがRigidbody
であるとします。このオブジェクトには、forceがRigidbody.AddForce
またはRigidbody.velocity
によって追加されます。オブジェクトは別のオブジェクトに命中して方向を変えることができます。リジッドボディオブジェクトの位置をx秒で予測する
は、私は約Extrapolationを知っているが、この場合には、そのオブジェクトが他のオブジェクトをヒットし、その過程での速度/方向を変えることができるので、X秒でオブジェクトの位置を取得するために、いくつかの式を使用することはほぼ不可能です。
Unity 2017は、この問題を解決するためにPhysics.autoSimulation
とPhysics.Simulate
を導入しました。 2D物理学の場合、それはPhysics2D.autoSimulation
とPhysics2D.Simulate
です。最初にPhysics.autoSimulation
をfalseに設定してからPhysics.Simulate
関数を呼び出すだけでした。私の例では
、私はRigidbody
がそれに力を追加した後4
秒になる場所を知りたいと思った、1
のような小さな秒間正常に動作するようです。問題は、5
以上のような大きな数値をSimulate
関数に渡すと、予測される位置はではなく、であることが問題です。それは道のりです。
なぜこのようなことが起こり、どうすれば解決できますか?この問題は、Android搭載端末では悪化しています。
私の現在のUnityバージョンは、Unity 2017.2.0b5です。
以下は、私が使用しているサンプルコードです。 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;
}
ありがとう:
は、次のようになりますあなたの新しい完全な
predictRigidBodyPosInTime
機能をを交換してください。これはうまくいった。 – Programmer