Chapter 29 Unity Joint Joint

Joint components connect a rigid body to another rigid body or to a fixed point in space. Joints apply forces that move a rigid body, and joint limits limit that movement. Unity provides the following joints that can apply different forces and constraints to rigid body components, allowing these rigid bodies to have different motions:

Hinge Joint: Makes two rigid bodies move as if they were connected on a hinge. Used to simulate doors and finger joints.

Fixed Joint: Binds two rigid bodies together so that their relative positions remain unchanged.

Spring Joint: Connects two rigid bodies together so that they move like a spring.

Character Joint: Simulates a ball and socket joint, such as a hip or shoulder.

Configurable Joint Configurable Joint: Simulates any skeletal joint, such as the joints in a ragdoll.

Next create a new scene "SampleScene4.unity" and then create a sphere Sphere

Next, we add a "Hinge Joint" type joint to the Sphere. Select "Component" -> "Physics" in the navigation menu bar and select the "Hinge Joint" component from there. Before adding this component, you must first add two components: a collision body and a rigid body. When we create the Sphere, we automatically add a collision body to it, and then when we add the "Hinge Joint" component, Unity automatically adds a rigid body component for us. In order to prevent gravity from affecting the Sphere, we canceled the "Use Gravity" attribute of the rigid body component. Then, we click on the Sphere game object to view its Inspector view:

In the component "Hinge Joint", we can see three properties: "Connected Body" (connected rigid body), "Anchor" (anchor point) and "Axis" (axial direction). The anchor point and axis are both the local coordinate system of the game object, and "connecting the rigid body" requires us to add another game object with a rigid body. As follows:

We added a Cube directly above the Sphere to which the Rigidbody component was added. In order to prevent gravity from affecting the Cube, we canceled the "Use Gravity" attribute of the rigid body component. As follows:

Next, we click to select the Sphere game object, and then drag the Cube from the Hierarchy panel to the input box behind the "Connected Body" under the "Hinge Joint" component of the Sphere game object, as shown below:

Next we apply a force in the X-axis direction to the Sphere, as shown below:

Then, we come to Play to run the entire project,

The running effect can be seen. When the yellow ball Sphere moves to the right, the red Cube is pulled to move together. It's as if there is a "connecting line" between the two.

A classic case of using "Hinge Joint" hinge joints is the opening and closing effect of doors.

First, we create a new scene "SampleScene5.unity", then we add a Plane as the ground, then add a Cube to make it a "door axis", and then add a Cube(1) to make it a "door axis". Become a "door leaf". Add rigid bodies to both the door shaft and the door leaf, and finally add a "Hinge Joint" to the door leaf.

First add a portal axis Cube and add a rigid body component, as shown below:

Then add the door leaf Cube(1) and also add the rigid body component, as shown below:

 

Next, we add the "Hinge Joint" component to the door leafCube(1). What we have to do now is to make the yellow "door leaf" rotate around the purple "door axis" to create the effect of opening the door. In other words, when we apply a force to the door leaf, the door leaf needs to rotate around the door axis, while the door axis remains stationary. Therefore, for the door axis, its rigid body component needs to check the "Is Kinematic" attribute (it cannot move or rotate under the control of the rigid body). As follows:

Then we also need to give a resistance to the rigid body component of the door leaf so that the door leaf cannot continue to rotate after encountering a collision, as follows:

Next is the "Hinge Joint" hinge joint properties of the door leaf, as shown below:

First, we need to drag the portal axis to the "Connected Body" position, and then modify the Anchor anchor point and Axis axis parameters. To control the rotation of the door, you mainly change the Anchor and Axis properties of the hinge joint component. Please note that the game object door leaves move or rotate in the axial direction around the anchor point. Therefore, our anchor point should be the edge of the door leaf close to the door axis, and the axial direction should be the vertical direction of the Y axis. Click "Edit Angular Limits" as shown below:

 

The picture above shows that our anchor point is located at the door axis, and the axis direction is the Y-axis upward. Then, when we give the door a force, the door will move and rotate around the door axis. Here we add a capsule Capsule game object,

Then add a rigid body component and check the "Is Kinematic" property.

Next, we create the "MoveScript.cs" script and use code to control the capsule movement. The code is as follows:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveScript : MonoBehaviour
{
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.W)) transform.Translate(transform.forward);
    }
}

Next, we attach the script to the Capsule capsule, and then Play runs the entire project, as shown below:

When we move the green capsule forward, it will collide with the door, and the door will move forward, but due to the influence of the hinge joint, it can only rotate along the door axis, which is the effect of opening the door.

The content involved in this course has been shared on Baidu Cloud Disk: https://pan.baidu.com/s/1e1jClK3MnN66GlxBmqoJWA?pwd=b2id

Guess you like

Origin blog.csdn.net/konkon2012/article/details/130479899