Table of contents
1 Collision2D 2D collision class
contactCount number of touch points
contacts Contact point (avoid use)
enabled Whether to enable collision response
gameObject the game object to collide with
otherRigidbody Other Rigidbody
relativeVelocity relative linear velocity (read-only reference)
2 Collider2D 2D collision body class
3 Contrasting the use of collision detection & trigger detection scripts
official document
Unity - Scripting API: Collision2D (unity3d.com)
Unity - Scripting API: Collider2D (unity3d.com)
Why do you want to summarize the two classes Collision2D and Collider2D? Because OnTriggerEnter2D and OnCollisionEnter2D are currently used in the process of learning from the tutorial , even though the format is somewhat similar but the content is slightly different, this blog simply summarizes the variables and usage differences between the two.
1 Collision2D 2D collision class
Collision2D is a class in UnityEngine, which returns the detailed information of two rigid body collisions through three callback functions.
1.1 Variables
The class variables are:
collider collider
The incoming Collider2D currently involved in a collision with otherCollider, attached to Collision2D.rigidbody2D .
contactCount number of touch points
Get the number of contact points of the collision. A collision may have multiple contact points. It can be used when retrieving the collision with GetContact or GetContacts .
contacts Contact point (avoid use)
Avoid using (will generate memory garbage), use GetContact or GetContacts mentioned above instead.
enabled Whether to enable collision response
Is a bool variable that prompts to enable or disable the collision response or reaction.
Collisions may be disabled for certain actions that the game wishes to implement, when disabled: Collision callbacks can still be obtained, but no responses or reactions will occur. For example, two boxes passing through each other without any apparent reaction .
gameObject the game object to collide with
The incoming GameObject involved in the collision.
When performing collision detection, to obtain a certain component of the game object where the current collision occurs, you need to access the variable .gameObject and then access the component. For example, the current collision class is other:
other.gameObject.GetComponent<RubyController>();
otherCollider other colliders
The incoming otherCollider2D involved in the collision with the collider attached to Collision2D.otherRigidbody2D .
otherRigidbody Other Rigidbody
rigidbody rigid body
relativeVelocity relative linear velocity (read-only reference)
Indicates the relative linear velocity of the two colliding objects. Since it is on 2D, this parameter is a vector2 type parameter. is a read-only parameter. To expand a bit, you can use:
vector2.magnitude - Returns a floating point number indicating the current relative velocity (retains the magnitude and loses direction information)
vector2.Normalize() ——Normalize a vector into a unit vector whose size is 1 and only indicates the direction
For example, the official documentation gives the following examples:
// Play a sound when we hit an object with a big velocity
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
AudioSource audioSource;
void Start()
{
audioSource = GetComponent<AudioSource>();
}
void OnCollisionEnter2D(Collision2D collision)
{
//.magnitude是C#中用以计算vector2类型变量长度的一个方法
if (collision.relativeVelocity.magnitude > 2)
audioSource.Play();
}
}
transform
The Transform of the incoming object involved in the collision.
1.2 Functions
GetContact
Collision2D-GetContact - Unity Screenplay API
GetContacts
2 Collider2D 2D collision body class
Collider2D is also a class in UnityEngine. In fact, it is the types of colliders we choose when we hang collider components for players. I also wrote a blog about 2D colliders to give a brief introduction:
Compared with Collision2D, Collider2D has more variables and public functions, directly check the official documentation.
3 Contrasting the use of collision detection & trigger detection scripts
We learned that Collider2D is composed of three callback functions: OnCollisionEnter2D, OnCollisionStay2D, and OnCollisionExit2D. These three callback functions can be used to realize the game logic after two rigid bodies collide.
Here you can compare the difference between OnTriggerEnter2D that appeared before and OnCollisionEnter2D in the above code. The following is the code that involves the trigger in the blood bag of the learning tutorial - trigger detection:
private void OnTriggerEnter2D(Collider2D other)
{
//获取Ruby对象的脚本组件,还是用GetComponent方法
RubyController rubyController=other.GetComponent<RubyController>();
if(rubyController != null)
{
...
Here is the code for collision blood loss when the enemy moves—collision detection——
//碰撞检测
//添加敌人伤害的逻辑
private void OnCollisionEnter2D(Collision2D other)
{
//获取和当前collision碰撞的对象的脚本组件
RubyController player = other.gameObject.GetComponent<RubyController>();
if (player != null)
{
player.ChangeHealth(Hurt);
}
}
It can be found that when obtaining the object script component, OnTriggerEnter2D (Collider2D other) is
other.GetComponent<RubyController>();
And OnCollisionEnter2D(Collision2D other) is
other.gameObject.GetComponent<RubyController>();
One difference is that the classes are different:
Trigger ---- Collider, refers to the collision body
Collision ---- Collision, refers to the collision itself
The class is different, so the variables of the class are also different, which determines the way to obtain the current object component. Combined with the simple description of Collision2D and Collider2D above, we can find:
- Collision2D wants to obtain some component information of the collision object, it needs to access the variable gameObject, and then obtain it through the GetComponent function of the GameObject class;
- Collider2D itself has the GetComponent function, so it can be accessed directly.