- Home /
Mecanim - Fast moving collider not colliding
I am trying to create a kind of simple 2D fighting game. I have a game object that represents a character which has a child object that represents the a melee weapon (currently a sword) that they are holding. The sword object has a Collider2D attached so that I can detect when the sword collides with another character/weapon.
I am trying to use mecanim to animate attacks but I have run into a problem; the sword often moves too fast and so passes straight through other colliders.
I attempted to add a rigidbody2D to the sword so that I could set 'Collision Detection' to 'Continuous' but this didn't solve the problem and also had some other negative side effects (the sword wouldn't always be in the correct position). I guess this makes sense as it is mecanim that is moving the sword rather than the physics engine.
I also attempted to add a separate child object which I moved the collider onto, I then tried to update the position of this new object to match the sword that is controlled by mecanim. This also had negative effects and the collider seemed to always lag behind the sword.
This feels like it should be a very common thing to need to do (animate a fast moving collider) for any kind of fighting game so I'm probably either missing something or going about this the wrong way entirely.
Any ideas how I could solve this problem?
Thanks
Answer by carrollh · Jan 24, 2015 at 05:42 PM
It really depends on what you're doing. Different colliders behave differently as they interact with other things. For instance, you might have noticed that in certain configurations your character will register collisions with walls, but not with moving enemies.
I know you can do the rigidbody thing with your weapons, as that's exactly what I do. But you have to update the local position and rotation of the weapon every frame because the rigidbody physics want to move it around. You don't need a rigidbody (you can use a regular collider instead) unless you want your weapon to push/bash other rigidbodies around.
Look here for a very good outline of when you should use the different things: http://answers.unity3d.com/questions/7671/guidelines-for-using-rigidbody-collider-characterc.html
I seem to have finally got it to a point where none of the collisions are missed by having a separate collider object with a rigid body and continuous collision detection. I update its position and rotation every frame as you suggest, however the collider always seems to lag slightly behind the actual sword.
Where do you update your position?
Sorry for the delay. I just do it in the Update method of the script attached to the weapon. Is that what you mean. How are you debugging? Are you debugging on a device on just in the editor?
Your answer
Follow this Question
Related Questions
Collision problem 0 Answers
2D box colliders not touching but are colliding, how to fix? 0 Answers
Weapon System with collide detection (Helps with script pls)!!! 0 Answers
Collision vs trigger 1 Answer
Modular Room System Similar to Mega Man 0 Answers