Search Unity

Question Cast for rotating rigidbody2d

Discussion in 'Physics' started by theGreatSlutze, Jul 23, 2021.

  1. theGreatSlutze

    theGreatSlutze

    Joined:
    Jan 7, 2013
    Posts:
    25
    Hey all, I’m trying to simulate swinging a sword around my player. My sword is a trigger with a rigidbody2D, and my enemies have continuous collision detection (which I understand doesn’t work with triggers).

    At low speeds this works but at high speeds sometimes my sword seems to pass through enemies. I’m at a loss for my options here - there’s no rigidbody2D equivalent of transform.rotateAround, and there’s no straightforward way to sweeptest that motion either. Is there something else I’m missing here?

    Thanks!
     
  2. Epsilon_Delta

    Epsilon_Delta

    Joined:
    Mar 14, 2018
    Posts:
    258
    Maybe others will have better ideas, but in the meantime, you can try use 3D physics instead of 2D physics. 3D rigidbody has a continuous speculative collision detection, which works well with rotating bodies. Also, from my experience, 3D physX engine is oftentimes faster than Box2D engine.
    Just keep in mind you cannot have interaction between 3D and 2D physics, so, depending on your situation, this can be a special case, when you use 3D physics and don't interact with anything else, or you will use 3D everywhere else, too.
     
  3. theGreatSlutze

    theGreatSlutze

    Joined:
    Jan 7, 2013
    Posts:
    25
    Doesnt continuous detection not work for triggers, regardless of 2D vs 3d?
     
  4. Epsilon_Delta

    Epsilon_Delta

    Joined:
    Mar 14, 2018
    Posts:
    258
    I don't know the answer for sure, but I tested it real quick and it seems indeed it does not work for triggers. You can try decrease the fixedDeltaTime to have more precision or do boxCast/checkBox when you expect a hit (like every frame in the last 1s of the sword hit animation) - this solution will still not account for extreme speeds where there is too much distance covered between 2 frames. Third solution might be to make your sword non-trigger collider and check for collisions.
     
  5. theGreatSlutze

    theGreatSlutze

    Joined:
    Jan 7, 2013
    Posts:
    25
    Thanks for the replies! I’m leaning towards making weapons not be triggers; I think as long as everything is kinematic it’ll function basically the same with collision instead of trigger detectors, plus then I can use collision to do things like cancel attacks that hit walls. Thanks!
     
    Epsilon_Delta likes this.