简体   繁体   English

Unity3D Physics.Raycast()永远不正确

[英]Unity3D Physics.Raycast() never true

I have a scene where I have an game object with sprite renderer and image attached to it. 我有一个场景,其中有一个带有精灵渲染器和图像的游戏对象。 The game object has a 2D collider to. 游戏对象具有2D对撞机。 I'm trying to get a true reading when I'm clicking the image but its always false, I have no idea what em i doing wrong. 当我单击图像时,我试图获得真实的读数,但是它始终是错误的,我不知道自己在做什么错。

 TapPos = GetComponent<Camera> ().ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 0));   

 bool SomethingTapped(){

    Vector3 CanvasTapPoint = new Vector3 (TapPos.x, TapPos.y, 10);
    Debug.DrawLine (TapPos, CanvasTapPoint, Color.green, 10f);

    if (Physics.Raycast (TapPos,CanvasTapPoint, 20f)) {
        return true;
    } else
        return false;
}

PS: PS:

  • The Debug.DrawLine goes right through my sprite. Debug.DrawLine贯穿我的精灵。

  • The script is on the camera and the camera Z position is -10 脚本位于相机上,相机Z位置为-10

EDIT After Andrew Griffin's response changed my code to this if anyone want to do the same thing: 编辑在安德鲁·格里芬(Andrew Griffin)的回应之后,如果有人想做同样的事情,我的代码将更改为:

    public Transform ObjectTapped(){

    RaycastHit2D FoundObj = Physics2D.Raycast(TapPos, new Vector2(TapPos.x, TapPos.y + 1),0f);

    if(FoundObj.collider  != null){
        Transform obj = FoundObj.transform;
        return obj;
    }
    else{
        return null;
    }
}

Look into Physics2D.Raycast . 查看Physics2D.Raycast As far as I am aware, Physics.Raycast will not work with 2D colliders. 据我所知,Physics.Raycast不适用于2D对撞机。 You must use Physics 2D .Raycast instead. 您必须改用Physics 2D .Raycast。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM