代码之家  ›  专栏  ›  技术社区  ›  Enoy

RayCastHit2D未检测到播放器

  •  0
  • Enoy  · 技术社区  · 6 年前

    我试图从一个实例化的导弹预制件中投射一个2D射线,如下所示:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class EnemyMissile : MonoBehaviour {
    
    
        void Update () {
            RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.down,1f);
            Debug.DrawRay(transform.position, Vector2.down, Color.green);
            if(hit)
            {
                print("Hit");
            }
            else
            {
                print("No hit");
            }
        }
    }
    

    enter image description here

    enter image description here

    enter image description here

    不过,我看到控制台上什么也没印

    我看到: Raycast Hit Collider is Always NULL Physics2D.Raycast returning null

    谢谢你的帮助。

    编辑 回答@Monofuse评论:当我选择玩家时,转换是: x:0.4567,y:-1.58,z:0

    enter image description here

    当我选择导弹时,变换是: x:0.3699,y:-1.054,z:0

    enter image description here

    编辑回答@Ada Nub:

    我默认选中了这两个选项:查询命中触发器,查询在碰撞器中启动。

    现在我在Colliders中启动了未选中的查询,但是结果是相同的,我在控制台上看不到打印:

    enter image description here

    enter image description here

    现在它工作了: enter image description here

    1 回复  |  直到 6 年前
        1
  •  5
  •   Somdip Dey    6 年前

    是否已在“编辑”>“项目设置”>“Physics2D”中启用“查询命中触发器”?

    Image of 'Queries hit triggers' option

    它下面的选项“查询在碰撞器中开始”可能也值得研究;因为您是从导弹的碰撞器内部投射光线投射,所以您可能希望取消选中该选项,这样就不会将它开始的导弹计算为命中。