- Home /
Melee Combat Collision
Hi All
I am trying to get some melee combat working; in this case I have a character (3rd person) with various attack animations. One or two of these animations seems to be too fast for the system, here is what I have done.
The character has a sword equipped (with a collider on the blade). When the character attacks an attack animation is triggered, when the animation reaches a certain point an AnimationEvent is fired (this event just sets a bool used to determine if the collision should be handled or not - used to avoid attacking an enemy when walking into one for example).
This works well, most of the time! But what I am experiencing is that once in a while the sword passes through the target without hitting, but then it hits on the return trip (by that I mean that the animation pulls the sword back to the ready stance after swinging, and this drags the blade (and hence the collider) through the target and triggers the hit).
So in short it seems that the sword passes through the enemy collider sometimes too fast (it is a fast attack). This is also something I have seen others mention that perhaps the animation is so fast that the position of the blade skips such a distance that at one frame it is “to one side of the enemy collider” and the next frame it is “on the other side of the enemy collider”?
In any case, I have tried to slow the animation and increase the enemy collider size, both seems to do the trick, however I would like to hear if there is a better solution for this “issue” and if it is indeed happening because the attack animation is too “fast paced”.
Also would I be better off dropping the collider approach and instead make an AnimationEvent at the point where the sword is supposed to “hit” and then do a RayCast in this event to check if an enemy is close enough and handle the hit here.
I hope it makes sense to someone!!!
Answer by iDerpCake · Nov 07, 2015 at 07:34 AM
This might sound confusing but I think I know a fix. When you hit the attack key or button make it so it enables a game object with a box collider in front of the player. Then make it so when the box collider collides with the enemy then send a message dealing the damage. I hope this worked if not try using it as a trigger instead. Hoped this helped
Answer by rhbrr5hrfgdfgw · Nov 07, 2015 at 01:23 PM
As you press the attack button send a raycast (just for example with a range of 2) and if it hits the enemy (lets say it has a "Enemy" tag then deal the damage, something like that:
var hit : RaycastHit;
void Update() {
Vector3 fwd = transform.TransformDirection(Vector3.forward);
if (Physics.Raycast(aaaaaa, fwd, hit, 2) && hit.transform.tag == "Enemy") {
print("There is something in front of the object!");
}
}
$$anonymous$$y problem is that I would like the impact to play a particle effect, and that should be played when the sword hits the target - which is a while after the attack button is clicked.
But perhaps I could do what you say from the hilt of the sword and out. However I suspect that I would still encounter the same issue in that case, namely that the raycast will occur one frame before the target, and then next raycast will be sent after the sword has passed the target.
Again I am back to two options as I can see it? Either trigger the particle effect and hit using a Animation Event (placed in the animation where the sword is straight out from the target) OR expand the collider.
The main issue is if this is actually happening because the frame skips such a distance that it passes the collider before the next frame. If that is the case then it should be something that has happened often to others with bullets I would guess (at least bullets done using colliders).
To be more clear, this is what you have to do: At the scene you make an empty gameobject and place it inside the player as a child, at about the middle or the top of the sword(you will have to check what works the best for you). So on click make a raycast that goes out of your gameobject. now as you click when you check this code:
if (Physics.Raycast(aaaaaa, fwd, hit, 2) && hit.transform.tag == "Enemy") {
print("There is something in front of the object!");
}
It says that the raycast hitted something, so after a the animation is over just instantiate an GameObject variable(the particles). To check when the animation is over check how much time the animation actually lasts and after that time instantiate. the code should look something like that:
var hit : RaycastHit;
var theNewEmptyGameObject : GameObject;
void SendRayCast(){
if (Physics.Raycast(theNewEmptyGameObject , theNewEmptyGameObject .forward, hit, 2) && hit.transform.tag == "Enemy") {
yield WaitForSeconds(YourAnimationTime);
print("Now you can instantiate the particles");
}
}
void Update() {
if(Input.Get$$anonymous$$ouse.......)
SendRayCast();
}
Your answer
Follow this Question
Related Questions
Can't click gameobject when over another trigger? 1 Answer
Ball activating triggers even with a script 0 Answers
How can I attach a colldier that moves with the enemies sword in an attack animation? 0 Answers
How do you detect which collider is triggered when you have multiple? 2 Answers
Trigger and Collider Issues 1 Answer