当前位置:网站首页>[unity3d] rigid body component

[unity3d] rigid body component

2022-06-26 05:02:00 little_ fat_ sheep

1 Preface

         rigid body (Rigidbody) yes kinematics (Kinematic) A concept in , It refers to the movement and force , The shape and size remain the same , And the relative positions of the interior points remain unchanged . stay Unity3D in , Rigid body components give game objects some kinematic properties , It mainly includes Mass( quality )、Drag( resistance )、Angular Drag( Angular resistance )、Use Gravity( Whether to use gravity )、Is Kinematic( Whether it is affected by physics )、Collision Detection( collision detection )、 Velocity( Speed )、Force( Stress )、Explosion Force( Explosive force ). No rigid body (RigidBody) Components , Game objects can penetrate each other , There's no collision .

        1) Get the rigid body component

Rigidbody rig = GetComponent<Rigidbody>();

        2) Rigid body component panel properties

  • Mass: The mass of the object ( Default in kilogram )
  • Drag: The amount of air resistance to the object
  • Angular Drag: When an object rotates , The magnitude of the rotational resistance received
  • Use Gravity: If enabled , Objects will be affected by gravity
  • Is Kinematic: If enabled , Objects will not be driven by the physics engine , Only by its  Transform Component operations
  • Interpolate: Interpolator of the moving position of the object
  • Collision Detection: Collision detection type , When you see an object moving too fast through a wall , Collision detection frequency can be enhanced , choice Continuous Options
  • Constraints: Restrictions on the motion and rotation of rigid bodies , The component on a coordinate axis remains unchanged when restricting the motion of an object
  • velocity: Object motion vector velocity

        3) Rigid body component method

//  The thrust on the rigid body 
public void AddForce(Vector3 force)
//  The explosive force on the rigid body ,explosionForce: The magnitude of the explosive force ,explosionPosition: Explosion point ,explosionRadius: Explosion radius 
public void AddExplosionForce(float explosionForce, Vector3 explosionPosition, float explosionRadius)

2 application

2.1 Application 1

        1) Create game objects

        establish Cube and Plane The game object , as follows :

         2) to Cube Game objects add rigid body components

        Choose Cube The game object , Click on Add Component Button , Search for Rigidbody, Add rigid body components .

         3) add to RigidbodyController Script

        RigidbodyController.cs 

using UnityEngine;

public class RigidbodyController : MonoBehaviour {
	private Rigidbody rig;

	void Start () {
		rig = GetComponent<Rigidbody>();
	}

	void Update () {
		float hor = Input.GetAxis("Horizontal");
        float ver = Input.GetAxis("Vertical");
		float up = Mathf.Sqrt(hor * hor + ver * ver);
		if (up > 0.1) {
			rig.velocity = new Vector3(hor, up, ver);
		}
	}
}

        4) Running effect

        Use the up, down, left and right arrow keys , Control the cube to lift up to the surrounding sky , Release the key , The cube fell down under the action of gravity .

2.2 Application 2

        1) add to RigidbodyController Script

        On the basis of application one , take  RigidbodyController The script is modified as follows :

        RigidbodyController.cs 

using UnityEngine;

public class RigidbodyController : MonoBehaviour {
	private Rigidbody rig;

	void Start () {
		rig = GetComponent<Rigidbody>();
	}

	void Update () {
		float hor = Input.GetAxis("Horizontal");
        float ver = Input.GetAxis("Vertical");
		float up = Mathf.Sqrt(hor * hor + ver * ver);
		if (up > 0.1) {
			rig.AddForce(new Vector3(hor, 0, ver) * 10); //  Add thrust 
		}
	}
}

        2) Running effect

        Use the up, down, left and right arrow keys , The control cube receives a push from all around .

2.3 Application 3

        1) Create game objects

        On the basis of application one , establish 4 individual Cube The game object ,position Respectively :(0, 1, 2)、(0, 1, -2)、(-2, 1, 0)、(2, 1, 0), as follows :

          to 4 individual Cube Game objects are added Rigidbody Components and RigidbodyController Script components .

        2) add to RigidbodyController Script

using UnityEngine;

public class RigidbodyController : MonoBehaviour {
	private Rigidbody rig;

	void Start () {
		rig = GetComponent<Rigidbody>();
	}

	void Update () {
		if (Input.GetKeyDown(KeyCode.Space)) {
            //  stay (0,0,0) Add... At coordinates 10 Within meters 300N Explosive force of 
			rig.AddExplosionForce(300, Vector3.zero, 10);
		}
	}
}

         3) Running effect

        Press the space bar ,4 Three cubes are subjected to an explosive force from the center .

原网站

版权声明
本文为[little_ fat_ sheep]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260457527377.html