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

时间:2018-01-17 15:13:46

标签: c# unity3d raycasting

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

我也尝试用鼠标点击这个光线,但仍然有同样的问题

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

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;
             }
         }
     }
   }
 }

编辑

屏幕截图

enter image description here

2 个答案:

答案 0 :(得分:1)

我根据评论中提供的信息撰写此答案。

为了让Physics.Raycast正常工作,您首先需要Collider对象Raycast要碰撞的对象。如果您的对象没有Collider,那么您的Raycast将不会返回任何内容(因为它没有与任何内容发生冲突)。

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

PhysicsRigidBody仅适用于3D碰撞器。 Physics2DRigidBody2D用于处理2d物理,你不能混合使用2d Physics和3d Physics组件,因为它们在Unity中不会相互配合。

LayerMask,是一个工具,用于确定要处理的Collision图层,名称下方的每个对象都有一个标记和一个图层。这用于确定您想要此对象的碰撞层。如果检查物理设置,则可以确定哪个图层相互碰撞/触发。

因此,解决方案是添加一个名为TouchableObject的新图层,然后将您想要“可触摸”的任何对象指定给该图层,并在代码中为其指定相应的Collider(2d Collider或3d)想要一个3D对撞机,或将你的Physics.Raycast电话转换为Physics2D.Raycast)。在Touch脚本中,您可以仅LayerMask定位该图层。

答案 1 :(得分:0)

我能够像这样解决问题

LocalSystem

当然,点击的僵尸是我点击的对象

感谢@Eddge的帮助