【问题标题】:How does velocity works?速度是如何工作的?
【发布时间】:2015-07-20 14:11:35
【问题描述】:

嘿,伙计们,我有一些问题要问一下,统一的速度到底是如何工作的?我最近一直在做一个项目,我想创建弹跳球游戏,所以每当球击中对撞机时,它会被弹跳取决于被击中的位置。

我正在使用 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;
    } 

}

【问题讨论】:

    标签: unity3d bounce gameobject rigid-bodies


    【解决方案1】:

    您应该创建一个具有高弹性和低摩擦力的物理材料 2D,并将其应用到您的 collider2D(您应该看到这些值,直到您感到满意为止。

    您在 Assets->Create->Physics2D Material 中创建它们。

    你正在尝试做的是模拟物理,它需要一个有点复杂的数学模型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-04
      • 2011-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-25
      • 2014-12-05
      • 1970-01-01
      相关资源
      最近更新 更多