これらの手順は、Update
またはFixedUpdate
メソッドで呼び出されるメソッドで実行します。剛体を使用する場合はFixedUpdate
をお勧めします。
まず、自分の位置からポイントまでの方向を見つけ、リジッドボディを使用しない場合はスクリプト内にvelocity
インスタンス変数を定義する必要があります。 にを使用してを使用している場合は、代わりにrigidbody.velocity
を使用してください。 target
は、あなたが加速しようとしているVector3
ポジションです。
// Use rigidbody.velocity instead of velocity if using a Rigidbody
private Vector3 velocity; // Only if you are NOT using a RigidBody
Vector3 direction = (target - transform.position).normalized;
次に、既にターゲットを通過したかどうかを確認する必要があります。このチェックは、我々はたちのTransform
かRigidbody
を加速する必要があり、これを行っているいったん速度が同じ
// If our velocity and the direction point in different directions
// we have already passed the target, return
if(Vector3.Dot(velocity, direction) < 0)
return;
残っていることを確認します。
// If you do NOT use rigidbodies
// Perform Euler integration
velocity += (accelMagnitude * direction) * Time.deltaTime;
transform.position += velocity * Time.deltaTime;
// If you DO use rigidbodies
// Simply add a force to the rigidbody
// We scale the acceleration by the mass to cancel it out
rigidbody.AddForce(rigidbody.mass * (accelMagnitude * direction));
私はこのような何かをやったときに、それははるかに理にかなっているので、あなたがRigidbody
を使用することをお勧めします。
申し訳ありませんが、その位置で停止しています。スクリプトの第3部分はどこに行くべきですか?あなたの答えの2番目の部分のif文の中に? @AlbertoO。 –
if文の後に*する必要があります。 – EvilTak