返回介绍

RaycastHit2D.collider 碰撞器

发布于 2019-12-18 15:38:23 字数 2641 浏览 1228 评论 0 收藏 0

JavaScript => public var collider: Collider2D;
C# => public Collider2D collider;

Description 描述

The collider hit by the ray.

射线碰到的碰撞器。

This can be useful if the hit object has more than one collider - this property can be used to determine the specific collider rather than just the object.

如果碰到的对象不只一个碰撞器,这个属性用来确定特定的碰撞器。

Note that some functions that return a single RaycastHit2D will leave the collider as NULL which indicates nothing hit. RaycastHit2D implements an implicit conversion operator converting to bool which checks this property allowing it to be used as a simple condition check for whether a hit occurred or not.

注意,某些函数返回单个RaycastHit2D的碰撞器为空,表示没有碰到东西。RaycastHit2D隐式转换为布尔,检查这个属性允许它被作为一个简单的条件检查是否发生碰撞。

JavaScript:

#pragma strict
//Attach this script to an empty gameobject.
//When you click on a sprite with a collider it will tell you it's name.
function Update() {
	//If the left mouse button is clicked.
	if (Input.GetMouseButtonDown(0)) {
		//Get the mouse position on the screen and send a raycast into the game world from that position.
		var worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
		var hit = Physics2D.Raycast(worldPoint, Vector2.zero);
		//If something was hit, the RaycastHit2D.collider will not be null.
		if (hit.collider != null) {
			Debug.Log(hit.collider.name);
		}
	}
}

C#:

using UnityEngine;
using System.Collections;
 
//Attach this script to an empty gameobject.
//When you click on a sprite with a collider it will tell you it's name.
public class ExampleClass : MonoBehaviour
{
	void Update()
	{
		//If the left mouse button is clicked.
		if (Input.GetMouseButtonDown(0))
		{
			//Get the mouse position on the screen and send a raycast into the game world from that position.
			Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
			RaycastHit2D hit = Physics2D.Raycast(worldPoint,Vector2.zero);
 
			//If something was hit, the RaycastHit2D.collider will not be null.
			if ( hit.collider != null )
			{
				Debug.Log( hit.collider.name );
			}
		}
	}
}

RaycastHit2D

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文