Unity game - collision detection between warriors and monsters

Summary: After pressing the mouse, the samurai will swing his sword to meet the attack. If he can successfully cut it, the monster will fly around. However, if you touch the monster without cutting it down, the game is over. In order to implement this function, it is necessary to check the conflict between the warrior and the monster, which is the so-called collision detection process.

In many games, collision detection is a very important part, but it is often troublesome in program processing. But in Unity, you only need to set the shape to calculate the collision detection


1. The problem of collision detection for each monster separately

First we tried using a cube for the samurai and a sphere for the monster to perform collision detection. Collision detection between characters is often performed using this rough geometry. The advantage of this is that it is much less computationally intensive than for strict geometries. And for most games, this approach can get more realistic results.

In general, spheres are less computationally intensive. Because there will be a large number of monsters in the game, we chose a sphere with as little calculation as possible as the detection shape.

After setting the shape, Unity can be used to perform the calculation of collision detection. Then implement the behavior of the samurai after the collision seems to be done. But in fact, after the game is running, you will find that there are still many problems.

1. A big problem is that the samurai will bypass the monster and move forward

1. Jump up and over

2. to pass by

 This is because the monster's collision detection shape is much smaller than the samurai's. In Unity's collision processing, in order to make the object can still move in the original direction after the collision, sliding and other processing are added to the collision object .

Making the collided objects slide would make the game easier and more natural to play in most cases, but in our case it was dangerous.

2. Another problem is that the difficulty of attack will increase.

After clicking the mouse button, the samurai starts to swing the machete. Attack detection will be performed along with this attack action. Similar to the collision detection of monsters, we also use spheres for detection.

While the sphere size for attack detection is large enough, offsets inward or outward from the center of the samurai may still be outside the attack range. This creates situations where it appears that the attack was successful, but it was not.

For this problem, trying to change the shape and size of the collision detection is also a solution. However, the size of the monster itself is small, and if it is enlarged, another extreme will appear, that is, the situation of "it seems to miss, but it turns out to be concentrated" may increase.

So you need to try other solutions, as follows.

2. Organize monsters into groups

The monsters in the game always appear together, and the monsters of the same batch are relatively dense with each other. In addition, the samurai always move forward in a straight line, so it seems that there is no problem even if the same batch of monsters are organized into a group to deal with.

So we use the OniGroup object to gather the monsters and treat the monsters as its child elements. Collision detection has also been changed to perform on the OniGroup object as a whole.

The monster team's collider anycheck uses a cube, which is roughly the same size as the cube used for the samurai's collision detection. With this set up, the samurai can no longer jump over monsters or go around monsters from one side.

In addition, assuming that OniGroup detects Ou's attack from a samurai, all monsters that are child elements will be attacked. In this way, the above-mentioned situation where the attack is unsuccessful due to a slight difference in the position of the monster will no longer exist.

summary

Unity allows direct use of the model's shape for collision detection. However, rough shapes are used in many games and can be used to great effect. Especially in the case of characters that are much smaller in size than other characters and a lot of objects are moving together, it is a good idea to group them together for collision detection.

Guess you like

Origin blog.csdn.net/m0_63024355/article/details/131853909