嘿,伙计们,我在这里有一些事情要问一下,统一的速度到底是如何工作的???我最近一直在做一个项目,我想创建弹跳球游戏,所以每当球击中对撞机时,它会被弹跳取决于被击中的位置。
我正在使用 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;
}
}