Question by
KachibaMyNibba · Jul 06, 2018 at 04:20 PM ·
2dbounceshot
Bullet ricochet 2D not working
Hello, I've tried to get my projectile bullet to work for so long but I just couldn't make it work so now I'm asking for help. It just doesn't bounce at all or bounces in the complete wrong direction. Here's the code:
void Update () {
rb.angularVelocity = 0;
rb.velocity = transform.up * (P1BulletSpeed / 2);
}
void OnCollisionEnter2D(Collision2D collision) {
if (collision.gameObject.tag == "Tank") {
print ("b");
} else if (collision.gameObject.tag == "Wall" || collision.gameObject.tag == "DWall"){
Vector2 reflectedPosition = Vector2.Reflect(transform.up, collision.contacts[0].normal);
rb.velocity = (reflectedPosition).normalized * (P1BulletSpeed / 2);
Vector2 dir = rb.velocity;
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
rb.MoveRotation(angle);
}
}
Comment
Your answer