Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Giving punch back to object on collision

Discussion in '2D' started by MatoXD, Oct 10, 2018.

  1. MatoXD

    MatoXD

    Joined:
    Feb 26, 2017
    Posts:
    15
    When the bullet touches the shield, I need it to just kinnda punch back, so it gets of the way, right now I have settted that bullet has 0 gravity and on collision gain it so it gets physics effect, but what I really need with it is getting puncture, so it looks more realistic, if anyone knows how... Here is my onCollision code:

    Code (CSharp):
    1.  
    2. private void PunchBackTheBullet(Collision2D collision) {
    3.         Vector2 PositionOfBullet = new Vector2(transform.position.x, transform.position.y);
    4.  
    5.         Vector2 dir = collision.GetContact(0).point - PositionOfBullet;
    6.         dir = -dir.normalized;
    7.         GetComponent<Rigidbody>().AddForce(dir * ForceOfPushback);
    8.     }
    9.  
    Thanks :)