- Home /
Rigidbody velocity updates on wrong collision Object,[2D] Rigidbody velocity updates on wrong collision object,
Making a Pong clone; the Rb velocity of the ball should update on a powerup "FastBall", which increases velocity. Instead it only updates when it hits a player Stick. It registers a collision but updates on the wrong object
void OnCollisionEnter2D(Collision2D col) {
print(col.gameObject.name);
if (col.gameObject.name == "Player1")
{
float y = hitObject(transform.position, col.transform.position, col.collider.bounds.size.y);
Vector2 dir = new Vector2(1, y);
GetComponent<Rigidbody2D>().velocity = dir * ballSpeed;
}
if (col.gameObject.name == "Player2")
{
float y = hitObject(transform.position, col.transform.position, col.collider.bounds.size.y);
Vector2 dir = new Vector2(-1, y);
GetComponent<Rigidbody2D>().velocity = dir * ballSpeed;
}
}
Here the FastBall script(Debug registers).
void OnTriggerEnter2D(Collider2D other) { Debug.Log("Collision"); if (gameObject.name == "FastBall") { ballSpeed.ballSpeed *= 4; } },Making a Pong clone with some extras; If the ball goes through the powerup field "FastBall" the velocity should be increased. But the velocity only updates when it touches a player stick. How does it trigger when it enters the powerup?
Here the Script for "FastBall". The Debug.Log works
void OnTriggerEnter2D(Collider2D other) { if (gameObject.name == "FastBall") { ballSpeed.ballSpeed *= 4; } } ,