[Unity study notes - how to add a collision body to a dynamic character]

prospect feed

As in the title, I want to achieve simple interaction with the character in the project, for example, when he clicks on his arm, he will play the movement of the arm, and when he clicks on the foot, he will play the preset animation related to the foot

My previous implementation method was very violent to add a sub-object to the corresponding part of the character model, add a regular collision body such as a cube/circle to the sub-object, and then because it is under the bone, the character is moving At this time, the collision body will also move together, which is much more flexible than being tied to a static mesh, so that it will not be detected by the static mesh when clicking on an unrelated part and then play an inconsistent animation

But this has great limitations. The first is that it is inaccurate. Because it is a regular collision body, the detection range is much larger. The second is that sometimes the collision body between different parts will cross. It will affect. In the end, this implementation method is too nc. At that time, I really couldn’t think of other ways.

But now after more than a year, when I came back to look at it again, I found a relatively simple and efficient method with the highest detection quality.
This is a model I found on the Internet.

insert image description here

Solution

Add an empty object to the child node of the bone, then mount the mesh collider component to it, find the mesh corresponding to this bone, then drag it to the mesh of the mesh collider, and then fine-tune the distance (I use it here when the collision experience is better than the model It’s a lot more forward, pull it back, and it’s the same as what you can see with the naked eye)

step

step 1

Add a child node to the bone node you need
insert image description here

step 2

Add mesh collider components to child nodes
insert image description here

step 3

Find the mesh corresponding to the bone, and then drag it to the mesh in the component created in step 2
insert image description here

step 4

Then when you run it, you will find that the collision body is much ahead of the model
insert image description here

Just move this child node to cover the mesh corresponding to the bone.
Compiler: It can be adjusted, and it can also be adjusted during operation, but it is relatively troublesome.
Game scene: as shown in the figure below, a small method, pause when you fine-tune Game scene, and then adjust it slowly, remember to copy the position of this child node after the adjustment, stop the Game scene and copy it to this child node
insert image description here

step 5

There is a canvas toggle on the mesh collider component, you can check it. After checking it, the number of slices is obviously much less, which will undoubtedly reduce the performance a little.

For other parts, you only need to take pictures of cats and tigers~
I have to say, this method is more comfortable just by looking at it.

By the way, here is a method for detecting 3d objects

Two things to pay attention to.
The first is that the layer of your main camera must be selected as the main camera.
The second is that the main camera must have a PhysicsRaycaster component.
Both are indispensable~

		PointerEventData pointerEventData = new PointerEventData(EventSystem.current);
        pointerEventData.position = Input.mousePosition;
        PhysicsRaycaster pr = Camera.main.GetComponent<PhysicsRaycaster>();
        List<RaycastResult> results = new List<RaycastResult>();
        pr.Raycast(pointerEventData, results);

The results are the detected results, just output it

For 2d object detection, just change the PhysicsRaycaster component above to GraphicRaycaster

Guess you like

Origin blog.csdn.net/pure81/article/details/126785474