【发布时间】: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