Ray2D ray;
?? ?void Update()
?? ?{
?? ??? ?ray = new Ray2D(transform.position, Vector2.right);
?? ??? ?RaycastHit2D info = Physics2D.Raycast(ray.origin, ray.direction,10);
?? ??? ?//Debug.DrawRay(ray.origin,ray.direction,Color.blue);
?? ??? ?if (info.collider != null)
?? ??? ?{
?? ??? ??? ?if (info.transform.gameObject.CompareTag("Boss"))
?? ??? ??? ?{
?? ??? ??? ??? ?Debug.LogWarning("檢測到敵人");
?? ??? ??? ?}
?? ??? ??? ?else
?? ??? ??? ?{
?? ??? ??? ??? ?Debug.Log("檢測到其他對象");
?? ??? ??? ?}
?? ??? ?}
?? ??? ?else
?? ??? ?{
?? ??? ??? ?Debug.Log("沒有碰撞任何對象");
?? ??? ?}
?? ?}
https://blog.csdn.net/yjy99yjy999/article/details/82904207
//起點、方向
RaycastHit2D info = Physics2D.Raycast(startPos, Vector2.right); //無限遠(yuǎn)
?
//起點、方向、距離:
RaycastHit2D info = Physics2D.Raycast( startPos, direction, 10f );
?
?
//如果已經(jīng)定義了光線,可以使用光線的信息投射:
RaycastHit2D info = Physics2D.Raycast(ray.origin, ray.direction);
只對指定層級激活
正確用法1
//使用Layer ID?
?RaycastHit2D info = Physics2D.Raycast(transform.position, dir, dist, 1<<14);
正確用法2:
//使用Layer Name
int LayerID = LayerMask.NameToLayer("Enemy");
RaycastHit2D info = Physics2D.Raycast(transform.position, dir, dist, 1<<LayerID);
用法說明:這個參數(shù)是一個奇妙的int值,用一個數(shù)字表示了所有圖層包含與否的設(shè)置。
1 << ?14 表示僅包含14層(不包含其余的層)
~(1<<14) 表示不包含14層(包含其他所有層)文章來源:http://www.zghlxwxcb.cn/news/detail-554191.html
(1<<12) | (1<<14) 表示包含12、14層 (不包含其余層)文章來源地址http://www.zghlxwxcb.cn/news/detail-554191.html
到了這里,關(guān)于unity 2D射線的使用方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!