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

2D Tank game problem

Discussion in 'Physics' started by cr4zy, Jul 14, 2015.

  1. cr4zy

    cr4zy

    Joined:
    Jul 14, 2015
    Posts:
    10
    Hello :D
    I started to make a 2D tank game and got a problem with the physics of the tank. In the script I move the tank with rigidbody2d.velocity = (Vector2)transform.TransformDirection(Vector3.up) * speed;. When the tank hits a wall diagonally I want the tank to stop but instead the tank rotates... to explain better what I mean I got 2 images.

    problem1.jpg
    This is what I want to happen when I press the forward key...

    problem2.jpg
    This is what actually happens.

    I already tried to attach a 2D physics material to the collider of the tank and changed the values but it didn't work :(
    I hope that somebody can help me :)
    And sorry for my english^^.. I am from germany
     
  2. DerpMerf

    DerpMerf

    Joined:
    Jan 1, 2014
    Posts:
    33
    You could attempt to create an OnDetect() method that detects when you've hit the wall, and then based on that create a boolean. This boolean affects whether or not your velocity keeps on updating, and if you've hit the wall it immediately zeroes the velocity. I've not much experienced with colliders as of yet, so I'm not entirely sure if that is in fact possible, but I have faith in the creators of Unity that they've left colliders open-ended. If not, you could try creating your own system of colliders, but that seems like an extensive amount of work. Hopefully this helped!
     
    cr4zy likes this.
  3. cr4zy

    cr4zy

    Joined:
    Jul 14, 2015
    Posts:
    10
    Hey thank you man :D you really helped me.
     
  4. DerpMerf

    DerpMerf

    Joined:
    Jan 1, 2014
    Posts:
    33
    No problem! Tell me how it goes!