2015-07-20 64 views
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; 
    } 

} 

回答

0

你应该创建一个物理材质的2D高反弹力和低摩擦,并把它应用到你的collider2D(你应该看到的值,直到你感到舒服它。

您的资产 - 创建它们> Create-> Physics2D材料。

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