C# 我从未进入过if(Physics.Raycast(光线、外击、Mathf.Infinity、touchInputMask)

C# 我从未进入过if(Physics.Raycast(光线、外击、Mathf.Infinity、touchInputMask),c#,unity3d,raycasting,C#,Unity3d,Raycasting,我想做的是,当用户触摸一个物体时,他可以移动它,特别是用屏幕上的多点触摸手指 我也试着用鼠标点击来做这个射线,但是仍然有同样的问题 为此,我在更新时使用此代码 public LayerMask touchInputMask; private static List<GameObject> touchList = new List<GameObject>(); public static Dictionary<int, objectst> touchobject

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

我也试着用鼠标点击来做这个射线,但是仍然有同样的问题

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

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;
             }
         }
     }
   }
 }
公共层掩码touchInputMask;
私有静态列表touchList=新列表();
公共静态字典touchobjects=new
字典();
私有游戏对象[]触摸屏;
二等兵雷卡斯特命中;
公共吉他文本计数,IndexLift;
私人向量机3 targetPos;
public struct objectst{public Vector3屏幕点;public Vector3偏移量;
}
无效更新(){
如果(大于0)
{
//nbc=5;
//打印(NBTouchs+检测到“触摸”);
touchesOld=新游戏对象[touchList.Count];
touchList.CopyTo(touchesOld);
touchList.Clear();
对于(int i=0;i

编辑

截屏


我根据从评论中获得的信息撰写此答案

为了使
Physics.Raycast
正常工作,首先需要在要与
Raycast
碰撞的对象上使用
碰撞器。如果对象没有
碰撞器
,则
Raycast
将不会返回任何内容(因为它没有与任何内容碰撞)

碰撞器
刚体
不是一回事,
碰撞器
通常用于碰撞检测,并且经常被
刚体
用来处理基于物理的碰撞响应。具有
刚体
的对象不会与
光线投射
碰撞,除非该对象也有一个
碰撞器

物理
刚体
将仅与三维碰撞器一起工作。
物理2d
刚体2d
用于处理二维物理,不能将二维物理和三维物理组件混合在一起,因为它们不能统一工作

LayerMask
是一种工具,用于确定要处理的碰撞层,每个对象名称下都有一个标记和一个层。这用于确定要将该对象放在哪个碰撞层上。如果检查物理设置,可以确定哪个层相互碰撞/触发


因此,一个解决方案是添加一个名为TouchableObject的新层,然后将任何您希望“可触摸”的对象指定给该层,并为其提供适当的
碰撞器
(在您的代码中,您可能需要一个3d碰撞器,或者将您的
physical.Raycast
调用转换为
Physics2D.Raycast
)。在您的
触摸
脚本中,您将使
层任务
仅针对该层。

我根据从评论中获得的信息编写此答案

为了使
Physics.Raycast
正常工作,首先需要在要与
Raycast
碰撞的对象上使用
碰撞器。如果对象没有
碰撞器
,则
Raycast
将不会返回任何内容(因为它没有与任何内容碰撞)

碰撞器
刚体
不是一回事,
碰撞器
通常用于碰撞检测,并且经常被
刚体
用来处理基于物理的碰撞响应。具有
刚体
的对象不会与
光线投射
碰撞,除非该对象也有一个
碰撞器

物理
刚体
将仅适用于3d碰撞器。
物理2d
刚体2d
用于处理2d物理,您不能混合使用2d物理和3d物理组件,因为它们不能用于每个碰撞器
            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)