Getting Started Tutorial] [Unity3D physics engine collision of resolve

Built Unity3D physics engine may simulate physical effects. Typical physical effect is a collision. This article will tell collision law Unity3D for the simplest cases.

 

1 Collision Rule

First, create a Sphere and Cube in a scene, they are the default with the impactor.





If you want the object being a physical control, you need to add a rigid assembly. At this time, the object will be affected by gravity and can collide with other objects.

 

  • Classification impactor

It can be classified according to shape impactor BoxCollider, SphereCollider, CapsuleCollider, WheelCollider, MeshCollider like. Here we are with the Box and Sphere Collider as an example to expand the narrative.

 

  • rigid body

Objects with only impactor without rigid body, it is unable to respond to the behavior of the object. Such as a static impactor, suitable for walls, floors and other stationary object. When added to a rigid object, the object can accept the forces and moments, resulting in close to real objects behave. Note that, if you want two objects collide, there is at least one object to be with a rigid assembly.


Mass rigid assembly quality, and the Drag Angular Drag resistance is resistance and the angle, these values ​​can affect all of collision effects. UseGravity gravity switch will open in response to gravity, no response closed. IsKinematic a kinematic switch, if enabled, will not be a physical object engine drive can only be operated by Transform. The following default Discrete CollisionDetection (the discontinuity detection). The bottom is Constraints, you can set limits rigid body motion.

 

2 Examples of collision

To detect a collision object and switches, need to be bound to the following script collider.cs the Cube.

using UnityEngine;
using System.Collections;

public class collider : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
    //碰撞检测
    void OnCollisionEnter(Collision colliderInfo)
    {
        Debug.Log(colliderInfo.collider.name+"撞过来了!");
    }
    void OnCollisionExit(Collision colliderInfo)
    {
        Debug.Log(colliderInfo.collider.name + "结束碰撞!");
    }
    void OnCollisionStay(Collision colliderInfo)
    {
        Debug.Log(colliderInfo.collider.name + "处于碰撞中!");
    }
    //物理开关
    void OnTriggerEnter(Collider collider)
    {
        Debug.Log("Trigger: "+collider.name +"进入!");
    }
    void OnTriggerExit(Collider collider)
    {
        Debug.Log("Trigger: " + collider.name + "离开!");
    }
    void OnTriggerStay(Collider collider)
    {
        Debug.Log("Trigger: " + collider.name + "触发中!");
    }

}


For already created Cube and Sphere, which itself is a collision with a filter, to which we add two rigid objects. Click on objects AddComponent, enter Rigidbody in the drop-down menu in the search, select and add to the rigid body can keep the default parameters.


(1) crash tests

不勾选Cube的BoxCollider的IsTrigger,运行游戏。用鼠标拖到Sphere去碰撞Cube,可以看到如下信息:


(2)测试物理开关

勾选Cube的BoxCollider的IsTrigger,运行游戏。用鼠标拖到Sphere去碰撞Cube,可以看到如下信息:



发布了65 篇原创文章 · 获赞 265 · 访问量 55万+

Guess you like

Origin blog.csdn.net/zzlyw/article/details/54287512