如果 (Physics.Raycast(ray, out hit,Mathf.Infinity, touchInputMask) 我永远进不去

I never get inside if (Physics.Raycast(ray, out hit,Mathf.Infinity, touchInputMask)

我想做的是当用户触摸一个对象时他可以移动它,特别是在屏幕上用多点触控手指

我也试过用鼠标点击做这个射线,但还是有同样的问题

为此,我在更新时使用此代码

public LayerMask touchInputMask;
private static List<GameObject> touchList = new List<GameObject>();
public static Dictionary<int, objectst> touchobjects = new 
Dictionary<int, objectst>();
private GameObject[] touchesOld;
private RaycastHit hit;
public GUIText Count, IndexLift;
private Vector3 targetPos;
public struct objectst { public Vector3 screenPoint; public Vector3 offset; 
}

void Update(){

 if (nbTouches > 0)
  {
     //nbTouches = 5;
     //print(nbTouches + " touch(es) detected");
     touchesOld = new GameObject[touchList.Count];
     touchList.CopyTo(touchesOld);
     touchList.Clear();
     for (int i = 0; i < nbTouches; i++)
     {
         Touch touch = Input.GetTouch(i);
         //print("Touch index " + touch.fingerId + " detected at 
  position " + touch.position);
         Ray ray = Camera.main.ScreenPointToRay(touch.position);
         if (Physics.Raycast(ray, out hit,Mathf.Infinity,  
   touchInputMask))
         {
             GameObject recipient = hit.transform.gameObject;
             print("#### touch hit name object "+recipient.name);
             touchList.Add(recipient);
             //recipient.;
             if (touch.phase == TouchPhase.Began)
             {
                 print("#### tcouh begin");

                 objectst tempobj = new objectst();
                 tempobj.screenPoint = 
    Camera.main.WorldToScreenPoint(recipient.transform.position);
                 tempobj.offset = recipient.transform.position - 
   Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, 
  touch.position.y, tempobj.screenPoint.z));
                 touchobjects.Add(touch.fingerId, tempobj);
             }
             if (touch.phase == TouchPhase.Stationary || touch.phase 
  == TouchPhase.Moved)
             {
                 print("#### tcouh stationary or moved");
                 Vector3 curScreenPoint = new Vector3(touch.position.x, touch.position.y,
                     touchobjects[touch.fingerId].screenPoint.z);
                 Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + touchobjects[touch.fingerId].offset;
                 print("#### objet doit être deplacer x = "+curPosition.x+"y = "+curPosition.y);
                 recipient.transform.position = curPosition;
             }
             if (touch.phase == TouchPhase.Ended)
             {
                 print("#### tcouh ended");
                 Vector3 curScreenPoint = new Vector3(touch.position.x, touch.position.y,
                     touchobjects[touch.fingerId].screenPoint.z);
                 Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) - touchobjects[touch.fingerId].offset;
                 recipient.transform.position = curPosition;
             }
         }
     }
   }
 }

编辑

屏幕截图

我正在根据我从评论中获得的信息写这个答案。

为了 Physics.Raycast 正常工作,您首先需要在您希望 Raycast 与之碰撞的物体上设置 Collider。如果你的对象没有 Collider 那么你的 Raycast 将不会 return 任何东西(因为它没有与任何东西碰撞)。

ColliderRigidbody 不是一回事,A Collider 通常用于碰撞检测,并且经常被 Rigidbody 用于处理物理基于碰撞响应。具有 Rigidbody 的对象不会与 Raycast 发生碰撞,除非该对象也具有 Collider.

PhysicsRigidBody 仅适用于 3d 碰撞体。 Physics2DRigidBody2D 用于处理 2d 物理,您不能混合使用 2d 物理和 3d 物理组件,因为它们在 Unity 中不能相互工作。

LayerMask, 是一个工具来决定你想要处理哪个碰撞层,每个对象都在他们的名字下面有一个标签,和一个层。这用于确定您希望该对象位于哪个碰撞层。如果您检查物理设置,您可以确定彼此在哪个层 collide/trigger。

所以一个解决方案是添加一个名为 TouchableObject 的新层,然后将您想要成为的任何对象分配给该层 "Touchable",并为其提供适当的 Collider(2d Collider 或 3d在您的代码中,您需要一个 3d 对撞机,或者将您的 Physics.Raycast 调用转换为 Physics2D.Raycast)。在您的 Touch 脚本中,您可以让 LayerMask 仅针对该层。

我能够像这样解决问题

            RaycastHit2D hit = Physics2D.Raycast(new Vector2(Camera.main.ScreenToWorldPoint(touch.position).x, Camera.main.ScreenToWorldPoint(touch.position).y), Vector2.zero, 0f);


            if (hit.rigidbody != null)

当然,命中的刚体是我点击的对象

感谢@Edge 的帮助