hey les gars j'ai quelque chose ici pour demander comment fonctionne vraiment la vitesse sur l'unité? J'ai travaillé sur un projet récemment, je veux créer les jeux de balle rebondissant, donc chaque fois que la balle frappe le collisionneur, elle sera rebondie dépend de la position qui a été touchée. J'utilise la vitesse getComponent(). Mais en quelque sorte la balle ne rebondit pas vraiment bien, et chaque fois que la balle atteint le milieu du collisionneur, elle doit être rebondie sans changer de direction. Aidez s'il vous plaît! !! toute aide serait reconnaissant ... voici mon code:Comment fonctionne la vélocité?
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;
}
}