2017-06-30 7 views
0

私はキューブを(0,2,0)上に持っていて、それをy軸上で2から1に移動してから3まで戻って1から3に戻るようにします。 ..ユニティは2つの座標間で振動する

Mathf.PingPong()を使用するときにパラメータとして渡すものは誰かが説明できますか?

I有し

public virtual void PingPongCollectable(Transform transform, float speed) 
{ 
    Vector3 pos = transform.position; // current position 
    pos.y = Mathf.PingPong(? , ?); // missing parameters, calculate new position on y 
    transform.position = pos; // new position 
} 

ので、ここでIは、速度および座標(上記)及びB(下記)に渡す必要がありますか?

キューブはループ内をスムーズに上下にスライドします。

ありがとうございます!

答えて

0

Mathf.PingPongメソッドのシグネチャは、次のいずれか:

public static float PingPong(float t, float length); 

PingPongs値t、それは長さよりも決して大きく、決してより小さい0
が返されないように値は0と長さの間で前後に移動します。

円滑な移行を行う場合は、最初のパラメータはTime.timeにする必要があります。
2番目のパラメータは最大値です。この方法では、0とその最大値の間の位置が得られます。
次に、ピンポン方式のボトムフロアと長さを設定できます。

public virtual void PingPongCollectable(Transform transform, float speed) 
{ 
    Vector3 pos = transform.position; 
    float length = 1.0f; // Desired length of the ping-pong 
    float bottomFloor = 1.5f; // The low position of the ping-pong 
    pos.y = Mathf.PingPong(Time.time, length) + bottomFloor; 
    transform.position = pos; // new position 
} 

出典:

は、ここでは、提供されたコードの使用例で使用してhttps://docs.unity3d.com/ScriptReference/Mathf.PingPong.html

+0

を 'Mathf.PingPong(Time.deltaTime、長さ - bottomFloor)+ bottomFloor'が正しくありません。立方体は即座に0にジャンプし、3に移動して0に戻りますが、1.5fから2.5fに移動する必要があります。 – Question3r

+0

私の例を編集しました。もし1.5fと2.5fとの間で動かしたいなら、これを行うことができます: 'Mathf.PingPong(Time.deltaTime、1.0f)+ 1.5f' – CBinet

+0

あなたは' Time.time'を使う必要があります。 docs、またはメンバー変数に時間を累積します。 'Time.deltaTime'は一般に時間の経過とともに増加しないので、フレームの後に同じ位置フレームを取得します。 – BMac

関連する問題