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;
}
}