0

やあ、ユニティのベロシティが実際にどのように機能するのかについて質問したいことがあります。私は最近プロジェクトに取り組んでいます。弾むボールゲームを作成したいので、ボールがコライダーに当たるたびに、当たった位置に応じて跳ね返ります。

私は getComponent().velocity を使用していますが、どういうわけかボールがうまく跳ね返らず、ボールがコライダーの真ん中に当たるたびに、方向を変えずに跳ね返るはずです..助けてください!!! どんな助けにも感謝します...これが私のコードです:

float getBallPos(Vector2 ballPos, Vector2 boxPos, float boxWide ){

    return (ballPos.x - boxPos.x)/boxWide ; 
} ---> to get the bounce direction


void OnCollisionEnter2D (Collision2D other){
    isHit = true;


    if (other.gameObject.tag == "up") {
        float x = getBallPos (transform.position, other.transform.position, other.collider.bounds.size.x);

        Vector2 dir = new Vector2 (x, 1).normalized;
        Debug.Log ("normalized : " + dir);
        GetComponent<Rigidbody2D> ().velocity = dir * 5f;

    }else if (other.gameObject.tag == "down") {
        float x = getBallPos (transform.position, other.transform.position, other.collider.bounds.size.x);

        Vector2 dir = new Vector2 (x, -1).normalized;
        Debug.Log ("normalized : " + dir);
        GetComponent<Rigidbody2D> ().velocity = dir * 5f;
    } 

}
4

1 に答える 1

0

弾力性が高く摩擦が少ない Physics Material 2D を作成し、それを collider2D に適用する必要があります (慣れるまで値を確認する必要があります。

Assets->Create->Physics2D Material で作成します。

あなたがやろうとしているのは物理学をシミュレートすることであり、それにはやや複雑な数学的モデルが必要です。

于 2015-08-09T20:20:23.803 に答える