Search Unity

Question Prevent Weapon Clipping Walls

Discussion in 'Physics' started by Feral_Pug, Sep 12, 2021.

  1. Feral_Pug

    Feral_Pug

    Joined:
    Sep 29, 2019
    Posts:
    49
    SwordClipScreenShot.PNG

    I do not know if this is the right place to ask this but I was wondering what are the standard ways of preventing clipping like the above example. I have seen lots of posts about preventing this with fps cameras, where you just render the weapon after the geometry and overlaying the weapon, but I don't want to render the weapon over the player.

    Solutions I can think of are:
    1) use a rigid body on the weapon and just use the physics engine. This seems like the worst solution because the weapon can go flying away on a collision. I suppose you could just set its velocity to zero in collision callbacks.

    2) Expand the collider on the player to cover the weapon. For this to work I feel like you would have to constantly be resizing the collider which could get difficult to deal with.

    3) Expand the colliders of the walls so that you can't get close enough.

    4) Maybe something with raycasts, where you raycast from the player to the tip of the weapon and stop movement if you would hit a wall.

    I feel like they all come with some baggage which is why I am asking what the community thinks.
     
  2. r31o

    r31o

    Joined:
    Jul 29, 2021
    Posts:
    460
    Firt solution is simmilar to what they do in Fortnite or PUBG (in third person camera) if you look tho the character, the gun is facing forwards, but when the character gets close to a wall the gun rotates so is not that noticeable that the gun is stuck in the wall (Still a bit stucked, but is very unnoticeable) you could try something like that. A racast of, something like 1 meter, if it collides, the charcter will change the weapon position so is not that obvious that the weapon is inside the wall. Or make the collider thicker.
     
  3. QtTx

    QtTx

    Joined:
    Aug 20, 2021
    Posts:
    16
    Larger capsule (or thicker walls) might work for a more capsule-like model, but I doubt it will work well with a sword sticking out this much.
    For high-tempo gameplay I think it might be better to just ignore this issue altogether. And maybe look for some sort of artistic solution. As an example, notice how Dante in most DMC games holds his (unrealistically large) sword on his back most of the time, and only takes it out when he performs an attack.
    For slower games you can try to change the position of the sword: check whether it collides with a static object (using trigger collisions or overlap/raycast queries), and then use appropriate animation clip or IK to move the sword away from the wall (essentially a character would lower his weapon when coming too close to the obstacle).
    Rigidbody-based solutions might work in games with silly physics based combat (popular in VR games), but it will probably look bad in more serious settings.
     
    Last edited: Sep 14, 2021
  4. Feral_Pug

    Feral_Pug

    Joined:
    Sep 29, 2019
    Posts:
    49
    Yeah, I was thinking of just making it so the weapon is holstered most of the time, resolving the issue, or just on a collision with the wall and the sword have the player put the weapon away.

    If doing a rigid body solution would you have to put the drag and friction values really high so that the weapon doesn't fly away out of the players hands?
     
  5. DejaMooGames

    DejaMooGames

    Joined:
    Apr 1, 2019
    Posts:
    108
    Personally, I would just let the weapon clip through the wall. Yeah it looks a little funky, but that weapon is HUGE and you are going to dramatically restrict your ability to design levels if you don't let it just clip through the wall. Think about how large your smallest hallway would need to be in order to accommodate that weapon. Tight corners become awkward too. A lot of AAA games take this approach. The only gameplay consideration is that you will want to bounce the weapon if it collides with a wall during a swing.
     
  6. Feral_Pug

    Feral_Pug

    Joined:
    Sep 29, 2019
    Posts:
    49
    Yeah that is a very good point. I guess i didn't list the option of just ignoring it. I was also just kind of curious if there was some simple solution that i was unaware of.
     
  7. r31o

    r31o

    Joined:
    Jul 29, 2021
    Posts:
    460
    Another solution: In a fps game you render the weapon in front, but you dont like it since (I guess) it will look strange the weapon in front of the player. So, my solution is:
    -If you dont have the cammera collision for tps already, make one. Just some raycast.
    -Render both the player model and the weapon in front.
    With this, the weapon will not clip into walls, and it wont look weird.
    Idk how to explain this better, but trust me, this will work.
    For the rendering in front just use the same two camera trick: One rendering the map and other rendering the player on top.
    There are several camera collision tutorials for tps games in youtube.
     
    Oxcaibi likes this.
  8. Feral_Pug

    Feral_Pug

    Joined:
    Sep 29, 2019
    Posts:
    49
    Yeah, i guess i didn't think of doing the FPS trick on the entire player, model and weapon. I was only thinking of doing it for the weapon, which would look bad when the player is faced away from the camera.
     
  9. QtTx

    QtTx

    Joined:
    Aug 20, 2021
    Posts:
    16
    It works in fps games, because the angle of the camera is fixed behind the weapon. This creates an illusion that weapon never actually reaches the wall (unless you specifically pay attention to it). It will be really hard to recreate the same illusion for arbitrary camera angles.
     
  10. r31o

    r31o

    Joined:
    Jul 29, 2021
    Posts:
    460
    Thats why I said to add a camera collision.
    If you add camera collision you will always see the player, so you wont see anithing wrong.
    Btw, isin't that the Ybot from mixamo?
     
  11. QtTx

    QtTx

    Joined:
    Aug 20, 2021
    Posts:
    16
    The image in the first post will look wrong with or without camera collision. Let alone other situations, where a character stands behind a low obstacle, such as a table or small stone (unless you suggest camera should collide with those as well)/
     
  12. r31o

    r31o

    Joined:
    Jul 29, 2021
    Posts:
    460
    I dont think that the image will look wrong
     
  13. TSRajesh

    TSRajesh

    Joined:
    Jun 19, 2013
    Posts:
    69
    probably this thread is not relevant anymore.This is just for those who reach here via search..

    I have a solution that works to my satisfaction..



    It is to identify how much it has clipped, and apply corrective displacement in the next frame.

    The full procedure will be covered in my tutorial of "Third person controller from the scratch"